Subversion-Projekte lars-tiefland.laravel_shop

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
148 lars 1
<?php
2
 
3
namespace Illuminate\Http\Resources\Json;
4
 
5
use ArrayAccess;
6
use Illuminate\Container\Container;
7
use Illuminate\Contracts\Routing\UrlRoutable;
8
use Illuminate\Contracts\Support\Arrayable;
9
use Illuminate\Contracts\Support\Responsable;
10
use Illuminate\Database\Eloquent\JsonEncodingException;
11
use Illuminate\Http\Resources\ConditionallyLoadsAttributes;
12
use Illuminate\Http\Resources\DelegatesToResource;
13
use JsonSerializable;
14
 
15
class JsonResource implements ArrayAccess, JsonSerializable, Responsable, UrlRoutable
16
{
17
    use ConditionallyLoadsAttributes, DelegatesToResource;
18
 
19
    /**
20
     * The resource instance.
21
     *
22
     * @var mixed
23
     */
24
    public $resource;
25
 
26
    /**
27
     * The additional data that should be added to the top-level resource array.
28
     *
29
     * @var array
30
     */
31
    public $with = [];
32
 
33
    /**
34
     * The additional meta data that should be added to the resource response.
35
     *
36
     * Added during response construction by the developer.
37
     *
38
     * @var array
39
     */
40
    public $additional = [];
41
 
42
    /**
43
     * The "data" wrapper that should be applied.
44
     *
45
     * @var string|null
46
     */
47
    public static $wrap = 'data';
48
 
49
    /**
50
     * Create a new resource instance.
51
     *
52
     * @param  mixed  $resource
53
     * @return void
54
     */
55
    public function __construct($resource)
56
    {
57
        $this->resource = $resource;
58
    }
59
 
60
    /**
61
     * Create a new resource instance.
62
     *
63
     * @param  mixed  ...$parameters
64
     * @return static
65
     */
66
    public static function make(...$parameters)
67
    {
68
        return new static(...$parameters);
69
    }
70
 
71
    /**
72
     * Create a new anonymous resource collection.
73
     *
74
     * @param  mixed  $resource
75
     * @return \Illuminate\Http\Resources\Json\AnonymousResourceCollection
76
     */
77
    public static function collection($resource)
78
    {
79
        return tap(new AnonymousResourceCollection($resource, static::class), function ($collection) {
80
            if (property_exists(static::class, 'preserveKeys')) {
81
                $collection->preserveKeys = (new static([]))->preserveKeys === true;
82
            }
83
        });
84
    }
85
 
86
    /**
87
     * Resolve the resource to an array.
88
     *
89
     * @param  \Illuminate\Http\Request|null  $request
90
     * @return array
91
     */
92
    public function resolve($request = null)
93
    {
94
        $data = $this->toArray(
95
            $request = $request ?: Container::getInstance()->make('request')
96
        );
97
 
98
        if ($data instanceof Arrayable) {
99
            $data = $data->toArray();
100
        } elseif ($data instanceof JsonSerializable) {
101
            $data = $data->jsonSerialize();
102
        }
103
 
104
        return $this->filter((array) $data);
105
    }
106
 
107
    /**
108
     * Transform the resource into an array.
109
     *
110
     * @param  \Illuminate\Http\Request  $request
111
     * @return array|\Illuminate\Contracts\Support\Arrayable|\JsonSerializable
112
     */
113
    public function toArray($request)
114
    {
115
        if (is_null($this->resource)) {
116
            return [];
117
        }
118
 
119
        return is_array($this->resource)
120
            ? $this->resource
121
            : $this->resource->toArray();
122
    }
123
 
124
    /**
125
     * Convert the model instance to JSON.
126
     *
127
     * @param  int  $options
128
     * @return string
129
     *
130
     * @throws \Illuminate\Database\Eloquent\JsonEncodingException
131
     */
132
    public function toJson($options = 0)
133
    {
134
        $json = json_encode($this->jsonSerialize(), $options);
135
 
136
        if (json_last_error() !== JSON_ERROR_NONE) {
137
            throw JsonEncodingException::forResource($this, json_last_error_msg());
138
        }
139
 
140
        return $json;
141
    }
142
 
143
    /**
144
     * Get any additional data that should be returned with the resource array.
145
     *
146
     * @param  \Illuminate\Http\Request  $request
147
     * @return array
148
     */
149
    public function with($request)
150
    {
151
        return $this->with;
152
    }
153
 
154
    /**
155
     * Add additional meta data to the resource response.
156
     *
157
     * @param  array  $data
158
     * @return $this
159
     */
160
    public function additional(array $data)
161
    {
162
        $this->additional = $data;
163
 
164
        return $this;
165
    }
166
 
167
    /**
168
     * Get the JSON serialization options that should be applied to the resource response.
169
     *
170
     * @return int
171
     */
172
    public function jsonOptions()
173
    {
174
        return 0;
175
    }
176
 
177
    /**
178
     * Customize the response for a request.
179
     *
180
     * @param  \Illuminate\Http\Request  $request
181
     * @param  \Illuminate\Http\JsonResponse  $response
182
     * @return void
183
     */
184
    public function withResponse($request, $response)
185
    {
186
        //
187
    }
188
 
189
    /**
190
     * Set the string that should wrap the outer-most resource array.
191
     *
192
     * @param  string  $value
193
     * @return void
194
     */
195
    public static function wrap($value)
196
    {
197
        static::$wrap = $value;
198
    }
199
 
200
    /**
201
     * Disable wrapping of the outer-most resource array.
202
     *
203
     * @return void
204
     */
205
    public static function withoutWrapping()
206
    {
207
        static::$wrap = null;
208
    }
209
 
210
    /**
211
     * Transform the resource into an HTTP response.
212
     *
213
     * @param  \Illuminate\Http\Request|null  $request
214
     * @return \Illuminate\Http\JsonResponse
215
     */
216
    public function response($request = null)
217
    {
218
        return $this->toResponse(
219
            $request ?: Container::getInstance()->make('request')
220
        );
221
    }
222
 
223
    /**
224
     * Create an HTTP response that represents the object.
225
     *
226
     * @param  \Illuminate\Http\Request  $request
227
     * @return \Illuminate\Http\JsonResponse
228
     */
229
    public function toResponse($request)
230
    {
231
        return (new ResourceResponse($this))->toResponse($request);
232
    }
233
 
234
    /**
235
     * Prepare the resource for JSON serialization.
236
     *
237
     * @return array
238
     */
239
    public function jsonSerialize(): array
240
    {
241
        return $this->resolve(Container::getInstance()->make('request'));
242
    }
243
}