1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* @package s9e\TextFormatter |
5
|
|
|
* @copyright Copyright (c) The s9e authors |
6
|
|
|
* @license http://www.opensource.org/licenses/mit-license.php The MIT License |
7
|
|
|
*/ |
8
|
|
|
namespace s9e\TextFormatter\Configurator\Collections; |
9
|
|
|
|
10
|
|
|
use InvalidArgumentException; |
11
|
|
|
use s9e\TextFormatter\Configurator\RulesGenerators\Interfaces\BooleanRulesGenerator; |
12
|
|
|
use s9e\TextFormatter\Configurator\RulesGenerators\Interfaces\TargetedRulesGenerator; |
13
|
|
|
|
14
|
|
|
class RulesGeneratorList extends NormalizedList |
15
|
|
|
{ |
16
|
|
|
/** |
17
|
|
|
* Normalize the value to an object |
18
|
|
|
* |
19
|
|
|
* @param string|BooleanRulesGenerator|TargetedRulesGenerator $generator Either a string, or an instance of a rules generator |
20
|
|
|
* @return BooleanRulesGenerator|TargetedRulesGenerator |
21
|
|
|
*/ |
22
|
4 |
|
public function normalizeValue($generator) |
23
|
|
|
{ |
24
|
4 |
|
if (is_string($generator)) |
25
|
|
|
{ |
26
|
2 |
|
$className = 's9e\\TextFormatter\\Configurator\\RulesGenerators\\' . $generator; |
27
|
|
|
|
28
|
2 |
|
if (class_exists($className)) |
29
|
|
|
{ |
30
|
1 |
|
$generator = new $className; |
31
|
|
|
} |
32
|
|
|
} |
33
|
|
|
|
34
|
4 |
|
if (!($generator instanceof BooleanRulesGenerator) |
35
|
4 |
|
&& !($generator instanceof TargetedRulesGenerator)) |
36
|
|
|
{ |
37
|
1 |
|
throw new InvalidArgumentException('Invalid rules generator ' . var_export($generator, true)); |
38
|
|
|
} |
39
|
|
|
|
40
|
3 |
|
return $generator; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* {@inheritdoc} |
45
|
|
|
*/ |
46
|
|
|
public function remove($value) |
47
|
|
|
{ |
48
|
|
|
if (is_string($value)) |
49
|
|
|
{ |
50
|
|
|
// Select by class name to avoid costly object instantiations |
51
|
|
|
$className = get_class($this->normalizeValue($value)); |
52
|
|
|
|
53
|
|
|
$cnt = 0; |
54
|
|
|
foreach ($this->items as $i => $rulesGenerator) |
55
|
|
|
{ |
56
|
|
|
if ($rulesGenerator instanceof $className) |
57
|
|
|
{ |
58
|
|
|
++$cnt; |
59
|
|
|
unset($this->items[$i]); |
60
|
|
|
} |
61
|
|
|
} |
62
|
|
|
$this->items = array_values($this->items); |
63
|
|
|
|
64
|
|
|
return $cnt; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
return parent::remove($value); |
68
|
|
|
} |
69
|
|
|
} |