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;
4
 
5
use Illuminate\Contracts\Support\Arrayable;
6
use Illuminate\Contracts\Support\Jsonable;
7
use Illuminate\Support\Traits\Macroable;
8
use InvalidArgumentException;
9
use JsonSerializable;
10
use Symfony\Component\HttpFoundation\JsonResponse as BaseJsonResponse;
11
 
12
class JsonResponse extends BaseJsonResponse
13
{
14
    use ResponseTrait, Macroable {
15
        Macroable::__call as macroCall;
16
    }
17
 
18
    /**
19
     * Constructor.
20
     *
21
     * @param  mixed  $data
22
     * @param  int  $status
23
     * @param  array  $headers
24
     * @param  int  $options
25
     * @param  bool  $json
26
     * @return void
27
     */
28
    public function __construct($data = null, $status = 200, $headers = [], $options = 0, $json = false)
29
    {
30
        $this->encodingOptions = $options;
31
 
32
        parent::__construct($data, $status, $headers, $json);
33
    }
34
 
35
    /**
36
     * {@inheritdoc}
37
     *
38
     * @return static
39
     */
40
    public static function fromJsonString(?string $data = null, int $status = 200, array $headers = []): static
41
    {
42
        return new static($data, $status, $headers, 0, true);
43
    }
44
 
45
    /**
46
     * Sets the JSONP callback.
47
     *
48
     * @param  string|null  $callback
49
     * @return $this
50
     */
51
    public function withCallback($callback = null)
52
    {
53
        return $this->setCallback($callback);
54
    }
55
 
56
    /**
57
     * Get the json_decoded data from the response.
58
     *
59
     * @param  bool  $assoc
60
     * @param  int  $depth
61
     * @return mixed
62
     */
63
    public function getData($assoc = false, $depth = 512)
64
    {
65
        return json_decode($this->data, $assoc, $depth);
66
    }
67
 
68
    /**
69
     * {@inheritdoc}
70
     *
71
     * @return static
72
     */
73
    public function setData($data = []): static
74
    {
75
        $this->original = $data;
76
 
77
        // Ensure json_last_error() is cleared...
78
        json_decode('[]');
79
 
80
        if ($data instanceof Jsonable) {
81
            $this->data = $data->toJson($this->encodingOptions);
82
        } elseif ($data instanceof JsonSerializable) {
83
            $this->data = json_encode($data->jsonSerialize(), $this->encodingOptions);
84
        } elseif ($data instanceof Arrayable) {
85
            $this->data = json_encode($data->toArray(), $this->encodingOptions);
86
        } else {
87
            $this->data = json_encode($data, $this->encodingOptions);
88
        }
89
 
90
        if (! $this->hasValidJson(json_last_error())) {
91
            throw new InvalidArgumentException(json_last_error_msg());
92
        }
93
 
94
        return $this->update();
95
    }
96
 
97
    /**
98
     * Determine if an error occurred during JSON encoding.
99
     *
100
     * @param  int  $jsonError
101
     * @return bool
102
     */
103
    protected function hasValidJson($jsonError)
104
    {
105
        if ($jsonError === JSON_ERROR_NONE) {
106
            return true;
107
        }
108
 
109
        return $this->hasEncodingOption(JSON_PARTIAL_OUTPUT_ON_ERROR) &&
110
                    in_array($jsonError, [
111
                        JSON_ERROR_RECURSION,
112
                        JSON_ERROR_INF_OR_NAN,
113
                        JSON_ERROR_UNSUPPORTED_TYPE,
114
                    ]);
115
    }
116
 
117
    /**
118
     * {@inheritdoc}
119
     *
120
     * @return static
121
     */
122
    public function setEncodingOptions($options): static
123
    {
124
        $this->encodingOptions = (int) $options;
125
 
126
        return $this->setData($this->getData());
127
    }
128
 
129
    /**
130
     * Determine if a JSON encoding option is set.
131
     *
132
     * @param  int  $option
133
     * @return bool
134
     */
135
    public function hasEncodingOption($option)
136
    {
137
        return (bool) ($this->encodingOptions & $option);
138
    }
139
}