|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Dice\Extra; |
|
4
|
|
|
|
|
5
|
|
|
class RuleValidator |
|
6
|
|
|
{ |
|
7
|
|
|
private $dice; |
|
8
|
|
|
|
|
9
|
|
|
public function __construct(\Dice\Dice $dice) |
|
10
|
|
|
{ |
|
11
|
|
|
$this->dice = $dice; |
|
12
|
|
|
} |
|
13
|
|
|
|
|
14
|
|
|
public function addRule($name, array $rule) |
|
15
|
|
|
{ |
|
16
|
|
|
$this->checkValidKeys($rule); |
|
17
|
|
|
$this->checkBoolean($rule, 'inherit'); |
|
18
|
|
|
$this->checkBoolean($rule, 'shared'); |
|
19
|
|
|
$this->checkNumericArray($rule, 'constructParams'); |
|
20
|
|
|
$this->checkNumericArray($rule, 'shareInstances'); |
|
21
|
|
|
$this->checkNumericArray($rule, 'call'); |
|
22
|
|
|
$this->checkAssocArray($rule, 'call'); |
|
23
|
|
|
|
|
24
|
|
|
$this->dice->addRule($name, $rule); |
|
25
|
|
|
} |
|
26
|
|
|
|
|
27
|
|
|
public function create($name, array $args = [], array $share = []) |
|
28
|
|
|
{ |
|
29
|
|
|
return $this->dice->create($name, $args, $share); |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
|
View Code Duplication |
public function checkAssocArray($rule, $key) |
|
|
|
|
|
|
33
|
|
|
{ |
|
34
|
|
|
if (!isset($rule[$key])) { |
|
35
|
|
|
return; |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
if (count(array_filter(array_keys($rule[$key]), 'is_string')) === 0) { |
|
39
|
|
|
throw new \InvalidArgumentException('Rule option ' . $key . ' must be a an associative array'); |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
public function checkBoolean($rule, $key) |
|
45
|
|
|
{ |
|
46
|
|
|
if (!isset($rule[$key])) { |
|
47
|
|
|
return; |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
if (!is_bool($rule[$key])) { |
|
51
|
|
|
throw new \InvalidArgumentException('Rule option ' . $key . ' must be true or false'); |
|
52
|
|
|
} |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
View Code Duplication |
public function checkNumericArray($rule, $key) |
|
|
|
|
|
|
56
|
|
|
{ |
|
57
|
|
|
if (!isset($rule[$key])) { |
|
58
|
|
|
return; |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
if (count(array_filter(array_keys($rule[$key]), 'is_string')) > 0) { |
|
62
|
|
|
throw new \InvalidArgumentException('Rule option ' . $key |
|
63
|
|
|
. ' must be a sequential array, not an associative array'); |
|
64
|
|
|
} |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
private function checkValidKeys($rule) |
|
68
|
|
|
{ |
|
69
|
|
|
$validKeys = ['call', 'shared', 'substitutions', 'instanceOf', 'inherit', 'shareInstances', 'constructParams']; |
|
70
|
|
|
|
|
71
|
|
|
foreach ($rule as $name => $value) { |
|
72
|
|
|
if (!in_array($name, $validKeys)) { |
|
73
|
|
|
throw new \InvalidArgumentException('Invalid rule option: ' . $name); |
|
74
|
|
|
} |
|
75
|
|
|
} |
|
76
|
|
|
} |
|
77
|
|
|
} |
|
78
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.