1 | <?php |
||
7 | class Transformer |
||
8 | { |
||
9 | /** |
||
10 | * @param \Wandu\Transformer\Contracts\TransformResource|array|\Traversable $resource |
||
11 | * @param array $includes |
||
12 | * @return array |
||
13 | */ |
||
14 | 6 | public function transform($resource, array $includes = []) |
|
22 | |||
23 | 6 | private function transformSingle(TransformResource $resource, $includes = []) |
|
24 | { |
||
25 | 6 | $entity = $resource->transform(); |
|
26 | 6 | if (!is_array($entity)) return $entity; |
|
27 | |||
28 | // now, result is always array |
||
29 | 6 | foreach ($this->parseIncludes($resource, $includes) as $key => $nextIncludes) { |
|
30 | 3 | $entity = array_merge( |
|
31 | 3 | $entity, |
|
32 | 3 | $this->transformCollection($resource->includeAttribute($key) ?: [], $nextIncludes) |
|
33 | ); |
||
34 | } |
||
35 | 6 | return $entity; |
|
36 | } |
||
37 | |||
38 | /** |
||
39 | * @param array|\Traversable $resources |
||
40 | * @param array $includes |
||
41 | * @return array |
||
42 | */ |
||
43 | 4 | private function transformCollection($resources, array $includes = []) |
|
57 | |||
58 | /** |
||
59 | * @param \Wandu\Transformer\Contracts\TransformResource $origin |
||
60 | * @param array $includes |
||
61 | * @return array |
||
62 | */ |
||
63 | 6 | private function parseIncludes(TransformResource $origin, array $includes = []) |
|
90 | } |
||
91 |