Skip to content
Closed
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
62 changes: 19 additions & 43 deletions Resources/ConditionallyLoadsAttributes.php
Original file line number Diff line number Diff line change
Expand Up @@ -100,13 +100,13 @@ protected function removeMissingValues($data)
* @param mixed $default
* @return \Illuminate\Http\Resources\MissingValue|mixed
*/
protected function when($condition, $value, $default = null)
protected function when($condition, $value, $default = new MissingValue)
{
if ($condition) {
return value($value);
}

return func_num_args() === 3 ? value($default) : new MissingValue;
return func_num_args() === 3 ? value($default) : $default;
}

/**
Expand All @@ -117,7 +117,7 @@ protected function when($condition, $value, $default = null)
* @param mixed $default
* @return \Illuminate\Http\Resources\MissingValue|mixed
*/
public function unless($condition, $value, $default = null)
public function unless($condition, $value, $default = new MissingValue)
{
$arguments = func_num_args() === 2 ? [$value] : [$value, $default];

Expand All @@ -143,13 +143,13 @@ protected function merge($value)
* @param mixed $default
* @return \Illuminate\Http\Resources\MergeValue|mixed
*/
protected function mergeWhen($condition, $value, $default = null)
protected function mergeWhen($condition, $value, $default = new MissingValue)
{
if ($condition) {
return new MergeValue(value($value));
}

return func_num_args() === 3 ? new MergeValue(value($default)) : new MissingValue();
return func_num_args() === 3 ? new MergeValue(value($default)) : $default;
}

/**
Expand All @@ -160,7 +160,7 @@ protected function mergeWhen($condition, $value, $default = null)
* @param mixed $default
* @return \Illuminate\Http\Resources\MergeValue|mixed
*/
protected function mergeUnless($condition, $value, $default = null)
protected function mergeUnless($condition, $value, $default = new MissingValue)
{
$arguments = func_num_args() === 2 ? [$value] : [$value, $default];

Expand Down Expand Up @@ -188,12 +188,8 @@ protected function attributes($attributes)
* @param mixed $default
* @return \Illuminate\Http\Resources\MissingValue|mixed
*/
public function whenHas($attribute, $value = null, $default = null)
public function whenHas($attribute, $value = null, $default = new MissingValue)
{
if (func_num_args() < 3) {
$default = new MissingValue;
}

if (! array_key_exists($attribute, $this->resource->getAttributes())) {
return value($default);
}
Expand All @@ -210,7 +206,7 @@ public function whenHas($attribute, $value = null, $default = null)
* @param mixed $default
* @return \Illuminate\Http\Resources\MissingValue|mixed
*/
protected function whenNull($value, $default = null)
protected function whenNull($value, $default = new MissingValue)
{
$arguments = func_num_args() == 1 ? [$value] : [$value, $default];

Expand All @@ -224,7 +220,7 @@ protected function whenNull($value, $default = null)
* @param mixed $default
* @return \Illuminate\Http\Resources\MissingValue|mixed
*/
protected function whenNotNull($value, $default = null)
protected function whenNotNull($value, $default = new MissingValue)
{
$arguments = func_num_args() == 1 ? [$value] : [$value, $default];

Expand All @@ -239,13 +235,13 @@ protected function whenNotNull($value, $default = null)
* @param mixed $default
* @return \Illuminate\Http\Resources\MissingValue|mixed
*/
protected function whenAppended($attribute, $value = null, $default = null)
protected function whenAppended($attribute, $value = null, $default = new MissingValue)
{
if ($this->resource->hasAppended($attribute)) {
return func_num_args() >= 2 ? value($value) : $this->resource->$attribute;
}

return func_num_args() === 3 ? value($default) : new MissingValue;
return func_num_args() === 3 ? value($default) : $default;
}

/**
Expand All @@ -256,12 +252,8 @@ protected function whenAppended($attribute, $value = null, $default = null)
* @param mixed $default
* @return \Illuminate\Http\Resources\MissingValue|mixed
*/
protected function whenLoaded($relationship, $value = null, $default = null)
protected function whenLoaded($relationship, $value = null, $default = new MissingValue)
{
if (func_num_args() < 3) {
$default = new MissingValue;
}

if (! $this->resource->relationLoaded($relationship)) {
return value($default);
}
Expand Down Expand Up @@ -291,12 +283,8 @@ protected function whenLoaded($relationship, $value = null, $default = null)
* @param mixed $default
* @return \Illuminate\Http\Resources\MissingValue|mixed
*/
public function whenCounted($relationship, $value = null, $default = null)
public function whenCounted($relationship, $value = null, $default = new MissingValue)
{
if (func_num_args() < 3) {
$default = new MissingValue;
}

$attribute = (new Stringable($relationship))->snake()->finish('_count')->value();

if (! array_key_exists($attribute, $this->resource->getAttributes())) {
Expand Down Expand Up @@ -328,12 +316,8 @@ public function whenCounted($relationship, $value = null, $default = null)
* @param mixed $default
* @return \Illuminate\Http\Resources\MissingValue|mixed
*/
public function whenAggregated($relationship, $column, $aggregate, $value = null, $default = null)
public function whenAggregated($relationship, $column, $aggregate, $value = null, $default = new MissingValue)
{
if (func_num_args() < 5) {
$default = new MissingValue;
}

$attribute = (new Stringable($relationship))->snake()->append('_')->append($aggregate)->append('_')->finish($column)->value();

if (! array_key_exists($attribute, $this->resource->getAttributes())) {
Expand Down Expand Up @@ -363,12 +347,8 @@ public function whenAggregated($relationship, $column, $aggregate, $value = null
* @param mixed $default
* @return \Illuminate\Http\Resources\MissingValue|mixed
*/
public function whenExistsLoaded($relationship, $value = null, $default = null)
public function whenExistsLoaded($relationship, $value = null, $default = new MissingValue)
{
if (func_num_args() < 3) {
$default = new MissingValue;
}

$attribute = (new Stringable($relationship))->snake()->finish('_exists')->value();

if (! array_key_exists($attribute, $this->resource->getAttributes())) {
Expand All @@ -394,7 +374,7 @@ public function whenExistsLoaded($relationship, $value = null, $default = null)
* @param mixed $default
* @return \Illuminate\Http\Resources\MissingValue|mixed
*/
protected function whenPivotLoaded($table, $value, $default = null)
protected function whenPivotLoaded($table, $value, $default = new MissingValue)
{
return $this->whenPivotLoadedAs('pivot', ...func_get_args());
}
Expand All @@ -408,12 +388,8 @@ protected function whenPivotLoaded($table, $value, $default = null)
* @param mixed $default
* @return \Illuminate\Http\Resources\MissingValue|mixed
*/
protected function whenPivotLoadedAs($accessor, $table, $value, $default = null)
protected function whenPivotLoadedAs($accessor, $table, $value, $default = new MissingValue)
{
if (func_num_args() === 3) {
$default = new MissingValue;
}

return $this->when(
$this->hasPivotLoadedAs($accessor, $table),
$value,
Expand Down Expand Up @@ -454,10 +430,10 @@ protected function hasPivotLoadedAs($accessor, $table)
* @param mixed $default
* @return mixed
*/
protected function transform($value, callable $callback, $default = null)
protected function transform($value, callable $callback, $default = new MissingValue)
{
return transform(
$value, $callback, func_num_args() === 3 ? $default : new MissingValue
$value, $callback, $default
);
}
}
Loading