Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 16 additions & 1 deletion src/Illuminate/Http/Resources/ConditionallyLoadsAttributes.php
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ protected function removeMissingValues($data)
}

/**
* Retrieve a value based on a given condition.
* Retrieve a value if the given "condition" is truthy.
*
* @param bool $condition
* @param mixed $value
Expand All @@ -109,6 +109,21 @@ protected function when($condition, $value, $default = null)
return func_num_args() === 3 ? value($default) : new MissingValue;
}

/**
* Retrieve a value if the given "condition" is falsy.
*
* @param bool $condition
* @param mixed $value
* @param mixed $default
* @return \Illuminate\Http\Resources\MissingValue|mixed
*/
public function unless($condition, $value, $default = null)
{
$arguments = func_num_args() === 2 ? [$value] : [$value, $default];

return $this->when(! $condition, ...$arguments);
}

/**
* Merge a value into the array.
*
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

namespace Illuminate\Tests\Integration\Http\Fixtures;

use Illuminate\Http\Resources\Json\JsonResource;

class PostResourceWithUnlessOptionalData extends JsonResource
{
public function toArray($request)
{
return [
'id' => $this->id,
'first' => $this->unless(false, 'value'),
'second' => $this->unless(true, 'value'),
'third' => $this->unless(true, function () {
return 'value';
}),
'fourth' => $this->unless(false, 'value', 'default'),
'fifth' => $this->unless(false, 'value', function () {
return 'default';
}),
];
}
}
26 changes: 26 additions & 0 deletions tests/Integration/Http/ResourceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
use Illuminate\Tests\Integration\Http\Fixtures\PostResourceWithOptionalRelationship;
use Illuminate\Tests\Integration\Http\Fixtures\PostResourceWithOptionalRelationshipCounts;
use Illuminate\Tests\Integration\Http\Fixtures\PostResourceWithoutWrap;
use Illuminate\Tests\Integration\Http\Fixtures\PostResourceWithUnlessOptionalData;
use Illuminate\Tests\Integration\Http\Fixtures\ReallyEmptyPostResource;
use Illuminate\Tests\Integration\Http\Fixtures\ResourceWithPreservedKeys;
use Illuminate\Tests\Integration\Http\Fixtures\SerializablePostResource;
Expand Down Expand Up @@ -167,6 +168,31 @@ public function testResourcesMayHaveOptionalValues()
]);
}

public function testResourcesMayHaveOptionalValuesUsingUnless()
{
Route::get('/', function () {
return new PostResourceWithUnlessOptionalData(new Post([
'id' => 5,
]));
});

$response = $this->withoutExceptionHandling()->get(
'/',
['Accept' => 'application/json']
);

$response->assertStatus(200);

$response->assertJson([
'data' => [
'id' => 5,
'first' => 'value',
'fourth' => 'value',
'fifth' => 'value',
],
]);
}

public function testResourcesMayHaveOptionalSelectedAttributes()
{
Route::get('/', function () {
Expand Down