Subversion-Projekte lars-tiefland.laravel_shop

Revision

Zur aktuellen Revision | Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
148 lars 1
<?php
2
 
3
namespace Illuminate\Http\Client;
4
 
5
use Illuminate\Support\Traits\Macroable;
6
use OutOfBoundsException;
7
 
8
class ResponseSequence
9
{
10
    use Macroable;
11
 
12
    /**
13
     * The responses in the sequence.
14
     *
15
     * @var array
16
     */
17
    protected $responses;
18
 
19
    /**
20
     * Indicates that invoking this sequence when it is empty should throw an exception.
21
     *
22
     * @var bool
23
     */
24
    protected $failWhenEmpty = true;
25
 
26
    /**
27
     * The response that should be returned when the sequence is empty.
28
     *
29
     * @var \GuzzleHttp\Promise\PromiseInterface
30
     */
31
    protected $emptyResponse;
32
 
33
    /**
34
     * Create a new response sequence.
35
     *
36
     * @param  array  $responses
37
     * @return void
38
     */
39
    public function __construct(array $responses)
40
    {
41
        $this->responses = $responses;
42
    }
43
 
44
    /**
45
     * Push a response to the sequence.
46
     *
47
     * @param  string|array|null  $body
48
     * @param  int  $status
49
     * @param  array  $headers
50
     * @return $this
51
     */
52
    public function push($body = null, int $status = 200, array $headers = [])
53
    {
54
        return $this->pushResponse(
55
            Factory::response($body, $status, $headers)
56
        );
57
    }
58
 
59
    /**
60
     * Push a response with the given status code to the sequence.
61
     *
62
     * @param  int  $status
63
     * @param  array  $headers
64
     * @return $this
65
     */
66
    public function pushStatus(int $status, array $headers = [])
67
    {
68
        return $this->pushResponse(
69
            Factory::response('', $status, $headers)
70
        );
71
    }
72
 
73
    /**
74
     * Push response with the contents of a file as the body to the sequence.
75
     *
76
     * @param  string  $filePath
77
     * @param  int  $status
78
     * @param  array  $headers
79
     * @return $this
80
     */
81
    public function pushFile(string $filePath, int $status = 200, array $headers = [])
82
    {
83
        $string = file_get_contents($filePath);
84
 
85
        return $this->pushResponse(
86
            Factory::response($string, $status, $headers)
87
        );
88
    }
89
 
90
    /**
91
     * Push a response to the sequence.
92
     *
93
     * @param  mixed  $response
94
     * @return $this
95
     */
96
    public function pushResponse($response)
97
    {
98
        $this->responses[] = $response;
99
 
100
        return $this;
101
    }
102
 
103
    /**
104
     * Make the sequence return a default response when it is empty.
105
     *
106
     * @param  \GuzzleHttp\Promise\PromiseInterface|\Closure  $response
107
     * @return $this
108
     */
109
    public function whenEmpty($response)
110
    {
111
        $this->failWhenEmpty = false;
112
        $this->emptyResponse = $response;
113
 
114
        return $this;
115
    }
116
 
117
    /**
118
     * Make the sequence return a default response when it is empty.
119
     *
120
     * @return $this
121
     */
122
    public function dontFailWhenEmpty()
123
    {
124
        return $this->whenEmpty(Factory::response());
125
    }
126
 
127
    /**
128
     * Indicate that this sequence has depleted all of its responses.
129
     *
130
     * @return bool
131
     */
132
    public function isEmpty()
133
    {
134
        return count($this->responses) === 0;
135
    }
136
 
137
    /**
138
     * Get the next response in the sequence.
139
     *
140
     * @return mixed
141
     *
142
     * @throws \OutOfBoundsException
143
     */
144
    public function __invoke()
145
    {
146
        if ($this->failWhenEmpty && count($this->responses) === 0) {
147
            throw new OutOfBoundsException('A request was made, but the response sequence is empty.');
148
        }
149
 
150
        if (! $this->failWhenEmpty && count($this->responses) === 0) {
151
            return value($this->emptyResponse ?? Factory::response());
152
        }
153
 
154
        return array_shift($this->responses);
155
    }
156
}