1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace iMega\Formatter; |
4
|
|
|
|
5
|
|
|
class Formatter |
6
|
|
|
{ |
7
|
|
|
protected $fields = []; |
8
|
|
|
|
9
|
28 |
|
public function __construct(array $meta) |
10
|
|
|
{ |
11
|
28 |
|
foreach ($meta as $item) { |
12
|
28 |
|
if (empty($item) || !is_array($item)) { |
13
|
4 |
|
throw new FormatterException('Wrong type'); |
14
|
|
|
} |
15
|
24 |
|
list($name, $default, $type, $error) = $item; |
16
|
24 |
|
if (0 !== $error) { |
17
|
4 |
|
throw new FormatterException('Wrong field name'); |
18
|
|
|
} |
19
|
20 |
|
if (get_parent_class($type) == GenericType::class) { |
20
|
20 |
|
$this->fields[$name] = [$default, $type]; |
21
|
20 |
|
} else { |
22
|
|
|
throw new FormatterException('Wrong instance'); |
23
|
|
|
} |
24
|
20 |
|
} |
25
|
20 |
|
} |
26
|
|
|
|
27
|
10 |
|
public function getData($name, $value) |
28
|
|
|
{ |
29
|
10 |
|
$type = $this->getType($name); |
30
|
|
|
try { |
31
|
10 |
|
return $type::getData($value); |
32
|
2 |
|
} catch (FormatterException $e) { |
33
|
2 |
|
return $type::getData($this->getDefaultValue($name)); |
34
|
|
|
} |
35
|
|
|
} |
36
|
|
|
|
37
|
8 |
|
public function getValue($name, $value) |
38
|
|
|
{ |
39
|
8 |
|
$type = $this->getType($name); |
40
|
|
|
try { |
41
|
8 |
|
return $type::getValue($value); |
42
|
2 |
|
} catch (FormatterException $e) { |
43
|
2 |
|
return $this->getDefaultValue($name); |
44
|
|
|
} |
45
|
|
|
} |
46
|
|
|
|
47
|
2 |
|
public function getDataCollection(array $values) |
48
|
|
|
{ |
49
|
2 |
|
$ret = []; |
50
|
2 |
|
foreach ($this->getFileds() as $name => $v) { |
51
|
2 |
|
if (array_key_exists($name, $values)) { |
52
|
2 |
|
$ret[$name] = $this->getData($name, $values[$name]); |
53
|
2 |
|
} else { |
54
|
2 |
|
$type = $this->getType($name); |
55
|
2 |
|
$ret[$name] = $type::getData($this->getDefaultValue($name)); |
56
|
|
|
} |
57
|
2 |
|
} |
58
|
|
|
|
59
|
2 |
|
return $ret; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
public function getValueCollection(array $values) |
63
|
|
|
{ |
64
|
|
|
$ret = []; |
65
|
|
|
foreach ($this->getFileds() as $name => $v) { |
66
|
|
|
if (array_key_exists($name, $values)) { |
67
|
|
|
$ret[$name] = $this->getValue($name, $values[$name]); |
68
|
|
|
} else { |
69
|
|
|
$ret[$name] = $this->getDefaultValue($name); |
70
|
|
|
} |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
return $ret; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* @return array |
78
|
|
|
*/ |
79
|
2 |
|
public function getFileds() |
80
|
|
|
{ |
81
|
2 |
|
return $this->fields; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* @param string $name |
86
|
|
|
* |
87
|
|
|
* @return GenericType |
88
|
|
|
*/ |
89
|
18 |
|
protected function getField($name) |
90
|
|
|
{ |
91
|
18 |
|
if (array_key_exists($name, $this->fields)) { |
92
|
18 |
|
return $this->fields[$name]; |
93
|
|
|
} else { |
94
|
|
|
throw new FormatterException('Field not exists'); |
95
|
|
|
} |
96
|
|
|
} |
97
|
|
|
|
98
|
6 |
|
protected function getDefaultValue($name) |
99
|
|
|
{ |
100
|
6 |
|
list($default, $type) = $this->getField($name); |
|
|
|
|
101
|
|
|
|
102
|
6 |
|
return $default; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* @param $name |
107
|
|
|
* |
108
|
|
|
* @return \iMega\Formatter\Typer |
109
|
|
|
*/ |
110
|
18 |
|
protected function getType($name) |
111
|
|
|
{ |
112
|
18 |
|
list($default, $type) = $this->getField($name); |
|
|
|
|
113
|
|
|
|
114
|
18 |
|
return $type; |
115
|
|
|
} |
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.