RulesGeneratorList::normalizeValue()   A
last analyzed

Complexity

Conditions 5
Paths 6

Size

Total Lines 19
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 5

Importance

Changes 0
Metric Value
eloc 8
dl 0
loc 19
ccs 9
cts 9
cp 1
rs 9.6111
c 0
b 0
f 0
cc 5
nc 6
nop 1
crap 5
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
}