|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace iMega\Formatter; |
|
4
|
|
|
|
|
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) |
|
28
|
|
|
{ |
|
29
|
12 |
|
$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
|
10 |
|
public function getValue($name, $value) |
|
38
|
|
|
{ |
|
39
|
10 |
|
$type = $this->getType($name); |
|
40
|
|
|
try { |
|
41
|
10 |
|
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) { |
|
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
|
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() |
|
80
|
|
|
{ |
|
81
|
4 |
|
return array_keys($this->fields); |
|
82
|
|
|
} |
|
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) |
|
99
|
|
|
{ |
|
100
|
8 |
|
list($default, $type) = $this->getField($name); |
|
|
|
|
|
|
101
|
|
|
|
|
102
|
8 |
|
return $default; |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
/** |
|
106
|
|
|
* @param $name |
|
107
|
|
|
* |
|
108
|
|
|
* @return \iMega\Formatter\Typer |
|
109
|
|
|
*/ |
|
110
|
22 |
|
protected function getType($name) |
|
111
|
|
|
{ |
|
112
|
22 |
|
list($default, $type) = $this->getField($name); |
|
|
|
|
|
|
113
|
|
|
|
|
114
|
20 |
|
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
$aand$care used. There was no need to assign$b.Instead, the list call could have been.