@@ 9-45 (lines=37) @@ | ||
6 | use Innmind\Reflection\ExtractionStrategyInterface; |
|
7 | use Innmind\Reflection\Exception\LogicException; |
|
8 | ||
9 | class ReflectionStrategy 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->hasProperty($property); |
|
19 | } |
|
20 | ||
21 | /** |
|
22 | * {@inheritdoc} |
|
23 | */ |
|
24 | public function extract($object, string $property) |
|
25 | { |
|
26 | if (!$this->supports($object, $property)) { |
|
27 | throw new LogicException; |
|
28 | } |
|
29 | ||
30 | $refl = new \ReflectionObject($object); |
|
31 | $refl = $refl->getProperty($property); |
|
32 | ||
33 | if (!$refl->isPublic()) { |
|
34 | $refl->setAccessible(true); |
|
35 | } |
|
36 | ||
37 | $value = $refl->getValue($object); |
|
38 | ||
39 | if (!$refl->isPublic()) { |
|
40 | $refl->setAccessible(false); |
|
41 | } |
|
42 | ||
43 | return $value; |
|
44 | } |
|
45 | } |
|
46 |
@@ 9-43 (lines=35) @@ | ||
6 | use Innmind\Reflection\InjectionStrategyInterface; |
|
7 | use Innmind\Reflection\Exception\LogicException; |
|
8 | ||
9 | class ReflectionStrategy implements InjectionStrategyInterface |
|
10 | { |
|
11 | /** |
|
12 | * {@inheritdoc} |
|
13 | */ |
|
14 | public function supports($object, string $property, $value): bool |
|
15 | { |
|
16 | $refl = new \ReflectionObject($object); |
|
17 | ||
18 | return $refl->hasProperty($property); |
|
19 | } |
|
20 | ||
21 | /** |
|
22 | * {@inheritdoc} |
|
23 | */ |
|
24 | public function inject($object, string $property, $value) |
|
25 | { |
|
26 | if (!$this->supports($object, $property, $value)) { |
|
27 | throw new LogicException; |
|
28 | } |
|
29 | ||
30 | $refl = new \ReflectionObject($object); |
|
31 | $refl = $refl->getProperty($property); |
|
32 | ||
33 | if (!$refl->isPublic()) { |
|
34 | $refl->setAccessible(true); |
|
35 | } |
|
36 | ||
37 | $refl->setValue($object, $value); |
|
38 | ||
39 | if (!$refl->isPublic()) { |
|
40 | $refl->setAccessible(false); |
|
41 | } |
|
42 | } |
|
43 | } |
|
44 |