FractalTransformer::__construct()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 1 Features 0
Metric Value
c 2
b 1
f 0
dl 0
loc 5
rs 9.4285
cc 2
eloc 3
nc 2
nop 2
1
<?php
2
3
namespace WZRD\Transformer;
4
5
use League\Fractal;
6
use Pagerfanta\Pagerfanta;
7
use League\Fractal\TransformerAbstract;
8
use WZRD\Contracts\Transformer\Transformer;
9
use League\Fractal\Pagination\PagerfantaPaginatorAdapter;
10
11
class FractalTransformer implements Transformer
12
{
13
     /**
14
      * Fractal.
15
      *
16
      * @var League\Fractal\Manager
17
      */
18
     protected $fractal;
19
20
     /**
21
      * Function to generate a route (for collections).
22
      *
23
      * @var callable
24
      */
25
     protected $route_generator;
26
27
    /**
28
     * Create a new fractal transformer instance.
29
     *
30
     * @param League\Fractal\Manager $fractal
31
     * @param callable               $route_generator
32
     */
33
    public function __construct(Fractal\Manager $fractal, callable $route_generator = null)
34
    {
35
        $this->fractal         = $fractal;
0 ignored issues
show
Documentation Bug introduced by
It seems like $fractal of type object<League\Fractal\Manager> is incompatible with the declared type object<WZRD\Transformer\League\Fractal\Manager> of property $fractal.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
36
        $this->route_generator = is_callable($route_generator) ? $route_generator : function ($page) {return $page;};
37
    }
38
39
    /**
40
     * Transform a response with a transformer.
41
     *
42
     * @param mixed                              $value
43
     * @param League\Fractal\TransformerAbstract $transformer
44
     * @param array                              $includes    Optional
45
     *
46
     * @return array
47
     */
48
    public function transform($value, $transformer, $includes = [])
49
    {
50
        if (!empty($includes)) {
51
            $this->fractal->parseIncludes(implode(',', $includes));
52
        }
53
54
        $resource = $this->createResource($value, $transformer);
55
56
        return $this->fractal->createData($resource)->toArray();
57
    }
58
59
    /**
60
     * Create a Fractal resource instance.
61
     *
62
     * @param mixed                              $value
63
     * @param League\Fractal\TransformerAbstract $transformer
64
     *
65
     * @return League\Fractal\Resource\ResourceAbstract
66
     */
67
    protected function createResource($value, TransformerAbstract $transformer)
68
    {
69
        if ($value instanceof Pagerfanta) {
70
            $resource = new Fractal\Resource\Collection($value, $transformer);
0 ignored issues
show
Documentation introduced by
$transformer is of type object<League\Fractal\TransformerAbstract>, but the function expects a callable.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
71
            $resource->setPaginator(new PagerfantaPaginatorAdapter($value, $this->route_generator));
72
        } elseif (is_array($value)) {
73
            $cursor   = new Fractal\Pagination\Cursor(null, null, null, count($value));
74
            $resource = new Fractal\Resource\Collection($value, $transformer);
0 ignored issues
show
Documentation introduced by
$transformer is of type object<League\Fractal\TransformerAbstract>, but the function expects a callable.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
75
            $resource->setCursor($cursor);
76
        } else {
77
            $resource = new Fractal\Resource\Item($value, $transformer);
0 ignored issues
show
Documentation introduced by
$transformer is of type object<League\Fractal\TransformerAbstract>, but the function expects a callable.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
78
        }
79
80
        return $resource;
81
    }
82
}
83