Completed
Push — master ( 221e6c...6037d3 )
by ARCANEDEV
9s
created

Transformer::setExtra()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 6
ccs 3
cts 3
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 2
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
abstract class Transformer implements Arrayable, Jsonable
17
{
18
    /* -----------------------------------------------------------------
19
     |  Properties
20
     | -----------------------------------------------------------------
21
     */
22
23
    /** @var  mixed */
24
    protected $resource;
25
26
    /** @var array */
27
    protected $extras = [];
28
29
    /* -----------------------------------------------------------------
30
     |  Constructor
31
     | -----------------------------------------------------------------
32
     */
33
34
    /**
35
     * Transformer constructor.
36
     *
37
     * @param  mixed  $resource
38
     */
39 30
    public function __construct($resource)
40
    {
41 30
        $this->resource = $resource;
42 30
    }
43
44
    /* -----------------------------------------------------------------
45
     |  Getters & Setters
46
     | -----------------------------------------------------------------
47
     */
48
49
    /**
50
     * Add extras data to the response.
51
     *
52
     * @param  array  $extras
53
     *
54
     * @return $this
55
     */
56 3
    public function withExtras(array $extras)
57
    {
58 3
        $this->extras = $extras;
59
60 3
        return $this;
61
    }
62
63
    /**
64
     * Add meta data to be associated with the response.
65
     *
66
     * @param  array  $data
67
     *
68
     * @return $this
69
     */
70 9
    public function withMeta(array $data)
71
    {
72 9
        return $this->setExtra('meta', Arr::sortRecursive($data));
73
    }
74
75
    /**
76
     * Get the value from the extra data.
77
     *
78
     * @param  string  $key
79
     * @param  mixed   $default
80
     *
81
     * @return mixed
82
     */
83 6
    public function getExtra($key, $default = null)
84
    {
85 6
        return Arr::get($this->extras, $key, $default);
86
    }
87
88
    /**
89
     * Set an extra data to the response.
90
     *
91
     * @param  string  $key
92
     * @param  mixed   $value
93
     *
94
     * @return $this
95
     */
96 12
    public function setExtra($key, $value)
97
    {
98 12
        $this->extras[$key] = $value;
99
100 12
        return $this;
101
    }
102
103
    /* -----------------------------------------------------------------
104
     |  Main Methods
105
     | -----------------------------------------------------------------
106
     */
107
108
    /**
109
     * Create a new instance of the response.
110
     *
111
     * @param  mixed  $resource
112
     *
113
     * @return static
114
     */
115 21
    public static function with($resource)
116
    {
117 21
        return new static($resource);
118
    }
119
120
    /**
121
     * Get a displayable API output for the given object.
122
     *
123
     * @return array
124
     */
125 30
    public function transform()
126
    {
127 30
        $object = $this->resource;
128
129 30
        $data = $object instanceof Collection || $object instanceof AbstractPaginator
0 ignored issues
show
Bug introduced by
The class Illuminate\Pagination\AbstractPaginator 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...
130 18
            ? $object->map([$this, 'transformResource'])->toArray()
131 30
            : $this->transformResource($object);
132
133 30
        if ($object instanceof AbstractPaginator) {
0 ignored issues
show
Bug introduced by
The class Illuminate\Pagination\AbstractPaginator 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...
134 6
            $this->withMeta(array_merge(
135 6
                $this->getExtra('meta', []), Arr::except($object->toArray(), ['data'])
136 2
            ));
137 2
        }
138
139 30
        $data = array_filter(compact('data') + $this->extras);
140 30
        ksort($data);
141
142 30
        return $data;
143
    }
144
145
    /**
146
     * Get a displayable API output for the given sub-resource object.
147
     *
148
     * @param  mixed  $resource
149
     *
150
     * @return array
151
     */
152 3
    public static function subResource($resource)
153
    {
154 3
        return Arr::get(static::with($resource)->toArray(), 'data');
155
    }
156
157
    /**
158
     * Transform the given resource for the API output.
159
     *
160
     * @param  mixed  $resource
161
     *
162
     * @return array
163
     */
164
    abstract public function transformResource($resource);
165
166
    /**
167
     * Get the instance as a json response object.
168
     *
169
     * @param  int    $status
170
     * @param  array  $headers
171
     * @param  int    $options
172
     *
173
     * @return \Illuminate\Http\JsonResponse
174
     */
175 3
    public function toResponse($status = 200, array $headers = [], $options = 0)
176
    {
177 3
        return new JsonResponse($this->transform(), $status, $headers, $options);
178
    }
179
180
    /**
181
     * Get the instance as an array.
182
     *
183
     * @return array
184
     */
185 21
    public function toArray()
186
    {
187 21
        return $this->transform();
188
    }
189
190
    /**
191
     * Get the instance as an array.
192
     *
193
     * @param  int  $options
194
     *
195
     * @return string
196
     */
197 3
    public function toJson($options = 0)
198
    {
199 3
        return json_encode($this->toArray(), $options);
200
    }
201
}
202