@@ 9-47 (lines=39) @@ | ||
6 | use Innmind\Reflection\Exception\LogicException; |
|
7 | use Innmind\Immutable\StringPrimitive; |
|
8 | ||
9 | class GetterStrategy implements ExtractionStrategyInterface |
|
10 | { |
|
11 | private $getter; |
|
12 | ||
13 | public function __construct() |
|
14 | { |
|
15 | $this->getter = new StringPrimitive('get%s'); |
|
16 | } |
|
17 | ||
18 | /** |
|
19 | * {@inheritdoc} |
|
20 | */ |
|
21 | public function supports($object, string $property): bool |
|
22 | { |
|
23 | $refl = new \ReflectionObject($object); |
|
24 | $getter = (string) $this->getter->sprintf( |
|
25 | (string) (new StringPrimitive($property))->camelize() |
|
26 | ); |
|
27 | ||
28 | return $refl->hasMethod($getter) && |
|
29 | $refl->getMethod($getter)->getNumberOfRequiredParameters() === 0; |
|
30 | } |
|
31 | ||
32 | /** |
|
33 | * {@inheritdoc} |
|
34 | */ |
|
35 | public function extract($object, string $property) |
|
36 | { |
|
37 | if (!$this->supports($object, $property)) { |
|
38 | throw new LogicException; |
|
39 | } |
|
40 | ||
41 | $getter = (string) $this->getter->sprintf( |
|
42 | (string) (new StringPrimitive($property))->camelize() |
|
43 | ); |
|
44 | ||
45 | return $object->$getter(); |
|
46 | } |
|
47 | } |
|
48 |
@@ 9-47 (lines=39) @@ | ||
6 | use Innmind\Reflection\Exception\LogicException; |
|
7 | use Innmind\Immutable\StringPrimitive; |
|
8 | ||
9 | class HasserStrategy implements ExtractionStrategyInterface |
|
10 | { |
|
11 | private $hasser; |
|
12 | ||
13 | public function __construct() |
|
14 | { |
|
15 | $this->hasser = new StringPrimitive('has%s'); |
|
16 | } |
|
17 | ||
18 | /** |
|
19 | * {@inheritdoc} |
|
20 | */ |
|
21 | public function supports($object, string $property): bool |
|
22 | { |
|
23 | $refl = new \ReflectionObject($object); |
|
24 | $hasser = (string) $this->hasser->sprintf( |
|
25 | (string) (new StringPrimitive($property))->camelize() |
|
26 | ); |
|
27 | ||
28 | return $refl->hasMethod($hasser) && |
|
29 | $refl->getMethod($hasser)->getNumberOfRequiredParameters() === 0; |
|
30 | } |
|
31 | ||
32 | /** |
|
33 | * {@inheritdoc} |
|
34 | */ |
|
35 | public function extract($object, string $property) |
|
36 | { |
|
37 | if (!$this->supports($object, $property)) { |
|
38 | throw new LogicException; |
|
39 | } |
|
40 | ||
41 | $hasser = (string) $this->hasser->sprintf( |
|
42 | (string) (new StringPrimitive($property))->camelize() |
|
43 | ); |
|
44 | ||
45 | return $object->$hasser(); |
|
46 | } |
|
47 | } |
|
48 |
@@ 9-47 (lines=39) @@ | ||
6 | use Innmind\Reflection\Exception\LogicException; |
|
7 | use Innmind\Immutable\StringPrimitive; |
|
8 | ||
9 | class IsserStrategy implements ExtractionStrategyInterface |
|
10 | { |
|
11 | private $isser; |
|
12 | ||
13 | public function __construct() |
|
14 | { |
|
15 | $this->isser = new StringPrimitive('is%s'); |
|
16 | } |
|
17 | ||
18 | /** |
|
19 | * {@inheritdoc} |
|
20 | */ |
|
21 | public function supports($object, string $property): bool |
|
22 | { |
|
23 | $refl = new \ReflectionObject($object); |
|
24 | $isser = (string) $this->isser->sprintf( |
|
25 | (string) (new StringPrimitive($property))->camelize() |
|
26 | ); |
|
27 | ||
28 | return $refl->hasMethod($isser) && |
|
29 | $refl->getMethod($isser)->getNumberOfRequiredParameters() === 0; |
|
30 | } |
|
31 | ||
32 | /** |
|
33 | * {@inheritdoc} |
|
34 | */ |
|
35 | public function extract($object, string $property) |
|
36 | { |
|
37 | if (!$this->supports($object, $property)) { |
|
38 | throw new LogicException; |
|
39 | } |
|
40 | ||
41 | $isser = (string) $this->isser->sprintf( |
|
42 | (string) (new StringPrimitive($property))->camelize() |
|
43 | ); |
|
44 | ||
45 | return $object->$isser(); |
|
46 | } |
|
47 | } |
|
48 |