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 Illuminate\Contracts\Support\Responsable;
6
use Illuminate\Database\Eloquent\Model;
7
use Illuminate\Support\Collection;
8
 
9
class ResourceResponse implements Responsable
10
{
11
    /**
12
     * The underlying resource.
13
     *
14
     * @var mixed
15
     */
16
    public $resource;
17
 
18
    /**
19
     * Create a new resource response.
20
     *
21
     * @param  mixed  $resource
22
     * @return void
23
     */
24
    public function __construct($resource)
25
    {
26
        $this->resource = $resource;
27
    }
28
 
29
    /**
30
     * Create an HTTP response that represents the object.
31
     *
32
     * @param  \Illuminate\Http\Request  $request
33
     * @return \Illuminate\Http\JsonResponse
34
     */
35
    public function toResponse($request)
36
    {
37
        return tap(response()->json(
38
            $this->wrap(
39
                $this->resource->resolve($request),
40
                $this->resource->with($request),
41
                $this->resource->additional
42
            ),
43
            $this->calculateStatus(),
44
            [],
45
            $this->resource->jsonOptions()
46
        ), function ($response) use ($request) {
47
            $response->original = $this->resource->resource;
48
 
49
            $this->resource->withResponse($request, $response);
50
        });
51
    }
52
 
53
    /**
54
     * Wrap the given data if necessary.
55
     *
56
     * @param  array  $data
57
     * @param  array  $with
58
     * @param  array  $additional
59
     * @return array
60
     */
61
    protected function wrap($data, $with = [], $additional = [])
62
    {
63
        if ($data instanceof Collection) {
64
            $data = $data->all();
65
        }
66
 
67
        if ($this->haveDefaultWrapperAndDataIsUnwrapped($data)) {
68
            $data = [$this->wrapper() => $data];
69
        } elseif ($this->haveAdditionalInformationAndDataIsUnwrapped($data, $with, $additional)) {
70
            $data = [($this->wrapper() ?? 'data') => $data];
71
        }
72
 
73
        return array_merge_recursive($data, $with, $additional);
74
    }
75
 
76
    /**
77
     * Determine if we have a default wrapper and the given data is unwrapped.
78
     *
79
     * @param  array  $data
80
     * @return bool
81
     */
82
    protected function haveDefaultWrapperAndDataIsUnwrapped($data)
83
    {
84
        return $this->wrapper() && ! array_key_exists($this->wrapper(), $data);
85
    }
86
 
87
    /**
88
     * Determine if "with" data has been added and our data is unwrapped.
89
     *
90
     * @param  array  $data
91
     * @param  array  $with
92
     * @param  array  $additional
93
     * @return bool
94
     */
95
    protected function haveAdditionalInformationAndDataIsUnwrapped($data, $with, $additional)
96
    {
97
        return (! empty($with) || ! empty($additional)) &&
98
               (! $this->wrapper() ||
99
                ! array_key_exists($this->wrapper(), $data));
100
    }
101
 
102
    /**
103
     * Get the default data wrapper for the resource.
104
     *
105
     * @return string
106
     */
107
    protected function wrapper()
108
    {
109
        return get_class($this->resource)::$wrap;
110
    }
111
 
112
    /**
113
     * Calculate the appropriate status code for the response.
114
     *
115
     * @return int
116
     */
117
    protected function calculateStatus()
118
    {
119
        return $this->resource->resource instanceof Model &&
120
               $this->resource->resource->wasRecentlyCreated ? 201 : 200;
121
    }
122
}