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