media-cloud-sync
/
includes
/
sdk
/
google
/
ramsey
/
collection
/
src
/
Tool
/
ValueExtractorTrait.php
ValueExtractorTrait.php in Media Cloud Sync 1.4.1, at includes/sdk/google/ramsey/collection/src/Tool/ValueExtractorTrait.php
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * This file is part of the ramsey/collection library |
| 5 | * |
| 6 | * For the full copyright and license information, please view the LICENSE |
| 7 | * file that was distributed with this source code. |
| 8 | * |
| 9 | * @copyright Copyright (c) Ben Ramsey <ben@benramsey.com> |
| 10 | * @license http://opensource.org/licenses/MIT MIT |
| 11 | */ |
| 12 | declare (strict_types=1); |
| 13 | namespace Dudlewebs\WPMCS\GCP\Ramsey\Collection\Tool; |
| 14 | |
| 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; |
| 19 | use function is_object; |
| 20 | use function method_exists; |
| 21 | use function property_exists; |
| 22 | use function sprintf; |
| 23 | /** |
| 24 | * Provides functionality to extract the value of a property or method from an object. |
| 25 | */ |
| 26 | trait ValueExtractorTrait |
| 27 | { |
| 28 | /** |
| 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. |
| 35 | * |
| 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 |
| 40 | * value should be extracted. |
| 41 | * |
| 42 | * @return mixed the value extracted from the specified property, method, |
| 43 | * or array key, or the element itself. |
| 44 | * |
| 45 | * @throws InvalidPropertyOrMethod |
| 46 | * @throws UnsupportedOperationException |
| 47 | */ |
| 48 | protected function extractValue(mixed $element, ?string $propertyOrMethod) : mixed |
| 49 | { |
| 50 | if ($propertyOrMethod === null) { |
| 51 | return $element; |
| 52 | } |
| 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())); |
| 55 | } |
| 56 | if (is_array($element)) { |
| 57 | return $element[$propertyOrMethod] ?? throw new InvalidPropertyOrMethod(sprintf('Key or index "%s" not found in collection elements', $propertyOrMethod)); |
| 58 | } |
| 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)); |
| 76 | } |
| 77 | } |
| 78 |