Skip to content

Commit e5caff0

Browse files
committed
add whenFilled() api resource helper
1 parent ef5aa3d commit e5caff0

File tree

3 files changed

+54
-0
lines changed

3 files changed

+54
-0
lines changed

src/Illuminate/Http/Resources/ConditionallyLoadsAttributes.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,20 @@ protected function whenNotNull($value, $default = new MissingValue)
227227
return $this->when(! is_null($value), ...$arguments);
228228
}
229229

230+
/**
231+
* Retrieve a model attribute if it is filled.
232+
*
233+
* @param mixed $value
234+
* @param mixed $default
235+
* @return \Illuminate\Http\Resources\MissingValue|mixed
236+
*/
237+
protected function whenFilled($value, $default = new MissingValue)
238+
{
239+
$arguments = func_num_args() == 1 ? [$value] : [$value, $default];
240+
241+
return $this->when(filled($value), ...$arguments);
242+
}
243+
230244
/**
231245
* Retrieve an accessor when it has been appended.
232246
*
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
3+
namespace Illuminate\Tests\Integration\Http\Fixtures;
4+
5+
use Illuminate\Http\Resources\Json\JsonResource;
6+
7+
class PostResourceWithOptionalFilledAttributes extends JsonResource
8+
{
9+
public function toArray($request)
10+
{
11+
return [
12+
'id' => $this->whenFilled($this->id, 42),
13+
'title' => $this->whenFilled($this->title, 'no title'),
14+
];
15+
}
16+
}

tests/Integration/Http/ResourceTest.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
use Illuminate\Tests\Integration\Http\Fixtures\PostResourceWithOptionalAppendedAttributes;
3232
use Illuminate\Tests\Integration\Http\Fixtures\PostResourceWithOptionalAttributes;
3333
use Illuminate\Tests\Integration\Http\Fixtures\PostResourceWithOptionalData;
34+
use Illuminate\Tests\Integration\Http\Fixtures\PostResourceWithOptionalFilledAttributes;
3435
use Illuminate\Tests\Integration\Http\Fixtures\PostResourceWithOptionalHasAttributes;
3536
use Illuminate\Tests\Integration\Http\Fixtures\PostResourceWithOptionalMerging;
3637
use Illuminate\Tests\Integration\Http\Fixtures\PostResourceWithOptionalPivotRelationship;
@@ -217,6 +218,29 @@ public function testResourcesMayHaveOptionalSelectedAttributes()
217218
]);
218219
}
219220

221+
public function testResourcesMayHaveOptionalFilledAttributes()
222+
{
223+
Route::get('/', function () {
224+
return new PostResourceWithOptionalFilledAttributes(new Post([
225+
'id' => 5,
226+
'title' => '',
227+
]));
228+
});
229+
230+
$response = $this->withoutExceptionHandling()->get(
231+
'/', ['Accept' => 'application/json']
232+
);
233+
234+
$response->assertStatus(200);
235+
236+
$response->assertJson([
237+
'data' => [
238+
'id' => 5,
239+
'title' => 'no title',
240+
],
241+
]);
242+
}
243+
220244
public function testResourcesMayHaveOptionalHasAttributes()
221245
{
222246
Route::get('/', function () {

0 commit comments

Comments
 (0)