Completed
Pull Request — master (#4)
by ARCANEDEV
05:37
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', $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
        return array_filter(compact('data') + $this->extras);
140
    }
141
142
    /**
143
     * Get a displayable API output for the given sub-resource object.
144
     *
145
     * @param  mixed  $resource
146
     *
147
     * @return array
148
     */
149 3
    public static function subResource($resource)
150
    {
151 3
        return Arr::get(static::with($resource)->toArray(), 'data');
152
    }
153
154
    /**
155
     * Transform the given resource for the API output.
156
     *
157
     * @param  mixed  $resource
158
     *
159
     * @return array
160
     */
161
    abstract public function transformResource($resource);
162
163
    /**
164
     * Get the instance as a json response object.
165
     *
166
     * @param  int    $status
167
     * @param  array  $headers
168
     * @param  int    $options
169
     *
170
     * @return \Illuminate\Http\JsonResponse
171
     */
172 3
    public function toResponse($status = 200, array $headers = [], $options = 0)
173
    {
174 3
        return new JsonResponse($this->transform(), $status, $headers, $options);
175
    }
176
177
    /**
178
     * Get the instance as an array.
179
     *
180
     * @return array
181
     */
182 21
    public function toArray()
183
    {
184 21
        return $this->transform();
185
    }
186
187
    /**
188
     * Get the instance as an array.
189
     *
190
     * @param  int  $options
191
     *
192
     * @return string
193
     */
194 3
    public function toJson($options = 0)
195
    {
196 3
        return json_encode($this->toArray(), $options);
197
    }
198
}
199