1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace TreeHouse\Model\Config; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* Class containing configuration for models. |
7
|
|
|
*/ |
8
|
|
|
class Config |
9
|
|
|
{ |
10
|
|
|
/** |
11
|
|
|
* @var array |
12
|
|
|
*/ |
13
|
|
|
protected $config; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* @var array |
17
|
|
|
*/ |
18
|
|
|
protected $multiValued; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @param array $config |
22
|
|
|
* @param array $multiValued |
23
|
|
|
*/ |
24
|
18 |
|
public function __construct(array $config, array $multiValued = []) |
25
|
|
|
{ |
26
|
18 |
|
$this->config = $config; |
27
|
18 |
|
$this->multiValued = $multiValued; |
28
|
18 |
|
} |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Returns the complete configuration. |
32
|
|
|
* |
33
|
|
|
* @return array |
34
|
|
|
*/ |
35
|
18 |
|
public function getConfig() |
36
|
|
|
{ |
37
|
18 |
|
return $this->config; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Returns the multiValued state of fields. |
42
|
|
|
* |
43
|
|
|
* @return array |
44
|
|
|
*/ |
45
|
|
|
public function getMultiValued() |
46
|
|
|
{ |
47
|
|
|
return $this->multiValued; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Checks if a field is multi-valued |
52
|
|
|
* |
53
|
|
|
* @param string $name |
54
|
|
|
* |
55
|
|
|
* @return boolean |
56
|
|
|
*/ |
57
|
14 |
|
public function isMultiValued($name) |
58
|
|
|
{ |
59
|
14 |
|
return $this->multiValued[$name]; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Checks if a configuration exists for a given field |
64
|
|
|
* |
65
|
|
|
* @param string $name |
66
|
|
|
* |
67
|
|
|
* @return boolean |
68
|
|
|
*/ |
69
|
18 |
|
public function hasFieldConfig($name) |
70
|
|
|
{ |
71
|
18 |
|
return array_key_exists($name, $this->getConfig()); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Returns the configuration for a field, if it exists |
76
|
|
|
* |
77
|
|
|
* @param string $name |
78
|
|
|
* |
79
|
|
|
* @return array|null |
80
|
|
|
*/ |
81
|
10 |
|
public function getFieldConfig($name) |
82
|
|
|
{ |
83
|
10 |
|
$config = $this->getConfig(); |
84
|
|
|
|
85
|
10 |
|
return array_key_exists($name, $config) ? $config[$name] : null; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* Checks if an key exists for a given field config |
90
|
|
|
* |
91
|
|
|
* @param string $config |
92
|
|
|
* @param integer $key |
93
|
|
|
* |
94
|
|
|
* @return boolean |
95
|
|
|
*/ |
96
|
10 |
View Code Duplication |
public function hasFieldConfigKey($config, $key) |
|
|
|
|
97
|
|
|
{ |
98
|
10 |
|
if (null === $values = $this->getFieldConfig($config)) { |
99
|
|
|
return false; |
100
|
|
|
} |
101
|
|
|
|
102
|
10 |
|
return array_key_exists($key, $values); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* Checks if a name-value exists for a given field config |
107
|
|
|
* |
108
|
|
|
* @param string $field |
109
|
|
|
* @param string $value |
110
|
|
|
* |
111
|
|
|
* @return boolean |
112
|
|
|
*/ |
113
|
|
View Code Duplication |
public function hasFieldConfigValue($field, $value) |
|
|
|
|
114
|
|
|
{ |
115
|
|
|
if (null === $values = $this->getFieldConfig($field)) { |
116
|
|
|
return false; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
return false !== array_search($value, $values); |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* Returns the name for a field config value |
124
|
|
|
* |
125
|
|
|
* @param string $config |
126
|
|
|
* @param integer $key |
127
|
|
|
* |
128
|
|
|
* @return string|null |
129
|
|
|
*/ |
130
|
|
View Code Duplication |
public function getFieldConfigValueByKey($config, $key) |
|
|
|
|
131
|
|
|
{ |
132
|
|
|
if (null === $values = $this->getFieldConfig($config)) { |
133
|
|
|
return null; |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
return array_key_exists($key, $values) ? $values[$key] : null; |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* Returns the key for a field config name-value |
141
|
|
|
* |
142
|
|
|
* @param string $config |
143
|
|
|
* @param string $value |
144
|
|
|
* |
145
|
|
|
* @return integer|null |
146
|
|
|
*/ |
147
|
|
View Code Duplication |
public function getFieldConfigKey($config, $value) |
|
|
|
|
148
|
|
|
{ |
149
|
|
|
if (null === $values = $this->getFieldConfig($config)) { |
150
|
|
|
return null; |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
return array_search($value, $values); |
154
|
|
|
} |
155
|
|
|
} |
156
|
|
|
|
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.