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\Auth\Access;
4
 
5
use Illuminate\Contracts\Support\Arrayable;
6
 
7
class Response implements Arrayable
8
{
9
    /**
10
     * Indicates whether the response was allowed.
11
     *
12
     * @var bool
13
     */
14
    protected $allowed;
15
 
16
    /**
17
     * The response message.
18
     *
19
     * @var string|null
20
     */
21
    protected $message;
22
 
23
    /**
24
     * The response code.
25
     *
26
     * @var mixed
27
     */
28
    protected $code;
29
 
30
    /**
31
     * The HTTP response status code.
32
     *
33
     * @var int|null
34
     */
35
    protected $status;
36
 
37
    /**
38
     * Create a new response.
39
     *
40
     * @param  bool  $allowed
41
     * @param  string  $message
42
     * @param  mixed  $code
43
     * @return void
44
     */
45
    public function __construct($allowed, $message = '', $code = null)
46
    {
47
        $this->code = $code;
48
        $this->allowed = $allowed;
49
        $this->message = $message;
50
    }
51
 
52
    /**
53
     * Create a new "allow" Response.
54
     *
55
     * @param  string|null  $message
56
     * @param  mixed  $code
57
     * @return \Illuminate\Auth\Access\Response
58
     */
59
    public static function allow($message = null, $code = null)
60
    {
61
        return new static(true, $message, $code);
62
    }
63
 
64
    /**
65
     * Create a new "deny" Response.
66
     *
67
     * @param  string|null  $message
68
     * @param  mixed  $code
69
     * @return \Illuminate\Auth\Access\Response
70
     */
71
    public static function deny($message = null, $code = null)
72
    {
73
        return new static(false, $message, $code);
74
    }
75
 
76
    /**
77
     * Create a new "deny" Response with a HTTP status code.
78
     *
79
     * @param  int  $status
80
     * @param  string|null  $message
81
     * @param  mixed  $code
82
     * @return \Illuminate\Auth\Access\Response
83
     */
84
    public static function denyWithStatus($status, $message = null, $code = null)
85
    {
86
        return static::deny($message, $code)->withStatus($status);
87
    }
88
 
89
    /**
90
     * Create a new "deny" Response with a 404 HTTP status code.
91
     *
92
     * @param  string|null  $message
93
     * @param  mixed  $code
94
     * @return \Illuminate\Auth\Access\Response
95
     */
96
    public static function denyAsNotFound($message = null, $code = null)
97
    {
98
        return static::denyWithStatus(404, $message, $code);
99
    }
100
 
101
    /**
102
     * Determine if the response was allowed.
103
     *
104
     * @return bool
105
     */
106
    public function allowed()
107
    {
108
        return $this->allowed;
109
    }
110
 
111
    /**
112
     * Determine if the response was denied.
113
     *
114
     * @return bool
115
     */
116
    public function denied()
117
    {
118
        return ! $this->allowed();
119
    }
120
 
121
    /**
122
     * Get the response message.
123
     *
124
     * @return string|null
125
     */
126
    public function message()
127
    {
128
        return $this->message;
129
    }
130
 
131
    /**
132
     * Get the response code / reason.
133
     *
134
     * @return mixed
135
     */
136
    public function code()
137
    {
138
        return $this->code;
139
    }
140
 
141
    /**
142
     * Throw authorization exception if response was denied.
143
     *
144
     * @return \Illuminate\Auth\Access\Response
145
     *
146
     * @throws \Illuminate\Auth\Access\AuthorizationException
147
     */
148
    public function authorize()
149
    {
150
        if ($this->denied()) {
151
            throw (new AuthorizationException($this->message(), $this->code()))
152
                ->setResponse($this)
153
                ->withStatus($this->status);
154
        }
155
 
156
        return $this;
157
    }
158
 
159
    /**
160
     * Set the HTTP response status code.
161
     *
162
     * @param  null|int  $status
163
     * @return $this
164
     */
165
    public function withStatus($status)
166
    {
167
        $this->status = $status;
168
 
169
        return $this;
170
    }
171
 
172
    /**
173
     * Set the HTTP response status code to 404.
174
     *
175
     * @return $this
176
     */
177
    public function asNotFound()
178
    {
179
        return $this->withStatus(404);
180
    }
181
 
182
    /**
183
     * Get the HTTP status code.
184
     *
185
     * @return int|null
186
     */
187
    public function status()
188
    {
189
        return $this->status;
190
    }
191
 
192
    /**
193
     * Convert the response to an array.
194
     *
195
     * @return array
196
     */
197
    public function toArray()
198
    {
199
        return [
200
            'allowed' => $this->allowed(),
201
            'message' => $this->message(),
202
            'code' => $this->code(),
203
        ];
204
    }
205
 
206
    /**
207
     * Get the string representation of the message.
208
     *
209
     * @return string
210
     */
211
    public function __toString()
212
    {
213
        return (string) $this->message();
214
    }
215
}