| 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 <[email protected]> |
| 10 |
* @license http://opensource.org/licenses/MIT MIT |
| 11 |
*/ |
| 12 |
|
| 13 |
declare(strict_types=1); |
| 14 |
|
| 15 |
namespace Ramsey\Collection\Tool; |
| 16 |
|
| 17 |
use Ramsey\Collection\Exception\InvalidPropertyOrMethod; |
| 18 |
use Ramsey\Collection\Exception\UnsupportedOperationException; |
| 19 |
use ReflectionProperty; |
| 20 |
|
| 21 |
use function is_array; |
| 22 |
use function is_object; |
| 23 |
use function method_exists; |
| 24 |
use function property_exists; |
| 25 |
use function sprintf; |
| 26 |
|
| 27 |
/** |
| 28 |
* Provides functionality to extract the value of a property or method from an object. |
| 29 |
*/ |
| 30 |
trait ValueExtractorTrait |
| 31 |
{ |
| 32 |
/** |
| 33 |
* Returns the type associated with this collection. |
| 34 |
*/ |
| 35 |
abstract public function getType(): string; |
| 36 |
|
| 37 |
/** |
| 38 |
* Extracts the value of the given property, method, or array key from the |
| 39 |
* element. |
| 40 |
* |
| 41 |
* If `$propertyOrMethod` is `null`, we return the element as-is. |
| 42 |
* |
| 43 |
* @param mixed $element The element to extract the value from. |
| 44 |
* @param string | null $propertyOrMethod The property or method for which the |
| 45 |
* value should be extracted. |
| 46 |
* |
| 47 |
* @return mixed the value extracted from the specified property, method, |
| 48 |
* or array key, or the element itself. |
| 49 |
* |
| 50 |
* @throws InvalidPropertyOrMethod |
| 51 |
* @throws UnsupportedOperationException |
| 52 |
*/ |
| 53 |
protected function extractValue(mixed $element, ?string $propertyOrMethod): mixed |
| 54 |
{ |
| 55 |
if ($propertyOrMethod === null) { |
| 56 |
return $element; |
| 57 |
} |
| 58 |
|
| 59 |
if (!is_object($element) && !is_array($element)) { |
| 60 |
throw new UnsupportedOperationException(sprintf( |
| 61 |
'The collection type "%s" does not support the $propertyOrMethod parameter', |
| 62 |
$this->getType(), |
| 63 |
)); |
| 64 |
} |
| 65 |
|
| 66 |
if (is_array($element)) { |
| 67 |
return $element[$propertyOrMethod] ?? throw new InvalidPropertyOrMethod(sprintf( |
| 68 |
'Key or index "%s" not found in collection elements', |
| 69 |
$propertyOrMethod, |
| 70 |
)); |
| 71 |
} |
| 72 |
|
| 73 |
if (property_exists($element, $propertyOrMethod) && method_exists($element, $propertyOrMethod)) { |
| 74 |
$reflectionProperty = new ReflectionProperty($element, $propertyOrMethod); |
| 75 |
if ($reflectionProperty->isPublic()) { |
| 76 |
return $element->$propertyOrMethod; |
| 77 |
} |
| 78 |
|
| 79 |
return $element->{$propertyOrMethod}(); |
| 80 |
} |
| 81 |
|
| 82 |
if (property_exists($element, $propertyOrMethod)) { |
| 83 |
return $element->$propertyOrMethod; |
| 84 |
} |
| 85 |
|
| 86 |
if (method_exists($element, $propertyOrMethod)) { |
| 87 |
return $element->{$propertyOrMethod}(); |
| 88 |
} |
| 89 |
|
| 90 |
if (isset($element->$propertyOrMethod)) { |
| 91 |
return $element->$propertyOrMethod; |
| 92 |
} |
| 93 |
|
| 94 |
throw new InvalidPropertyOrMethod(sprintf( |
| 95 |
'Method or property "%s" not defined in %s', |
| 96 |
$propertyOrMethod, |
| 97 |
$element::class, |
| 98 |
)); |
| 99 |
} |
| 100 |
} |