1
|
|
|
<?php |
2
|
|
|
declare(strict_types = 1); |
3
|
|
|
|
4
|
|
|
namespace Innmind\Reflection\ExtractionStrategy; |
5
|
|
|
|
6
|
|
|
use Innmind\Reflection\{ |
7
|
|
|
ExtractionStrategyInterface, |
8
|
|
|
Exception\InvalidArgumentException, |
9
|
|
|
Exception\PropertyCannotBeExtractedException |
10
|
|
|
}; |
11
|
|
|
use Innmind\Immutable\{ |
12
|
|
|
StreamInterface, |
13
|
|
|
Map |
14
|
|
|
}; |
15
|
|
|
|
16
|
|
|
final class DelegationStrategy implements ExtractionStrategyInterface |
17
|
|
|
{ |
18
|
|
|
private $strategies; |
19
|
|
|
private $cache; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @param StreamInterface<ExtractionStrategyInterface> $strategies |
|
|
|
|
23
|
|
|
*/ |
24
|
7 |
View Code Duplication |
public function __construct(StreamInterface $strategies) |
|
|
|
|
25
|
|
|
{ |
26
|
7 |
|
if ((string) $strategies->type() !== ExtractionStrategyInterface::class) { |
27
|
|
|
throw new InvalidArgumentException; |
28
|
|
|
} |
29
|
|
|
|
30
|
7 |
|
$this->strategies = $strategies; |
31
|
7 |
|
$this->cache = new Map('string', ExtractionStrategyInterface::class); |
32
|
7 |
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* {@inheritdoc} |
36
|
|
|
*/ |
37
|
1 |
|
public function supports($object, string $property): bool |
38
|
|
|
{ |
39
|
|
|
return $this |
40
|
1 |
|
->strategies |
41
|
1 |
|
->reduce( |
42
|
1 |
|
false, |
43
|
|
|
function(bool $supports, ExtractionStrategyInterface $strategy) use ($object, $property): bool { |
44
|
1 |
|
if ($supports === true) { |
45
|
1 |
|
return true; |
46
|
|
|
} |
47
|
|
|
|
48
|
1 |
|
return $strategy->supports($object, $property); |
49
|
1 |
|
} |
50
|
|
|
); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* {@inheritdoc} |
55
|
|
|
*/ |
56
|
6 |
|
public function extract($object, string $property) |
57
|
|
|
{ |
58
|
6 |
|
$key = $this->generateKey($object, $property); |
59
|
|
|
|
60
|
6 |
|
if ($this->cache->contains($key)) { |
|
|
|
|
61
|
|
|
return $this |
62
|
1 |
|
->cache |
63
|
1 |
|
->get($key) |
|
|
|
|
64
|
1 |
|
->extract($object, $property); |
65
|
|
|
} |
66
|
|
|
|
67
|
6 |
|
$strategy = $this->strategies->reduce( |
68
|
6 |
|
null, |
69
|
6 |
|
function($target, ExtractionStrategyInterface $strategy) use ($object, $property) { |
70
|
6 |
|
if ($target instanceof ExtractionStrategyInterface) { |
71
|
3 |
|
return $target; |
72
|
|
|
} |
73
|
|
|
|
74
|
6 |
|
if ($strategy->supports($object, $property)) { |
75
|
4 |
|
return $strategy; |
76
|
|
|
} |
77
|
6 |
|
} |
78
|
|
|
); |
79
|
|
|
|
80
|
6 |
|
if (!$strategy instanceof ExtractionStrategyInterface) { |
81
|
2 |
|
throw new PropertyCannotBeExtractedException($property); |
82
|
|
|
} |
83
|
|
|
|
84
|
4 |
|
$this->cache = $this->cache->put($key, $strategy); |
|
|
|
|
85
|
|
|
|
86
|
4 |
|
return $strategy->extract($object, $property); |
87
|
|
|
} |
88
|
|
|
|
89
|
6 |
|
private function generateKey($object, string $property): string |
90
|
|
|
{ |
91
|
6 |
|
return get_class($object).'::'.$property; |
92
|
|
|
} |
93
|
|
|
} |
94
|
|
|
|
This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.