|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Innmind\Reflection\ExtractionStrategy; |
|
4
|
|
|
|
|
5
|
|
|
use Innmind\Reflection\{ |
|
6
|
|
|
Cache\StrategyCachingCapabilities, |
|
7
|
|
|
Exception\LogicException, |
|
8
|
|
|
Exception\InvalidArgumentException |
|
9
|
|
|
}; |
|
10
|
|
|
use Innmind\Immutable\{ |
|
11
|
|
|
TypedCollection, |
|
12
|
|
|
TypedCollectionInterface |
|
13
|
|
|
}; |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* DefaultExtractionStrategies |
|
17
|
|
|
* |
|
18
|
|
|
* @author Hugues Maignol <[email protected]> |
|
19
|
|
|
*/ |
|
20
|
|
|
final class ExtractionStrategies implements ExtractionStrategiesInterface |
|
21
|
|
|
{ |
|
22
|
|
|
use StrategyCachingCapabilities; |
|
23
|
|
|
|
|
24
|
|
|
private $strategies; |
|
25
|
|
|
|
|
26
|
32 |
View Code Duplication |
public function __construct(TypedCollectionInterface $strategies = null) |
|
27
|
|
|
{ |
|
28
|
32 |
|
$this->strategies = $strategies ?? $this->all(); |
|
29
|
|
|
|
|
30
|
32 |
|
if ($this->strategies->getType() !== ExtractionStrategyInterface::class) { |
|
31
|
2 |
|
throw new InvalidArgumentException; |
|
32
|
|
|
} |
|
33
|
30 |
|
} |
|
34
|
|
|
|
|
35
|
30 |
|
public function all(): TypedCollectionInterface |
|
36
|
|
|
{ |
|
37
|
30 |
|
if ($this->strategies === null) { |
|
38
|
28 |
|
return $this->strategies = new TypedCollection( |
|
|
|
|
|
|
39
|
28 |
|
ExtractionStrategyInterface::class, |
|
40
|
|
|
[ |
|
41
|
28 |
|
new GetterStrategy, |
|
42
|
28 |
|
new NamedMethodStrategy, |
|
43
|
28 |
|
new IsserStrategy, |
|
44
|
28 |
|
new HasserStrategy, |
|
45
|
28 |
|
new ReflectionStrategy, |
|
46
|
|
|
] |
|
47
|
|
|
); |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
12 |
|
return $this->strategies; |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
6 |
View Code Duplication |
public function get($object, string $key): ExtractionStrategyInterface |
|
|
|
|
|
|
54
|
|
|
{ |
|
55
|
6 |
|
$strategy = $this->getCachedStrategy(get_class($object), $key); |
|
56
|
6 |
|
if (null !== $strategy) { |
|
57
|
2 |
|
return $strategy; |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
6 |
|
foreach ($this->all() as $strategy) { |
|
61
|
6 |
|
if ($strategy->supports($object, $key)) { |
|
62
|
|
|
|
|
63
|
4 |
|
$this->setCachedStrategy(get_class($object), $key, $strategy); |
|
64
|
|
|
|
|
65
|
6 |
|
return $strategy; |
|
66
|
|
|
} |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
2 |
|
throw new LogicException( |
|
70
|
|
|
sprintf( |
|
71
|
2 |
|
'Property "%s" cannot be extracted', |
|
72
|
|
|
$key |
|
73
|
|
|
) |
|
74
|
|
|
); |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
} |
|
78
|
|
|
|
This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.
The explanatory message should give you some clue as to whether and when the type will be removed from the class and what other constant to use instead.