1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Xsolve\SalesforceClient\QueryBuilder\Visitor\Parameters; |
4
|
|
|
|
5
|
|
|
class ReplacingStrategyCollection implements \ArrayAccess |
6
|
|
|
{ |
7
|
|
|
/** |
8
|
|
|
* @var ReplacingStrategyInterface |
9
|
|
|
*/ |
10
|
|
|
protected $defaultStrategy; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* @var ReplacingStrategyInterface[] |
14
|
|
|
*/ |
15
|
|
|
protected $strategies; |
16
|
|
|
|
17
|
38 |
|
public function __construct() |
18
|
|
|
{ |
19
|
38 |
|
$this->strategies = [ |
20
|
38 |
|
new StringReplacingStrategy(), |
21
|
38 |
|
new IntReplacingStrategy(), |
22
|
38 |
|
new DateTimeReplacingStrategy(), |
23
|
38 |
|
new FloatReplacingStrategy(), |
24
|
38 |
|
new BoolReplacingStrategy(), |
25
|
|
|
]; |
26
|
38 |
|
$this->defaultStrategy = new StringReplacingStrategy(); |
27
|
38 |
|
} |
28
|
|
|
|
29
|
11 |
|
public function offsetExists($offset): bool |
30
|
|
|
{ |
31
|
11 |
|
if (null === $offset) { |
32
|
1 |
|
return true; // default will be used |
33
|
|
|
} |
34
|
|
|
|
35
|
11 |
|
return $offset instanceof Type && $this->findApplicableStrategy($offset) instanceof ReplacingStrategyInterface; |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @param Type|null $offset |
40
|
|
|
* |
41
|
|
|
* @return ReplacingStrategyInterface|null |
42
|
|
|
*/ |
43
|
32 |
|
public function offsetGet($offset) |
44
|
|
|
{ |
45
|
32 |
|
if (null === $offset) { |
46
|
22 |
|
return $this->defaultStrategy; |
47
|
|
|
} |
48
|
|
|
|
49
|
11 |
|
return $this->findApplicableStrategy($offset); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @param mixed $offset not used here |
54
|
|
|
* @param ReplacingStrategyInterface $value |
55
|
|
|
* |
56
|
|
|
* @throws \InvalidArgumentException if $value is not an instance of ReplacingStrategyInterface |
57
|
|
|
*/ |
58
|
5 |
|
public function offsetSet($offset, $value) |
59
|
|
|
{ |
60
|
5 |
|
if (!$value instanceof ReplacingStrategyInterface) { |
|
|
|
|
61
|
|
|
throw new \InvalidArgumentException( |
62
|
|
|
sprintf('Only instances of %s can be added to the collection', ReplacingStrategyInterface::class) |
63
|
|
|
); |
64
|
|
|
} |
65
|
|
|
|
66
|
5 |
|
foreach ($this->strategies as $strategy) { |
67
|
5 |
|
if (is_a($strategy, get_class($value))) { |
68
|
5 |
|
return; |
69
|
|
|
} |
70
|
|
|
} |
71
|
|
|
|
72
|
5 |
|
$this->strategies[] = $value; |
73
|
5 |
|
} |
74
|
|
|
|
75
|
5 |
|
public function offsetUnset($offset) |
76
|
|
|
{ |
77
|
5 |
|
if (!$offset instanceof Type) { |
78
|
|
|
return; |
79
|
|
|
} |
80
|
|
|
|
81
|
5 |
|
$strategy = $this->findApplicableStrategy($offset); |
82
|
|
|
|
83
|
5 |
|
if (!$strategy) { |
84
|
|
|
return; |
85
|
|
|
} |
86
|
|
|
|
87
|
5 |
|
$index = array_search($strategy, $this->strategies, true); |
88
|
5 |
|
unset($this->strategies[$index]); |
89
|
5 |
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* @param Type $type |
93
|
|
|
* |
94
|
|
|
* @return ReplacingStrategyInterface|null |
95
|
|
|
*/ |
96
|
16 |
|
private function findApplicableStrategy(Type $type) |
97
|
|
|
{ |
98
|
16 |
|
foreach ($this->strategies as $strategy) { |
99
|
16 |
|
if ($strategy->isApplicable($type)) { |
100
|
16 |
|
return $strategy; |
101
|
|
|
} |
102
|
|
|
} |
103
|
5 |
|
} |
104
|
|
|
} |
105
|
|
|
|