Commaaa2
This commit is contained in:
49
vendor/ramsey/collection/src/Tool/TypeTrait.php
vendored
49
vendor/ramsey/collection/src/Tool/TypeTrait.php
vendored
@@ -36,39 +36,22 @@ trait TypeTrait
|
||||
* @param string $type The type to check the value against.
|
||||
* @param mixed $value The value to check.
|
||||
*/
|
||||
// phpcs:ignore SlevomatCodingStandard.TypeHints.ParameterTypeHint.MissingNativeTypeHint
|
||||
protected function checkType(string $type, $value): bool
|
||||
protected function checkType(string $type, mixed $value): bool
|
||||
{
|
||||
switch ($type) {
|
||||
case 'array':
|
||||
return is_array($value);
|
||||
case 'bool':
|
||||
case 'boolean':
|
||||
return is_bool($value);
|
||||
case 'callable':
|
||||
return is_callable($value);
|
||||
case 'float':
|
||||
case 'double':
|
||||
return is_float($value);
|
||||
case 'int':
|
||||
case 'integer':
|
||||
return is_int($value);
|
||||
case 'null':
|
||||
return $value === null;
|
||||
case 'numeric':
|
||||
return is_numeric($value);
|
||||
case 'object':
|
||||
return is_object($value);
|
||||
case 'resource':
|
||||
return is_resource($value);
|
||||
case 'scalar':
|
||||
return is_scalar($value);
|
||||
case 'string':
|
||||
return is_string($value);
|
||||
case 'mixed':
|
||||
return true;
|
||||
default:
|
||||
return $value instanceof $type;
|
||||
}
|
||||
return match ($type) {
|
||||
'array' => is_array($value),
|
||||
'bool', 'boolean' => is_bool($value),
|
||||
'callable' => is_callable($value),
|
||||
'float', 'double' => is_float($value),
|
||||
'int', 'integer' => is_int($value),
|
||||
'null' => $value === null,
|
||||
'numeric' => is_numeric($value),
|
||||
'object' => is_object($value),
|
||||
'resource' => is_resource($value),
|
||||
'scalar' => is_scalar($value),
|
||||
'string' => is_string($value),
|
||||
'mixed' => true,
|
||||
default => $value instanceof $type,
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
@@ -14,9 +14,10 @@ declare(strict_types=1);
|
||||
|
||||
namespace Ramsey\Collection\Tool;
|
||||
|
||||
use Ramsey\Collection\Exception\ValueExtractionException;
|
||||
use Ramsey\Collection\Exception\InvalidPropertyOrMethod;
|
||||
use Ramsey\Collection\Exception\UnsupportedOperationException;
|
||||
|
||||
use function get_class;
|
||||
use function is_array;
|
||||
use function is_object;
|
||||
use function method_exists;
|
||||
use function property_exists;
|
||||
@@ -28,34 +29,53 @@ use function sprintf;
|
||||
trait ValueExtractorTrait
|
||||
{
|
||||
/**
|
||||
* Extracts the value of the given property or method from the object.
|
||||
* Extracts the value of the given property, method, or array key from the
|
||||
* element.
|
||||
*
|
||||
* @param mixed $object The object to extract the value from.
|
||||
* @param string $propertyOrMethod The property or method for which the
|
||||
* If `$propertyOrMethod` is `null`, we return the element as-is.
|
||||
*
|
||||
* @param mixed $element The element to extract the value from.
|
||||
* @param string | null $propertyOrMethod The property or method for which the
|
||||
* value should be extracted.
|
||||
*
|
||||
* @return mixed the value extracted from the specified property or method.
|
||||
* @return mixed the value extracted from the specified property, method,
|
||||
* or array key, or the element itself.
|
||||
*
|
||||
* @throws ValueExtractionException if the method or property is not defined.
|
||||
* @throws InvalidPropertyOrMethod
|
||||
* @throws UnsupportedOperationException
|
||||
*/
|
||||
// phpcs:ignore SlevomatCodingStandard.TypeHints.ParameterTypeHint.MissingNativeTypeHint
|
||||
protected function extractValue($object, string $propertyOrMethod)
|
||||
protected function extractValue(mixed $element, ?string $propertyOrMethod): mixed
|
||||
{
|
||||
if (!is_object($object)) {
|
||||
throw new ValueExtractionException('Unable to extract a value from a non-object');
|
||||
if ($propertyOrMethod === null) {
|
||||
return $element;
|
||||
}
|
||||
|
||||
if (property_exists($object, $propertyOrMethod)) {
|
||||
return $object->$propertyOrMethod;
|
||||
if (!is_object($element) && !is_array($element)) {
|
||||
throw new UnsupportedOperationException(sprintf(
|
||||
'The collection type "%s" does not support the $propertyOrMethod parameter',
|
||||
$this->getType(),
|
||||
));
|
||||
}
|
||||
|
||||
if (method_exists($object, $propertyOrMethod)) {
|
||||
return $object->{$propertyOrMethod}();
|
||||
if (is_array($element)) {
|
||||
return $element[$propertyOrMethod] ?? throw new InvalidPropertyOrMethod(sprintf(
|
||||
'Key or index "%s" not found in collection elements',
|
||||
$propertyOrMethod,
|
||||
));
|
||||
}
|
||||
|
||||
throw new ValueExtractionException(
|
||||
// phpcs:ignore SlevomatCodingStandard.Classes.ModernClassNameReference.ClassNameReferencedViaFunctionCall
|
||||
sprintf('Method or property "%s" not defined in %s', $propertyOrMethod, get_class($object)),
|
||||
);
|
||||
if (property_exists($element, $propertyOrMethod)) {
|
||||
return $element->$propertyOrMethod;
|
||||
}
|
||||
|
||||
if (method_exists($element, $propertyOrMethod)) {
|
||||
return $element->{$propertyOrMethod}();
|
||||
}
|
||||
|
||||
throw new InvalidPropertyOrMethod(sprintf(
|
||||
'Method or property "%s" not defined in %s',
|
||||
$propertyOrMethod,
|
||||
$element::class,
|
||||
));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -16,7 +16,7 @@ namespace Ramsey\Collection\Tool;
|
||||
|
||||
use DateTimeInterface;
|
||||
|
||||
use function get_class;
|
||||
use function assert;
|
||||
use function get_resource_type;
|
||||
use function is_array;
|
||||
use function is_bool;
|
||||
@@ -24,7 +24,6 @@ use function is_callable;
|
||||
use function is_object;
|
||||
use function is_resource;
|
||||
use function is_scalar;
|
||||
use function var_export;
|
||||
|
||||
/**
|
||||
* Provides functionality to express a value as string
|
||||
@@ -46,8 +45,7 @@ trait ValueToStringTrait
|
||||
*
|
||||
* @param mixed $value the value to return as a string.
|
||||
*/
|
||||
// phpcs:ignore SlevomatCodingStandard.TypeHints.ParameterTypeHint.MissingNativeTypeHint
|
||||
protected function toolValueToString($value): string
|
||||
protected function toolValueToString(mixed $value): string
|
||||
{
|
||||
// null
|
||||
if ($value === null) {
|
||||
@@ -74,12 +72,8 @@ trait ValueToStringTrait
|
||||
return '(' . get_resource_type($value) . ' resource #' . (int) $value . ')';
|
||||
}
|
||||
|
||||
// If we don't know what it is, use var_export().
|
||||
if (!is_object($value)) {
|
||||
return '(' . var_export($value, true) . ')';
|
||||
}
|
||||
|
||||
// From here, $value should be an object.
|
||||
assert(is_object($value));
|
||||
|
||||
// __toString() is implemented
|
||||
if (is_callable([$value, '__toString'])) {
|
||||
@@ -92,7 +86,6 @@ trait ValueToStringTrait
|
||||
}
|
||||
|
||||
// unknown type
|
||||
// phpcs:ignore SlevomatCodingStandard.Classes.ModernClassNameReference.ClassNameReferencedViaFunctionCall
|
||||
return '(' . get_class($value) . ' Object)';
|
||||
return '(' . $value::class . ' Object)';
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user