1 | <?php |
||
26 | class CompositionIterator implements Iterator, Countable |
||
27 | { |
||
28 | private $model = null; |
||
29 | |||
30 | private $direct = false; |
||
31 | |||
32 | private $types = []; |
||
33 | |||
34 | /** |
||
35 | * Models holder |
||
36 | * @var null|AnnotatedInterface[] |
||
37 | */ |
||
38 | private $models = null; |
||
39 | |||
40 | private $pointer = 0; |
||
41 | |||
42 | private $fieldNames = []; |
||
43 | |||
44 | 86 | public function __construct(AnnotatedInterface $model) |
|
48 | |||
49 | /** |
||
50 | * Limit results to only direct descendants. |
||
51 | * @return $this |
||
52 | */ |
||
53 | 6 | public function direct() |
|
58 | |||
59 | /** |
||
60 | * Limit results to only to the type provided. |
||
61 | * |
||
62 | * The `$type` should be class or interface name |
||
63 | * or object instance. |
||
64 | * |
||
65 | * Repeated calls will add types uniquely. |
||
66 | * |
||
67 | * @param $type string|object |
||
68 | * @param $include boolean Whether to include this type or skip |
||
69 | * @return $this |
||
70 | */ |
||
71 | 84 | public function ofType($type, $include = true) |
|
82 | |||
83 | /** |
||
84 | * Get currently iterated over field name, |
||
85 | * which have models |
||
86 | * @return string |
||
87 | */ |
||
88 | public function getCurrentField() |
||
96 | |||
97 | 86 | private function init() |
|
105 | |||
106 | 86 | private function iterate($model) |
|
107 | { |
||
108 | 86 | foreach (ManganMeta::create($model)->fields() as $name => $meta) |
|
109 | { |
||
110 | // Not one of: |
||
111 | // * Embedded(Array) |
||
112 | // * DbRef(Array) |
||
113 | // * Related(Array) |
||
114 | 86 | if(!$meta->owned) |
|
115 | { |
||
116 | 86 | continue; |
|
117 | } |
||
118 | 25 | if (is_array($model->$name)) |
|
119 | { |
||
120 | 15 | foreach ($model->$name as $child) |
|
121 | { |
||
122 | 15 | if ($this->skip($child)) |
|
123 | { |
||
124 | continue; |
||
125 | } |
||
126 | 15 | if ($this->doInclude($child)) |
|
127 | { |
||
128 | 9 | $this->fieldNames[] = $name; |
|
129 | 9 | $this->models[] = $child; |
|
130 | } |
||
131 | 15 | if($this->recurse()) |
|
132 | { |
||
133 | 12 | $this->iterate($child); |
|
134 | } |
||
135 | } |
||
136 | 15 | continue; |
|
137 | } |
||
138 | 20 | if ($this->skip($model->$name)) |
|
139 | { |
||
140 | 6 | continue; |
|
141 | } |
||
142 | 17 | if ($this->doInclude($model->$name)) |
|
143 | { |
||
144 | 13 | $this->fieldNames[] = $name; |
|
145 | 13 | $this->models[] = $model->$name; |
|
146 | } |
||
147 | 17 | if($this->recurse()) |
|
148 | { |
||
149 | 17 | $this->iterate($model->$name); |
|
150 | } |
||
151 | } |
||
152 | 86 | } |
|
153 | |||
154 | 25 | private function skip($model) |
|
155 | { |
||
156 | // Non-object |
||
157 | 25 | if (!is_object($model)) |
|
158 | { |
||
159 | 6 | return true; |
|
160 | } |
||
161 | // Skip if not annotated |
||
162 | 23 | if (!$model instanceof AnnotatedInterface) |
|
163 | { |
||
164 | return true; |
||
165 | } |
||
166 | 23 | return false; |
|
167 | } |
||
168 | |||
169 | /** |
||
170 | * Whether to include `$model` in result |
||
171 | * @param $model |
||
172 | * @return bool |
||
173 | */ |
||
174 | 23 | private function doInclude($model) |
|
192 | |||
193 | 23 | private function recurse() |
|
197 | |||
198 | 17 | public function current() |
|
203 | |||
204 | 17 | public function next() |
|
209 | |||
210 | public function key() |
||
215 | |||
216 | 86 | public function valid() |
|
221 | |||
222 | 86 | public function rewind() |
|
227 | |||
228 | 5 | public function count() |
|
233 | |||
234 | |||
235 | } |