Allow PHP-8.2 and up Compatibility instead of just PHP-8.4

This commit is contained in:
johnnyq
2026-06-12 17:06:10 -04:00
parent 2204bd52f4
commit d3a93652f3
220 changed files with 7198 additions and 2635 deletions

View File

@@ -33,6 +33,8 @@ use function Illuminate\Support\enum_value;
* @property-read HigherOrderCollectionProxy<TKey, TValue> $first
* @property-read HigherOrderCollectionProxy<TKey, TValue> $flatMap
* @property-read HigherOrderCollectionProxy<TKey, TValue> $groupBy
* @property-read HigherOrderCollectionProxy<TKey, TValue> $hasMany
* @property-read HigherOrderCollectionProxy<TKey, TValue> $hasSole
* @property-read HigherOrderCollectionProxy<TKey, TValue> $keyBy
* @property-read HigherOrderCollectionProxy<TKey, TValue> $last
* @property-read HigherOrderCollectionProxy<TKey, TValue> $map
@@ -81,6 +83,8 @@ trait EnumeratesValues
'first',
'flatMap',
'groupBy',
'hasMany',
'hasSole',
'keyBy',
'last',
'map',
@@ -329,6 +333,27 @@ trait EnumeratesValues
return $this->first($this->operatorForWhere(...func_get_args()));
}
/**
* Determine if the collection contains multiple items, optionally matching the given criteria.
*
* @param (callable(TValue, TKey): bool)|string|null $key
* @param mixed $operator
* @param mixed $value
* @return bool
*/
public function hasMany($key = null, $operator = null, $value = null): bool
{
$filter = func_num_args() > 1
? $this->operatorForWhere(...func_get_args())
: $key;
return $this
->unless($filter == null)
->filter($filter)
->take(2)
->count() === 2;
}
/**
* Get a single key's value from the first matching item in the collection.
*
@@ -340,11 +365,11 @@ trait EnumeratesValues
*/
public function value($key, $default = null)
{
if ($value = $this->firstWhere($key)) {
return data_get($value, $key, $default);
}
$value = $this->first(function ($target) use ($key) {
return data_has($target, $key);
});
return value($default);
return data_get($value, $key, $default);
}
/**
@@ -961,15 +986,12 @@ trait EnumeratesValues
public function jsonSerialize(): array
{
return array_map(function ($value) {
if ($value instanceof JsonSerializable) {
return $value->jsonSerialize();
} elseif ($value instanceof Jsonable) {
return json_decode($value->toJson(), true);
} elseif ($value instanceof Arrayable) {
return $value->toArray();
}
return $value;
return match (true) {
$value instanceof JsonSerializable => $value->jsonSerialize(),
$value instanceof Jsonable => json_decode($value->toJson(), true),
$value instanceof Arrayable => $value->toArray(),
default => $value,
};
}, $this->all());
}

View File

@@ -2,9 +2,12 @@
namespace Illuminate\Support\Traits;
use Illuminate\Database\Eloquent\Attributes\UseResource;
use Illuminate\Database\Eloquent\Attributes\UseResourceCollection;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Http\Resources\Json\ResourceCollection;
use LogicException;
use ReflectionClass;
trait TransformsToResourceCollection
{
@@ -47,6 +50,18 @@ trait TransformsToResourceCollection
throw_unless(method_exists($className, 'guessResourceName'), LogicException::class, sprintf('Expected class %s to implement guessResourceName method. Make sure the model uses the TransformsToResource trait.', $className));
$useResourceCollection = $this->resolveResourceCollectionFromAttribute($className);
if ($useResourceCollection !== null && class_exists($useResourceCollection)) {
return new $useResourceCollection($this);
}
$useResource = $this->resolveResourceFromAttribute($className);
if ($useResource !== null && class_exists($useResource)) {
return $useResource::collection($this);
}
$resourceClasses = $className::guessResourceName();
foreach ($resourceClasses as $resourceClass) {
@@ -65,4 +80,42 @@ trait TransformsToResourceCollection
throw new LogicException(sprintf('Failed to find resource class for model [%s].', $className));
}
/**
* Get the resource class from the class attribute.
*
* @param class-string<\Illuminate\Http\Resources\Json\JsonResource> $class
* @return class-string<*>|null
*/
protected function resolveResourceFromAttribute(string $class): ?string
{
if (! class_exists($class)) {
return null;
}
$attributes = (new ReflectionClass($class))->getAttributes(UseResource::class);
return $attributes !== []
? $attributes[0]->newInstance()->class
: null;
}
/**
* Get the resource collection class from the class attribute.
*
* @param class-string<\Illuminate\Http\Resources\Json\ResourceCollection> $class
* @return class-string<*>|null
*/
protected function resolveResourceCollectionFromAttribute(string $class): ?string
{
if (! class_exists($class)) {
return null;
}
$attributes = (new ReflectionClass($class))->getAttributes(UseResourceCollection::class);
return $attributes !== []
? $attributes[0]->newInstance()->class
: null;
}
}