1 | <?php |
||
25 | class Manager |
||
26 | { |
||
27 | /** |
||
28 | * Array of scope identifiers for resources to include. |
||
29 | * |
||
30 | * @var array |
||
31 | */ |
||
32 | protected $requestedIncludes = []; |
||
33 | |||
34 | /** |
||
35 | * Array of scope identifiers for resources to exclude. |
||
36 | * |
||
37 | * @var array |
||
38 | */ |
||
39 | protected $requestedExcludes = []; |
||
40 | |||
41 | /** |
||
42 | * Array containing modifiers as keys and an array value of params. |
||
43 | * |
||
44 | * @var array |
||
45 | */ |
||
46 | protected $includeParams = []; |
||
47 | |||
48 | /** |
||
49 | * The character used to separate modifier parameters. |
||
50 | * |
||
51 | * @var string |
||
52 | */ |
||
53 | protected $paramDelimiter = '|'; |
||
54 | |||
55 | /** |
||
56 | * Upper limit to how many levels of included data are allowed. |
||
57 | * |
||
58 | * @var int |
||
59 | */ |
||
60 | protected $recursionLimit = 10; |
||
61 | |||
62 | /** |
||
63 | * Serializer. |
||
64 | * |
||
65 | * @var SerializerAbstract |
||
66 | */ |
||
67 | protected $serializer; |
||
68 | |||
69 | /** |
||
70 | * Create Data. |
||
71 | * |
||
72 | * Main method to kick this all off. Make a resource then pass it over, and use toArray() |
||
73 | * |
||
74 | * @param ResourceInterface $resource |
||
75 | * @param string $scopeIdentifier |
||
76 | * @param Scope $parentScopeInstance |
||
77 | * |
||
78 | * @return Scope |
||
79 | */ |
||
80 | 32 | public function createData(ResourceInterface $resource, $scopeIdentifier = null, Scope $parentScopeInstance = null) |
|
81 | { |
||
82 | 32 | $scopeInstance = new Scope($this, $resource, $scopeIdentifier); |
|
83 | |||
84 | // Update scope history |
||
85 | 32 | if ($parentScopeInstance !== null) { |
|
86 | // This will be the new children list of parents (parents parents, plus the parent) |
||
87 | 27 | $scopeArray = $parentScopeInstance->getParentScopes(); |
|
88 | 27 | $scopeArray[] = $parentScopeInstance->getScopeIdentifier(); |
|
89 | |||
90 | 27 | $scopeInstance->setParentScopes($scopeArray); |
|
91 | 27 | } |
|
92 | |||
93 | 32 | return $scopeInstance; |
|
94 | } |
||
95 | |||
96 | /** |
||
97 | * Get Include Params. |
||
98 | * |
||
99 | * @param string $include |
||
100 | * |
||
101 | * @return \League\Fractal\ParamBag|null |
||
102 | */ |
||
103 | 23 | public function getIncludeParams($include) |
|
104 | { |
||
105 | 23 | if (! isset($this->includeParams[$include])) { |
|
106 | 21 | return; |
|
107 | } |
||
108 | |||
109 | 2 | $params = $this->includeParams[$include]; |
|
110 | |||
111 | 2 | return new ParamBag($params); |
|
112 | } |
||
113 | |||
114 | /** |
||
115 | * Get Requested Includes. |
||
116 | * |
||
117 | * @return array |
||
118 | */ |
||
119 | 34 | public function getRequestedIncludes() |
|
123 | |||
124 | /** |
||
125 | * Get Requested Excludes. |
||
126 | * |
||
127 | * @return array |
||
128 | */ |
||
129 | 24 | public function getRequestedExcludes() |
|
133 | |||
134 | /** |
||
135 | * Get Serializer. |
||
136 | * |
||
137 | * @return SerializerAbstract |
||
138 | */ |
||
139 | 42 | public function getSerializer() |
|
140 | { |
||
141 | 42 | if (! $this->serializer) { |
|
142 | 7 | $this->setSerializer(new DataArraySerializer()); |
|
143 | 7 | } |
|
144 | |||
145 | 42 | return $this->serializer; |
|
146 | } |
||
147 | |||
148 | /** |
||
149 | * Parse Include String. |
||
150 | * |
||
151 | * @param array|string $includes Array or csv string of resources to include |
||
152 | * |
||
153 | * @return $this |
||
154 | */ |
||
155 | 36 | public function parseIncludes($includes) |
|
156 | { |
||
157 | // Wipe these before we go again |
||
158 | 36 | $this->requestedIncludes = $this->includeParams = []; |
|
159 | |||
160 | 36 | if (is_string($includes)) { |
|
161 | 30 | $includes = explode(',', $includes); |
|
162 | 30 | } |
|
163 | |||
164 | 36 | if (! is_array($includes)) { |
|
165 | 2 | throw new \InvalidArgumentException( |
|
166 | 2 | 'The parseIncludes() method expects a string or an array. '.gettype($includes).' given' |
|
167 | 2 | ); |
|
168 | } |
||
169 | |||
170 | 34 | foreach ($includes as $include) { |
|
171 | 34 | list($includeName, $allModifiersStr) = array_pad(explode(':', $include, 2), 2, null); |
|
172 | |||
173 | // Trim it down to a cool level of recursion |
||
174 | 34 | $includeName = $this->trimToAcceptableRecursionLevel($includeName); |
|
175 | |||
176 | 34 | if (in_array($includeName, $this->requestedIncludes)) { |
|
177 | 1 | continue; |
|
178 | } |
||
179 | 34 | $this->requestedIncludes[] = $includeName; |
|
180 | |||
181 | // No Params? Bored |
||
182 | 34 | if ($allModifiersStr === null) { |
|
183 | 33 | continue; |
|
184 | } |
||
185 | |||
186 | // Matches multiple instances of 'something(foo|bar|baz)' in the string |
||
187 | // I guess it ignores : so you could use anything, but probably don't do that |
||
188 | 2 | preg_match_all('/([\w]+)(\(([^\)]+)\))?/', $allModifiersStr, $allModifiersArr); |
|
189 | |||
190 | // [0] is full matched strings... |
||
191 | 2 | $modifierCount = count($allModifiersArr[0]); |
|
192 | |||
193 | 2 | $modifierArr = []; |
|
194 | |||
195 | 2 | for ($modifierIt = 0; $modifierIt < $modifierCount; $modifierIt++) { |
|
196 | // [1] is the modifier |
||
197 | 2 | $modifierName = $allModifiersArr[1][$modifierIt]; |
|
198 | |||
199 | // and [3] is delimited params |
||
200 | 2 | $modifierParamStr = $allModifiersArr[3][$modifierIt]; |
|
201 | |||
202 | // Make modifier array key with an array of params as the value |
||
203 | 2 | $modifierArr[$modifierName] = explode($this->paramDelimiter, $modifierParamStr); |
|
204 | 2 | } |
|
205 | |||
206 | 2 | $this->includeParams[$includeName] = $modifierArr; |
|
207 | 34 | } |
|
208 | |||
209 | // This should be optional and public someday, but without it includes would never show up |
||
210 | 34 | $this->autoIncludeParents(); |
|
211 | |||
212 | 34 | return $this; |
|
213 | } |
||
214 | |||
215 | /** |
||
216 | * Parse Exclude String. |
||
217 | * |
||
218 | * @param array|string $excludes Array or csv string of resources to exclude |
||
219 | * |
||
220 | * @return $this |
||
221 | */ |
||
222 | 5 | public function parseExcludes($excludes) |
|
223 | { |
||
224 | 5 | $this->requestedExcludes = []; |
|
225 | |||
226 | 5 | if (is_string($excludes)) { |
|
227 | 2 | $excludes = explode(',', $excludes); |
|
228 | 2 | } |
|
229 | |||
230 | 5 | if (! is_array($excludes)) { |
|
231 | 2 | throw new \InvalidArgumentException( |
|
232 | 2 | 'The parseExcludes() method expects a string or an array. '.gettype($excludes).' given' |
|
233 | 2 | ); |
|
234 | } |
||
235 | |||
236 | 3 | foreach ($excludes as $excludeName) { |
|
237 | 3 | $excludeName = $this->trimToAcceptableRecursionLevel($excludeName); |
|
238 | |||
239 | 3 | if (in_array($excludeName, $this->requestedExcludes)) { |
|
240 | 1 | continue; |
|
241 | } |
||
242 | |||
243 | 3 | $this->requestedExcludes[] = $excludeName; |
|
244 | 3 | } |
|
245 | |||
246 | 3 | return $this; |
|
247 | } |
||
248 | |||
249 | /** |
||
250 | * Set Recursion Limit. |
||
251 | * |
||
252 | * @param int $recursionLimit |
||
253 | * |
||
254 | * @return $this |
||
255 | */ |
||
256 | 1 | public function setRecursionLimit($recursionLimit) |
|
257 | { |
||
258 | 1 | $this->recursionLimit = $recursionLimit; |
|
259 | |||
260 | 1 | return $this; |
|
261 | } |
||
262 | |||
263 | /** |
||
264 | * Set Serializer |
||
265 | * |
||
266 | * @param SerializerAbstract $serializer |
||
267 | * |
||
268 | * @return $this |
||
269 | */ |
||
270 | 42 | public function setSerializer(SerializerAbstract $serializer) |
|
271 | { |
||
272 | 42 | $this->serializer = $serializer; |
|
273 | |||
274 | 42 | return $this; |
|
275 | } |
||
276 | |||
277 | /** |
||
278 | * Auto-include Parents |
||
279 | * |
||
280 | * Look at the requested includes and automatically include the parents if they |
||
281 | * are not explicitly requested. E.g: [foo, bar.baz] becomes [foo, bar, bar.baz] |
||
282 | * |
||
283 | * @internal |
||
284 | * |
||
285 | * @return void |
||
286 | */ |
||
287 | 34 | protected function autoIncludeParents() |
|
288 | { |
||
289 | 34 | $parsed = []; |
|
290 | |||
291 | 34 | foreach ($this->requestedIncludes as $include) { |
|
292 | 34 | $nested = explode('.', $include); |
|
293 | |||
294 | 34 | $part = array_shift($nested); |
|
295 | 34 | $parsed[] = $part; |
|
296 | |||
297 | 34 | while (count($nested) > 0) { |
|
298 | 7 | $part .= '.'.array_shift($nested); |
|
299 | 7 | $parsed[] = $part; |
|
300 | 7 | } |
|
301 | 34 | } |
|
302 | |||
303 | 34 | $this->requestedIncludes = array_values(array_unique($parsed)); |
|
304 | 34 | } |
|
305 | |||
306 | /** |
||
307 | * Trim to Acceptable Recursion Level |
||
308 | * |
||
309 | * Strip off any requested resources that are too many levels deep, to avoid DiCaprio being chased |
||
310 | * by trains or whatever the hell that movie was about. |
||
311 | * |
||
312 | * @internal |
||
313 | * |
||
314 | * @param string $includeName |
||
315 | * |
||
316 | * @return string |
||
317 | */ |
||
318 | 36 | protected function trimToAcceptableRecursionLevel($includeName) |
|
322 | } |
||
323 |