1 | <?php |
||
5 | class Formatter |
||
6 | { |
||
7 | protected $fields = []; |
||
8 | |||
9 | 34 | public function __construct(array $meta) |
|
10 | { |
||
11 | 34 | foreach ($meta as $item) { |
|
12 | 34 | if (empty($item) || !is_array($item)) { |
|
13 | 4 | throw new FormatterException('Wrong type'); |
|
14 | } |
||
15 | 30 | list($name, $default, $type, $error) = $item; |
|
16 | 30 | if (0 !== $error) { |
|
17 | 4 | throw new FormatterException('Wrong field name'); |
|
18 | } |
||
19 | 26 | if (get_parent_class($type) == GenericType::class) { |
|
20 | 24 | $this->fields[$name] = [$default, $type]; |
|
21 | 24 | } else { |
|
22 | 2 | throw new FormatterException('Wrong instance'); |
|
23 | } |
||
24 | 24 | } |
|
25 | 24 | } |
|
26 | |||
27 | 12 | public function getData($name, $value) |
|
36 | |||
37 | 10 | public function getValue($name, $value) |
|
46 | |||
47 | 2 | public function getDataCollection(array $values) |
|
61 | |||
62 | 2 | public function getValueCollection(array $values) |
|
63 | { |
||
64 | 2 | $ret = []; |
|
65 | 2 | foreach ($this->getFileds() as $name) { |
|
66 | 2 | if (array_key_exists($name, $values)) { |
|
67 | 2 | $ret[$name] = $this->getValue($name, $values[$name]); |
|
68 | 2 | } else { |
|
69 | 2 | $ret[$name] = $this->getDefaultValue($name); |
|
70 | } |
||
71 | 2 | } |
|
72 | |||
73 | 2 | return $ret; |
|
74 | } |
||
75 | |||
76 | /** |
||
77 | * @return array |
||
78 | */ |
||
79 | 4 | public function getFileds() |
|
83 | |||
84 | /** |
||
85 | * @param string $name |
||
86 | * |
||
87 | * @return GenericType |
||
88 | */ |
||
89 | 22 | protected function getField($name) |
|
90 | { |
||
91 | 22 | if (array_key_exists($name, $this->fields)) { |
|
92 | 20 | return $this->fields[$name]; |
|
93 | } else { |
||
94 | 2 | throw new FormatterException('Field not exists'); |
|
95 | } |
||
96 | } |
||
97 | |||
98 | 8 | protected function getDefaultValue($name) |
|
104 | |||
105 | /** |
||
106 | * @param $name |
||
107 | * |
||
108 | * @return \iMega\Formatter\Typer |
||
109 | */ |
||
110 | 22 | protected function getType($name) |
|
116 | } |
||
117 |
This checks looks for assignemnts to variables using the
list(...)
function, where not all assigned variables are subsequently used.Consider the following code example.
Only the variables
$a
and$c
are used. There was no need to assign$b
.Instead, the list call could have been.