-   Notifications  
You must be signed in to change notification settings  - Fork 11.6k
 
[12.x] EnumerateValues::value() support and return negative values if exists #54910 #57566
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[12.x] EnumerateValues::value() support and return negative values if exists #54910 #57566
Conversation
…m in negative case
…m in negative case
|   @rafaelqueiroz I don't think this fixes the expected behavior. The last code assertion is this: $c = new $collection([['id' => 1], ['id' => 2, 'balance' => 200]]); $this->assertEquals(200, $c->value('balance'));If we change the input data to $c = new $collection([['id' => 1], ['id' => 2, 'balance' => 0]]); dump($c->value('balance'));It outputs: $ php artisan local:test null // routes/local.php:33Ignoring the balance with a  Or: $c = new $collection([ ['id' => 1], ['id' => 2, 'balance' => 0], ['id' => 3, 'balance' => 200], ]); dump($c->value('balance'));Which outputs: $ php artisan local:test 200 // routes/local.php:33And also ignores the balance with a  It just fixes the very first element.  |  
|   @rodrigopedra you're right, probably it would be impossible use   |  
|   @rafaelqueiroz I tried this, and it seems to work for the test cases on your PR and the ones I added in my comment: public function value($key, $default = null) { if ($value = $this->first(fn ($v) => Arr::accessible($v) && Arr::exists($v, $key))) { return data_get($value, $key, $default); } return value($default); }It grabs the first item where the key exists and returns whatever value it has, even   |  
 @rodrigopedra we are at the same page :)  |  
|   @rafaelqueiroz there is still a problem with a collection of objects... I am looking into it.  |  
 
 Really? No make to much sense because the support classes and helper methods will handle that.  |  
 
 
 @rafaelqueiroz this should do it: <?php // routes/console.php use Illuminate\Support\Collection; use Illuminate\Support\Facades\Artisan; Artisan::command('local:test', function () { Collection::macro('myValue', function ($key, $default = null) { if ($value = $this->first(fn ($item) => filled(data_get($item, $key)))) { return data_get($value, $key, $default); } return value($default); }); $collection = collect([ ['id' => 1], ['id' => 2, 'balance' => 0], ['id' => 3, 'balance' => 200], ]); dump($collection->myValue('balance')); $collection = collect([ literal(id: 1), literal(id: 2, balance: 0), literal(id: 3, balance: 200), ]); dump($collection->myValue('balance')); // nested $collection = collect([ ['id' => 1], ['id' => 2, 'balance' => ['currency' => 'USD', 'value' => 0]], ['id' => 3, 'balance' => ['currency' => 'USD', 'value' => 200]], ]); dump($collection->myValue('balance.value')); $collection = collect([ literal(id: 1), literal(id: 2, balance: literal(currency: 'USD', value: 0)), literal(id: 3, balance: literal(currency: 'USD', value: 200)), ]); dump($collection->myValue('balance.value')); // collection of scalars => should return null, as no matching record $collection = collect([1, 2, 3]); dump($collection->value('balance')); });Outputs: $ php artisan local:test 0 // routes/local.php:22 0 // routes/local.php:30 0 // routes/local.php:40 0 // routes/local.php:48 null // routes/local.php:54Note that I used a macro to test it out.  |  
|   @rodrigopedra I got it, let's move for #57570  |  
Another one from The value collection method shouldn't consider 0 as null #54910