Skip to content

Commit 4e32834

Browse files
committed
refactor: whenHas check for model & other resource type
1 parent ac78ce8 commit 4e32834

File tree

3 files changed

+37
-22
lines changed

3 files changed

+37
-22
lines changed

src/Descriptors/Values/Value.php

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,12 @@
33
namespace Ark4ne\JsonApi\Descriptors\Values;
44

55
use Ark4ne\JsonApi\Descriptors\Describer;
6-
use Ark4ne\JsonApi\Support\Arr;
76
use Ark4ne\JsonApi\Support\Config;
87
use Ark4ne\JsonApi\Support\Fields;
8+
use Ark4ne\JsonApi\Support\Values;
99
use Closure;
1010
use Illuminate\Database\Eloquent\Model;
1111
use Illuminate\Http\Request;
12-
use Illuminate\Support\Arr as IlluminateArr;
1312

1413
/**
1514
* @template T
@@ -81,23 +80,15 @@ public function whenHas(string $field = null): static
8180
Request $request,
8281
mixed $model,
8382
string $attribute
84-
): bool => match (true) {
85-
$model instanceof Model => array_key_exists($field ?? $attribute, $model->getAttributes()),
86-
IlluminateArr::accessible($model) => array_key_exists($field ?? $attribute, $model),
87-
default => property_exists($model, $field ?? $attribute)
88-
});
83+
): bool => Values::hasAttribute($model, $field ?? $attribute));
8984
}
9085

9186
public function resolveFor(Request $request, mixed $model, string $field): mixed
9287
{
9388
if ($this->attribute instanceof Closure) {
9489
$value = ($this->attribute)($model, $field);
95-
} elseif ($model instanceof Model) {
96-
$value = $model->getAttribute($this->attribute ?? $field);
97-
} elseif (IlluminateArr::accessible($model)) {
98-
$value = $model[$this->attribute ?? $field] ?? null;
9990
} else {
100-
$value = $model->{$this->attribute ?? $field} ?? null;
91+
$value = Values::getAttribute($model, $this->attribute ?? $field);
10192
}
10293

10394
return $value === null && $this->nullable

src/Resources/Concerns/ConditionallyLoadsAttributes.php

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use Ark4ne\JsonApi\Support\Fields;
99
use Ark4ne\JsonApi\Support\Includes;
1010
use Ark4ne\JsonApi\Support\Supported;
11+
use Ark4ne\JsonApi\Support\Values;
1112
use Illuminate\Http\Request;
1213
use Illuminate\Http\Resources\MergeValue;
1314
use Illuminate\Http\Resources\MissingValue;
@@ -72,8 +73,8 @@ protected function applyWhen(bool $condition, iterable $data): MergeValue
7273
}
7374

7475
/**
75-
* @polyfill JsonResource::whenHas
76-
* @see https://github.com/laravel/framework/pull/45376/files
76+
* @override JsonResource::whenHas
77+
* Support none Model resource
7778
*
7879
* Retrieve an attribute if it exists on the resource.
7980
*
@@ -84,22 +85,17 @@ protected function applyWhen(bool $condition, iterable $data): MergeValue
8485
*/
8586
public function whenHas($attribute, $value = null, $default = null)
8687
{
87-
if (Supported::$whenHas) {
88-
// @phpstan-ignore-next-line
89-
return parent::whenHas($attribute, $value, $default);
90-
}
91-
9288
if (func_num_args() < 3) {
9389
$default = new MissingValue;
9490
}
9591

96-
if (!array_key_exists($attribute, $this->resource->getAttributes())) {
92+
if (!Values::hasAttribute($this->resource, $attribute)) {
9793
return value($default);
9894
}
9995

10096
return func_num_args() === 1
101-
? $this->resource->{$attribute}
102-
: value($value, $this->resource->{$attribute});
97+
? Values::getAttribute($this->resource, $attribute)
98+
: value($value, Values::getAttribute($this->resource, $attribute));
10399
}
104100

105101
/**

src/Support/Values.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Ark4ne\JsonApi\Support;
44

5+
use Illuminate\Database\Eloquent\Model;
56
use Illuminate\Http\Resources\Json\JsonResource;
67
use Illuminate\Http\Resources\MergeValue;
78
use Illuminate\Http\Resources\PotentiallyMissing;
@@ -85,4 +86,31 @@ public static function isMissing(mixed $resource): bool
8586
$resource->resource instanceof PotentiallyMissing &&
8687
$resource->resource->isMissing());
8788
}
89+
90+
/**
91+
* @param array<array-key, mixed>|object $object
92+
* @param string $attribute
93+
* @return bool
94+
*/
95+
public static function hasAttribute(array|object $object, string $attribute): bool
96+
{
97+
if ($object instanceof Model) {
98+
return array_key_exists($attribute, $object->getAttributes());
99+
}
100+
if (Arr::accessible($object)) {
101+
return Arr::exists($object, $attribute);
102+
}
103+
return property_exists($object, $attribute);
104+
}
105+
106+
public static function getAttribute(array|object $object, string $attribute): mixed
107+
{
108+
if ($object instanceof Model) {
109+
return $object->getAttribute($attribute);
110+
}
111+
if (Arr::accessible($object)) {
112+
return $object[$attribute] ?? null;
113+
}
114+
return $object->$attribute ?? null;
115+
}
88116
}

0 commit comments

Comments
 (0)