FractalTransformer::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
1
<?php
2
3
namespace Yajra\DataTables\Transformers;
4
5
use League\Fractal\Manager;
6
use League\Fractal\Resource\Collection;
7
use League\Fractal\TransformerAbstract;
8
use League\Fractal\Serializer\SerializerAbstract;
9
10
class FractalTransformer
11
{
12
    /**
13
     * @var \League\Fractal\Manager
14
     */
15
    protected $fractal;
16
17
    /**
18
     * FractalTransformer constructor.
19
     *
20
     * @param \League\Fractal\Manager $fractal
21
     */
22
    public function __construct(Manager $fractal)
23
    {
24
        $this->fractal = $fractal;
25
    }
26
27
    /**
28
     * Transform output using the given transformer and serializer.
29
     *
30
     * @param  mixed $output
31
     * @param  mixed $transformer
32
     * @param  mixed $serializer
33
     * @return array
34
     */
35
    public function transform($output, $transformer, $serializer = null)
36
    {
37
        if ($serializer !== null) {
38
            $this->fractal->setSerializer($this->createSerializer($serializer));
39
        }
40
41
        $collector = [];
42
        foreach ($transformer as $transform) {
43
            if ($transform != null) {
44
                $resource       = new Collection($output, $this->createTransformer($transform));
45
                $collection     = $this->fractal->createData($resource)->toArray();
46
                $transformed    = $collection['data'] ?? $collection;
47
                $collector      = array_map(
48
                    function ($item_collector, $item_transformed) {
49
                        if ($item_collector === null) {
50
                            $item_collector = [];
51
                        }
52
53
                        return array_merge($item_collector, $item_transformed);
54
                    }, $collector, $transformed
55
                );
56
            }
57
        }
58
59
        return $collector;
60
    }
61
62
    /**
63
     * Get or create transformer serializer instance.
64
     *
65
     * @param  mixed $serializer
66
     * @return \League\Fractal\Serializer\SerializerAbstract
67
     */
68
    protected function createSerializer($serializer)
69
    {
70
        if ($serializer instanceof SerializerAbstract) {
71
            return $serializer;
72
        }
73
74
        return new $serializer();
75
    }
76
77
    /**
78
     * Get or create transformer instance.
79
     *
80
     * @param  mixed $transformer
81
     * @return \League\Fractal\TransformerAbstract
82
     */
83
    protected function createTransformer($transformer)
84
    {
85
        if ($transformer instanceof TransformerAbstract || $transformer instanceof \Closure) {
86
            return $transformer;
0 ignored issues
show
Bug Compatibility introduced by
The expression return $transformer; of type League\Fractal\TransformerAbstract|Closure is incompatible with the return type documented by Yajra\DataTables\Transfo...rmer::createTransformer of type League\Fractal\TransformerAbstract as it can also be of type Closure which is not included in this return type.
Loading history...
87
        }
88
89
        return new $transformer();
90
    }
91
}
92