| @@ 9-33 (lines=25) @@ | ||
| 6 | use Innmind\Reflection\ExtractionStrategyInterface; |
|
| 7 | use Innmind\Reflection\Exception\LogicException; |
|
| 8 | ||
| 9 | class NamedMethodStrategy implements ExtractionStrategyInterface |
|
| 10 | { |
|
| 11 | /** |
|
| 12 | * {@inheritdoc} |
|
| 13 | */ |
|
| 14 | public function supports($object, string $property): bool |
|
| 15 | { |
|
| 16 | $refl = new \ReflectionObject($object); |
|
| 17 | ||
| 18 | return $refl->hasMethod($property) && |
|
| 19 | $refl->getMethod($property)->getNumberOfRequiredParameters() === 0; |
|
| 20 | } |
|
| 21 | ||
| 22 | /** |
|
| 23 | * {@inheritdoc} |
|
| 24 | */ |
|
| 25 | public function extract($object, string $property) |
|
| 26 | { |
|
| 27 | if (!$this->supports($object, $property)) { |
|
| 28 | throw new LogicException; |
|
| 29 | } |
|
| 30 | ||
| 31 | return $object->$property(); |
|
| 32 | } |
|
| 33 | } |
|
| 34 | ||
| @@ 19-43 (lines=25) @@ | ||
| 16 | * public function foo($foo); |
|
| 17 | * </code> |
|
| 18 | */ |
|
| 19 | class NamedMethodStrategy implements InjectionStrategyInterface |
|
| 20 | { |
|
| 21 | /** |
|
| 22 | * {@inheritdoc} |
|
| 23 | */ |
|
| 24 | public function supports($object, string $property, $value): bool |
|
| 25 | { |
|
| 26 | $refl = new \ReflectionObject($object); |
|
| 27 | ||
| 28 | return $refl->hasMethod($property) && |
|
| 29 | $refl->getMethod($property)->getNumberOfParameters() > 0; |
|
| 30 | } |
|
| 31 | ||
| 32 | /** |
|
| 33 | * {@inheritdoc} |
|
| 34 | */ |
|
| 35 | public function inject($object, string $property, $value) |
|
| 36 | { |
|
| 37 | if (!$this->supports($object, $property, $value)) { |
|
| 38 | throw new LogicException; |
|
| 39 | } |
|
| 40 | ||
| 41 | $object->$property($value); |
|
| 42 | } |
|
| 43 | } |
|
| 44 | ||