1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Zerg\Field; |
4
|
|
|
|
5
|
|
|
use Zerg\Stream\AbstractStream; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* Class Arr repeat given field needed times. |
9
|
|
|
* |
10
|
|
|
* This field return array of values, that are read from Stream by given field, |
11
|
|
|
* specified count of times, or until some condition are performed. |
12
|
|
|
* |
13
|
|
|
* @since 1.0 |
14
|
|
|
* @package Zerg\Field |
15
|
|
|
*/ |
16
|
|
|
class Arr extends Collection |
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* @var int Count of elements. |
20
|
|
|
*/ |
21
|
|
|
protected $count; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @var string|callable |
25
|
|
|
*/ |
26
|
|
|
protected $until; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @var array|AbstractField Field to be repeated. |
30
|
|
|
*/ |
31
|
|
|
protected $field; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @var int |
35
|
|
|
*/ |
36
|
|
|
protected $index; |
37
|
|
|
|
38
|
10 |
|
public function __construct($count, $field = [], $options = []) |
39
|
|
|
{ |
40
|
10 |
|
$this->index = 0; |
41
|
10 |
|
if (is_array($count)) { |
42
|
2 |
|
$this->configure($count); |
43
|
2 |
|
} else { |
44
|
9 |
|
$this->setCount($count); |
45
|
9 |
|
$this->setField($field); |
46
|
9 |
|
$this->configure($options); |
47
|
|
|
} |
48
|
10 |
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Call parse method on arrayed field needed times. |
52
|
|
|
* |
53
|
|
|
* @api |
54
|
|
|
* @param AbstractStream $stream Stream from which children read. |
55
|
|
|
* @return array Array of parsed values. |
56
|
|
|
* @since 1.0 |
57
|
|
|
*/ |
58
|
5 |
|
public function parse(AbstractStream $stream) |
59
|
|
|
{ |
60
|
|
|
try { |
61
|
5 |
|
return parent::parse($stream); |
62
|
2 |
|
} catch (\OutOfBoundsException $e) { |
63
|
2 |
|
if ($this->isUntilEof()) { |
64
|
1 |
|
return $this->dataSet->getData(); |
65
|
|
|
} |
66
|
|
|
|
67
|
1 |
|
throw $e; |
68
|
|
|
} |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* @return int |
73
|
|
|
*/ |
74
|
4 |
|
public function getCount() |
75
|
|
|
{ |
76
|
4 |
|
return (int) $this->resolveProperty('count'); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* @param int $count |
81
|
|
|
* @return $this |
82
|
|
|
*/ |
83
|
10 |
|
public function setCount($count) |
84
|
|
|
{ |
85
|
10 |
|
$this->count = $count; |
86
|
10 |
|
return $this; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* @return callable|string |
91
|
|
|
*/ |
92
|
6 |
|
public function getUntil() |
93
|
|
|
{ |
94
|
6 |
|
return $this->until; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* @param callable|string $until |
99
|
|
|
* @return $this |
100
|
|
|
*/ |
101
|
5 |
|
public function setUntil($until) |
102
|
|
|
{ |
103
|
5 |
|
$this->until = $until; |
104
|
5 |
|
return $this; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* @return AbstractField |
|
|
|
|
109
|
|
|
*/ |
110
|
5 |
|
public function getField() |
111
|
|
|
{ |
112
|
5 |
|
$field = $this->resolveProperty('field'); |
113
|
5 |
|
if (is_array($field)) { |
114
|
1 |
|
$field = Factory::get($field); |
115
|
1 |
|
} |
116
|
5 |
|
return $field; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* @param array|AbstractField $field |
121
|
|
|
* @return $this |
122
|
|
|
*/ |
123
|
10 |
|
public function setField($field) |
124
|
|
|
{ |
125
|
10 |
|
if (is_array($field)) { |
126
|
9 |
|
$field = Factory::get($field); |
127
|
9 |
|
} |
128
|
10 |
|
$this->field = $field; |
129
|
10 |
|
return $this; |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* @inheritdoc |
134
|
|
|
* @return AbstractField |
|
|
|
|
135
|
|
|
*/ |
136
|
5 |
|
public function current() |
137
|
|
|
{ |
138
|
5 |
|
return $this->getField(); |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* @inheritdoc |
143
|
|
|
*/ |
144
|
5 |
|
public function next() |
145
|
|
|
{ |
146
|
5 |
|
$this->index++; |
147
|
5 |
|
} |
148
|
|
|
|
149
|
|
|
/** |
150
|
|
|
* @inheritdoc |
151
|
|
|
*/ |
152
|
5 |
|
public function key() |
153
|
|
|
{ |
154
|
5 |
|
return $this->index; |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
/** |
158
|
|
|
* @inheritdoc |
159
|
|
|
*/ |
160
|
5 |
|
public function valid() |
161
|
|
|
{ |
162
|
5 |
|
if (is_callable($this->getUntil())) { |
163
|
2 |
|
$value = $this->getDataSet()->getValueByCurrentPath(); |
164
|
2 |
|
return call_user_func($this->getUntil(), end($value)); |
165
|
|
|
} |
166
|
3 |
|
return $this->index < $this->getCount() || $this->isUntilEof(); |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
/** |
170
|
|
|
* @inheritdoc |
171
|
|
|
*/ |
172
|
5 |
|
public function rewind() |
173
|
|
|
{ |
174
|
5 |
|
$this->index = 0; |
175
|
5 |
|
} |
176
|
|
|
|
177
|
|
|
/** |
178
|
|
|
* Whether array must read until end of file. |
179
|
|
|
* |
180
|
|
|
* @return bool |
181
|
|
|
*/ |
182
|
4 |
|
private function isUntilEof() |
183
|
|
|
{ |
184
|
4 |
|
$until = $this->getUntil(); |
185
|
4 |
|
return is_string($until) && strtolower($until) === 'eof'; |
186
|
|
|
} |
187
|
|
|
} |
This check compares the return type specified in the
@return
annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.