Transformer::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 1
1
<?php namespace Arcanedev\LaravelApiHelper;
2
3
use Illuminate\Contracts\Support\Arrayable;
4
use Illuminate\Contracts\Support\Jsonable;
5
use Illuminate\Http\JsonResponse;
6
use Illuminate\Pagination\AbstractPaginator;
7
use Illuminate\Support\Arr;
8
use Illuminate\Support\Collection;
9
10
/**
11
 * Class     Transformer
12
 *
13
 * @package  Arcanedev\LaravelApiHelper
14
 * @author   ARCANEDEV <[email protected]>
15
 *
16
 * @deprecated Since v2.2, use \Arcanedev\LaravelApiHelper\Http\Resource instead.
17
 */
18
abstract class Transformer implements Arrayable, Jsonable
19
{
20
    /* -----------------------------------------------------------------
21
     |  Properties
22
     | -----------------------------------------------------------------
23
     */
24
25
    /** @var  mixed */
26
    protected $resource;
27
28
    /** @var array */
29
    protected $extras = [];
30
31
    /* -----------------------------------------------------------------
32
     |  Constructor
33
     | -----------------------------------------------------------------
34
     */
35
36
    /**
37
     * Transformer constructor.
38
     *
39
     * @param  mixed  $resource
40
     */
41 40
    public function __construct($resource)
42
    {
43 40
        $this->resource = $resource;
44 40
    }
45
46
    /* -----------------------------------------------------------------
47
     |  Getters & Setters
48
     | -----------------------------------------------------------------
49
     */
50
51
    /**
52
     * Add extras data to the response.
53
     *
54
     * @param  array  $extras
55
     *
56
     * @return $this
57
     */
58 4
    public function withExtras(array $extras)
59
    {
60 4
        $this->extras = $extras;
61
62 4
        return $this;
63
    }
64
65
    /**
66
     * Add meta data to be associated with the response.
67
     *
68
     * @param  array  $data
69
     *
70
     * @return $this
71
     */
72 12
    public function withMeta(array $data)
73
    {
74 12
        return $this->setExtra('meta', Arr::sortRecursive($data));
75
    }
76
77
    /**
78
     * Get the value from the extra data.
79
     *
80
     * @param  string  $key
81
     * @param  mixed   $default
82
     *
83
     * @return mixed
84
     */
85 8
    public function getExtra($key, $default = null)
86
    {
87 8
        return Arr::get($this->extras, $key, $default);
88
    }
89
90
    /**
91
     * Set an extra data to the response.
92
     *
93
     * @param  string  $key
94
     * @param  mixed   $value
95
     *
96
     * @return $this
97
     */
98 16
    public function setExtra($key, $value)
99
    {
100 16
        $this->extras[$key] = $value;
101
102 16
        return $this;
103
    }
104
105
    /* -----------------------------------------------------------------
106
     |  Main Methods
107
     | -----------------------------------------------------------------
108
     */
109
110
    /**
111
     * Create a new instance of the response.
112
     *
113
     * @param  mixed  $resource
114
     *
115
     * @return static
116
     */
117 28
    public static function with($resource)
118
    {
119 28
        return new static($resource);
0 ignored issues
show
Deprecated Code introduced by
The class Arcanedev\LaravelApiHelper\Transformer has been deprecated with message: Since v2.2, use \Arcanedev\LaravelApiHelper\Http\Resource instead.

This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the type will be removed from the class and what other constant to use instead.

Loading history...
120
    }
121
122
    /**
123
     * Get a displayable API output for the given object.
124
     *
125
     * @return array
126
     */
127 40
    public function transform()
128
    {
129 40
        $object = $this->resource;
130
131 40
        $data = $object instanceof Collection || $object instanceof AbstractPaginator
132 16
            ? $object->map([$this, 'transformResource'])->toArray()
0 ignored issues
show
Bug introduced by
The method map does only exist in Illuminate\Support\Collection, but not in Illuminate\Pagination\AbstractPaginator.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
133 40
            : $this->transformResource($object);
134
135 40
        if ($object instanceof AbstractPaginator) {
136 8
            $this->withMeta(array_merge(
137 8
                $this->getExtra('meta', []), Arr::except($object->toArray(), ['data'])
138
            ));
139
        }
140
141 40
        $data = array_filter(compact('data') + $this->extras);
142 40
        ksort($data);
143
144 40
        return $data;
145
    }
146
147
    /**
148
     * Get a displayable API output for the given sub-resource object.
149
     *
150
     * @param  mixed  $resource
151
     *
152
     * @return array
153
     */
154 4
    public static function subResource($resource)
155
    {
156 4
        return Arr::get(static::with($resource)->toArray(), 'data');
157
    }
158
159
    /**
160
     * Transform the given resource for the API output.
161
     *
162
     * @param  mixed  $resource
163
     *
164
     * @return array
165
     */
166
    abstract public function transformResource($resource);
167
168
    /**
169
     * Get the instance as a json response object.
170
     *
171
     * @param  int    $status
172
     * @param  array  $headers
173
     * @param  int    $options
174
     *
175
     * @return \Illuminate\Http\JsonResponse
176
     */
177 4
    public function toResponse($status = 200, array $headers = [], $options = 0)
178
    {
179 4
        return new JsonResponse($this->transform(), $status, $headers, $options);
180
    }
181
182
    /**
183
     * Get the instance as an array.
184
     *
185
     * @return array
186
     */
187 28
    public function toArray()
188
    {
189 28
        return $this->transform();
190
    }
191
192
    /**
193
     * Get the instance as an array.
194
     *
195
     * @param  int  $options
196
     *
197
     * @return string
198
     */
199 4
    public function toJson($options = 0)
200
    {
201 4
        return json_encode($this->toArray(), $options);
202
    }
203
}
204