Completed
Push — master ( 38b04e...42e234 )
by Arjay
02:06
created

FractalTransformer   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 69
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Importance

Changes 0
Metric Value
dl 0
loc 69
rs 10
c 0
b 0
f 0
wmc 7
lcom 0
cbo 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
B transform() 0 28 3
A createSerializer() 0 8 2
A createTransformer() 0 8 2
1
<?php
2
3
namespace Yajra\Datatables\Transformers;
4
5
use League\Fractal\Resource\Collection;
6
use League\Fractal\Serializer\SerializerAbstract;
7
use League\Fractal\TransformerAbstract;
8
9
class FractalTransformer
10
{
11
    /**
12
     * Transform output using the given transformer and serializer.
13
     *
14
     * @param array $output
15
     * @param mixed $transformer
16
     * @param mixed $serializer
17
     * @return array
18
     */
19
    public function transform(array $output, $transformer, $serializer = null)
20
    {
21
        $fractal = app('datatables.fractal');
22
23
        if ($serializer !== null) {
24
            $fractal->setSerializer($this->createSerializer($serializer));
25
        }
26
27
        //Get transformer reflection
28
        //Firs method parameter should be data/object to transform
29
        $reflection = new \ReflectionMethod($transformer, 'transform');
30
        $parameter  = $reflection->getParameters()[0];
31
32
        //If parameter is class assuming it requires object
33
        //Else just pass array by default
34
        if ($parameter->getClass()) {
35
            $resource = new Collection($output, $this->createTransformer($transformer));
36
        } else {
37
            $resource = new Collection(
38
                $output,
39
                $this->createTransformer($transformer)
40
            );
41
        }
42
43
        $collection = $fractal->createData($resource)->toArray();
44
45
        return $collection['data'];
46
    }
47
48
    /**
49
     * Get or create transformer serializer instance.
50
     *
51
     * @param mixed $serializer
52
     * @return \League\Fractal\Serializer\SerializerAbstract
53
     */
54
    protected function createSerializer($serializer)
55
    {
56
        if ($serializer instanceof SerializerAbstract) {
0 ignored issues
show
Bug introduced by
The class League\Fractal\Serializer\SerializerAbstract does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
57
            return $serializer;
58
        }
59
60
        return new $serializer();
61
    }
62
63
    /**
64
     * Get or create transformer instance.
65
     *
66
     * @param mixed $transformer
67
     * @return \League\Fractal\TransformerAbstract
68
     */
69
    protected function createTransformer($transformer)
70
    {
71
        if ($transformer instanceof TransformerAbstract) {
0 ignored issues
show
Bug introduced by
The class League\Fractal\TransformerAbstract does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
72
            return $transformer;
73
        }
74
75
        return new $transformer();
76
    }
77
}
78