← All changes
|
includes/sdk/google/ramsey/collection/src/Tool/ValueExtractorTrait.php
+43
-20
1.2.12
→
1.4.2
View file →
| @@ -9,12 +9,14 @@ | ||
| 9 | 9 | * @copyright Copyright (c) Ben Ramsey <[email protected]> |
| 10 | 10 | * @license http://opensource.org/licenses/MIT MIT |
| 11 | 11 | */ |
| 12 | 12 | declare (strict_types=1); |
| 13 | -namespace Dudlewebs\WPMCS\Ramsey\Collection\Tool; | |
| 13 | +namespace Dudlewebs\WPMCS\GCP\Ramsey\Collection\Tool; | |
| 14 | 14 | |
| 15 | -use Dudlewebs\WPMCS\Ramsey\Collection\Exception\ValueExtractionException; | |
| 16 | -use function get_class; | |
| 15 | +use Dudlewebs\WPMCS\GCP\Ramsey\Collection\Exception\InvalidPropertyOrMethod; | |
| 16 | +use Dudlewebs\WPMCS\GCP\Ramsey\Collection\Exception\UnsupportedOperationException; | |
| 17 | +use ReflectionProperty; | |
| 18 | +use function is_array; | |
| 17 | 19 | use function is_object; |
| 18 | 20 | use function method_exists; |
| 19 | 21 | use function property_exists; |
| 20 | 22 | use function sprintf; |
| @@ -23,32 +25,53 @@ | ||
| 23 | 25 | */ |
| 24 | 26 | trait ValueExtractorTrait |
| 25 | 27 | { |
| 26 | 28 | /** |
| 27 | - * Extracts the value of the given property or method from the object. | |
| 29 | + * Returns the type associated with this collection. | |
| 30 | + */ | |
| 31 | + public abstract function getType() : string; | |
| 32 | + /** | |
| 33 | + * Extracts the value of the given property, method, or array key from the | |
| 34 | + * element. | |
| 28 | 35 | * |
| 29 | - * @param mixed $object The object to extract the value from. | |
| 30 | - * @param string $propertyOrMethod The property or method for which the | |
| 36 | + * If `$propertyOrMethod` is `null`, we return the element as-is. | |
| 37 | + * | |
| 38 | + * @param mixed $element The element to extract the value from. | |
| 39 | + * @param string | null $propertyOrMethod The property or method for which the | |
| 31 | 40 | * value should be extracted. |
| 32 | 41 | * |
| 33 | - * @return mixed the value extracted from the specified property or method. | |
| 42 | + * @return mixed the value extracted from the specified property, method, | |
| 43 | + * or array key, or the element itself. | |
| 34 | 44 | * |
| 35 | - * @throws ValueExtractionException if the method or property is not defined. | |
| 45 | + * @throws InvalidPropertyOrMethod | |
| 46 | + * @throws UnsupportedOperationException | |
| 36 | 47 | */ |
| 37 | - // phpcs:ignore SlevomatCodingStandard.TypeHints.ParameterTypeHint.MissingNativeTypeHint | |
| 38 | - protected function extractValue($object, string $propertyOrMethod) | |
| 48 | + protected function extractValue(mixed $element, ?string $propertyOrMethod) : mixed | |
| 39 | 49 | { |
| 40 | - if (!is_object($object)) { | |
| 41 | - throw new ValueExtractionException('Unable to extract a value from a non-object'); | |
| 50 | + if ($propertyOrMethod === null) { | |
| 51 | + return $element; | |
| 42 | 52 | } |
| 43 | - if (property_exists($object, $propertyOrMethod)) { | |
| 44 | - return $object->{$propertyOrMethod}; | |
| 53 | + if (!is_object($element) && !is_array($element)) { | |
| 54 | + throw new UnsupportedOperationException(sprintf('The collection type "%s" does not support the $propertyOrMethod parameter', $this->getType())); | |
| 45 | 55 | } |
| 46 | - if (method_exists($object, $propertyOrMethod)) { | |
| 47 | - return $object->{$propertyOrMethod}(); | |
| 56 | + if (is_array($element)) { | |
| 57 | + return $element[$propertyOrMethod] ?? throw new InvalidPropertyOrMethod(sprintf('Key or index "%s" not found in collection elements', $propertyOrMethod)); | |
| 48 | 58 | } |
| 49 | - throw new ValueExtractionException( | |
| 50 | - // phpcs:ignore SlevomatCodingStandard.Classes.ModernClassNameReference.ClassNameReferencedViaFunctionCall | |
| 51 | - sprintf('Method or property "%s" not defined in %s', $propertyOrMethod, get_class($object)) | |
| 52 | - ); | |
| 59 | + if (property_exists($element, $propertyOrMethod) && method_exists($element, $propertyOrMethod)) { | |
| 60 | + $reflectionProperty = new ReflectionProperty($element, $propertyOrMethod); | |
| 61 | + if ($reflectionProperty->isPublic()) { | |
| 62 | + return $element->{$propertyOrMethod}; | |
| 63 | + } | |
| 64 | + return $element->{$propertyOrMethod}(); | |
| 65 | + } | |
| 66 | + if (property_exists($element, $propertyOrMethod)) { | |
| 67 | + return $element->{$propertyOrMethod}; | |
| 68 | + } | |
| 69 | + if (method_exists($element, $propertyOrMethod)) { | |
| 70 | + return $element->{$propertyOrMethod}(); | |
| 71 | + } | |
| 72 | + if (isset($element->{$propertyOrMethod})) { | |
| 73 | + return $element->{$propertyOrMethod}; | |
| 74 | + } | |
| 75 | + throw new InvalidPropertyOrMethod(sprintf('Method or property "%s" not defined in %s', $propertyOrMethod, $element::class)); | |
| 53 | 76 | } |
| 54 | 77 | } |