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