|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Zerg\Field; |
|
4
|
|
|
|
|
5
|
|
|
use Zerg\DataSet; |
|
6
|
|
|
use Zerg\Stream\AbstractStream; |
|
7
|
|
|
|
|
8
|
|
|
/** |
|
9
|
|
|
* Class Collection compose other types of fields. |
|
10
|
|
|
* |
|
11
|
|
|
* This field return array of values, that are read from Stream by other types of fields. |
|
12
|
|
|
* |
|
13
|
|
|
* @since 0.1 |
|
14
|
|
|
* @package Zerg\Field |
|
15
|
|
|
*/ |
|
16
|
|
|
class Collection extends AbstractField implements \ArrayAccess, \Iterator |
|
17
|
|
|
{ |
|
18
|
|
|
/** |
|
19
|
|
|
* @var AbstractField[] List of children fields. |
|
20
|
|
|
*/ |
|
21
|
|
|
protected $fields; |
|
22
|
|
|
|
|
23
|
8 |
|
public function __construct($schema, $options = []) |
|
24
|
|
|
{ |
|
25
|
8 |
|
$this->initFromArray($schema); |
|
26
|
6 |
|
$this->configure($options); |
|
27
|
6 |
|
} |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* Add a new child node to field list. |
|
31
|
|
|
* |
|
32
|
|
|
* @param string $name The field name. |
|
33
|
|
|
* @param AbstractField $child Field instance. |
|
34
|
|
|
*/ |
|
35
|
5 |
|
public function addField($name, AbstractField $child) |
|
36
|
|
|
{ |
|
37
|
5 |
|
$this->fields[$name] = $child; |
|
38
|
5 |
|
} |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* Recursively call parse method of all children and store values in associated DataSet. |
|
42
|
|
|
* |
|
43
|
|
|
* @api |
|
44
|
|
|
* @param AbstractStream $stream Stream from which children read. |
|
45
|
|
|
* @return array Array of parsed values. |
|
46
|
|
|
*/ |
|
47
|
6 |
|
public function parse(AbstractStream $stream) |
|
48
|
|
|
{ |
|
49
|
6 |
|
if (!($this->dataSet instanceof DataSet)) { |
|
50
|
4 |
|
$this->dataSet = new DataSet; |
|
51
|
4 |
|
} |
|
52
|
|
|
|
|
53
|
6 |
|
$this->rewind(); |
|
54
|
|
|
do { |
|
55
|
6 |
|
$field = $this->current(); |
|
56
|
6 |
|
$field->setDataSet($this->getDataSet()); |
|
57
|
6 |
|
if ($field instanceof Conditional) { |
|
58
|
1 |
|
$field = $field->resolveField(); |
|
59
|
1 |
|
} |
|
60
|
6 |
|
if ($field instanceof self) { |
|
61
|
1 |
|
$this->dataSet->push($this->key()); |
|
62
|
1 |
|
$field->parse($stream); |
|
63
|
1 |
|
$this->dataSet->pop(); |
|
64
|
1 |
|
} else { |
|
65
|
6 |
|
$this->dataSet->setValue($this->key(), $field->parse($stream)); |
|
66
|
|
|
} |
|
67
|
6 |
|
$this->next(); |
|
68
|
6 |
|
} while ($this->valid()); |
|
69
|
|
|
|
|
70
|
4 |
|
if (isset($this->assert)) { |
|
71
|
1 |
|
$this->validate($this->dataSet->getValueByCurrentPath()); |
|
72
|
1 |
|
} |
|
73
|
|
|
|
|
74
|
4 |
|
return $this->dataSet->getData(); |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* Recursively creates field instances form their declarations. |
|
79
|
|
|
* |
|
80
|
|
|
* @param array $fieldArray Array of declarations. |
|
81
|
|
|
* @throws ConfigurationException If one of declarations are invalid. |
|
82
|
|
|
*/ |
|
83
|
7 |
|
private function initFromArray(array $fieldArray = []) |
|
84
|
|
|
{ |
|
85
|
7 |
|
foreach ($fieldArray as $fieldName => $fieldParams) { |
|
86
|
6 |
|
$this->addField($fieldName, Factory::get($fieldParams)); |
|
87
|
6 |
|
} |
|
88
|
6 |
|
} |
|
89
|
|
|
|
|
90
|
|
|
/** |
|
91
|
|
|
* @inheritdoc |
|
92
|
|
|
*/ |
|
93
|
1 |
|
public function offsetExists($offset) |
|
94
|
|
|
{ |
|
95
|
1 |
|
return isset($this->fields[$offset]); |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
/** |
|
99
|
|
|
* @inheritdoc |
|
100
|
|
|
*/ |
|
101
|
2 |
|
public function offsetGet($offset) |
|
102
|
|
|
{ |
|
103
|
2 |
|
return $this->fields[$offset]; |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
/** |
|
107
|
|
|
* @inheritdoc |
|
108
|
|
|
*/ |
|
109
|
1 |
|
public function offsetSet($offset, $value) |
|
110
|
|
|
{ |
|
111
|
1 |
|
$this->fields[$offset] = $value; |
|
112
|
1 |
|
} |
|
113
|
|
|
|
|
114
|
|
|
/** |
|
115
|
|
|
* @inheritdoc |
|
116
|
|
|
*/ |
|
117
|
1 |
|
public function offsetUnset($offset) |
|
118
|
|
|
{ |
|
119
|
1 |
|
unset($this->fields[$offset]); |
|
120
|
1 |
|
} |
|
121
|
|
|
|
|
122
|
|
|
/** |
|
123
|
|
|
* @inheritdoc |
|
124
|
|
|
* @return AbstractField |
|
125
|
|
|
*/ |
|
126
|
3 |
|
public function current() |
|
127
|
|
|
{ |
|
128
|
3 |
|
return current($this->fields); |
|
129
|
|
|
} |
|
130
|
|
|
|
|
131
|
|
|
/** |
|
132
|
|
|
* @inheritdoc |
|
133
|
|
|
*/ |
|
134
|
3 |
|
public function next() |
|
135
|
|
|
{ |
|
136
|
3 |
|
next($this->fields); |
|
137
|
3 |
|
} |
|
138
|
|
|
|
|
139
|
|
|
/** |
|
140
|
|
|
* @inheritdoc |
|
141
|
|
|
*/ |
|
142
|
3 |
|
public function key() |
|
143
|
|
|
{ |
|
144
|
3 |
|
return key($this->fields); |
|
145
|
|
|
} |
|
146
|
|
|
|
|
147
|
|
|
/** |
|
148
|
|
|
* @inheritdoc |
|
149
|
|
|
*/ |
|
150
|
3 |
|
public function valid() |
|
151
|
|
|
{ |
|
152
|
3 |
|
return isset($this->fields[$this->key()]); |
|
153
|
|
|
} |
|
154
|
|
|
|
|
155
|
|
|
/** |
|
156
|
|
|
* @inheritdoc |
|
157
|
|
|
*/ |
|
158
|
3 |
|
public function rewind() |
|
159
|
|
|
{ |
|
160
|
3 |
|
reset($this->fields); |
|
161
|
|
|
} |
|
162
|
|
|
} |