Completed
Push — options ( 9b8513 )
by Akihito
02:57
created

AbstractRequest::serialize()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 0
cts 2
cp 0
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
crap 2
1
<?php
2
/**
3
 * This file is part of the BEAR.Resource package.
4
 *
5
 * @license http://opensource.org/licenses/MIT MIT
6
 */
7
namespace BEAR\Resource;
8
9
use BEAR\Resource\Exception\MethodException;
10
use BEAR\Resource\Exception\OutOfBoundsException;
11
12
abstract class AbstractRequest implements RequestInterface, \ArrayAccess, \IteratorAggregate, \Serializable
13
{
14
    const GET = 'get';
15
    const POST = 'post';
16
    const PUT = 'put';
17
    const PATCH = 'patch';
18
    const DELETE = 'delete';
19
    const HEAD = 'head';
20
    const OPTIONS = 'options';
21
22
    /**
23
     * URI
24
     *
25
     * @var string
26
     */
27
    public $uri;
28
29
    /**
30
     * Resource object
31
     *
32
     * @var \BEAR\Resource\ResourceObject
33
     */
34
    public $resourceObject;
35
36
    /**
37
     * Method
38
     *
39
     * @var string
40
     */
41
    public $method = '';
42
43
    /**
44
     * Query
45
     *
46
     * @var array
47
     */
48
    public $query = [];
49
50
    /**
51
     * Options
52
     *
53
     * @var array
54
     */
55
    public $options = [];
56
57
    /**
58
     * Request option (eager or lazy)
59
     *
60
     * @var string
61
     */
62
    public $in;
63
64
    /**
65
     * Links
66
     *
67
     * @var \BEAR\Resource\LinkType[]
68
     */
69
    public $links = [];
70
71
    /**
72
     * Request Result
73
     *
74
     * @var ResourceObject
75
     */
76
    protected $result;
77
78
    /**
79
     * @var InvokerInterface
80
     */
81
    protected $invoker;
82
83
    /**
84
     * @var LinkerInterface
85
     */
86
    private $linker;
87
88
    /**
89
     * @param InvokerInterface     $invoker
90
     * @param ResourceObject|null  $ro
91
     * @param string               $method
92
     * @param array                $query
93
     * @param array                $links
94
     * @param LinkerInterface|null $linker
95
     *
96
     * @throws MethodException
97
     */
98 66
    public function __construct(
99
        InvokerInterface $invoker,
100
        ResourceObject $ro = null,
101
        $method = Request::GET,
102
        array $query = [],
103
        array $links = [],
104
        LinkerInterface $linker = null
105
    ) {
106 66
        $this->invoker = $invoker;
107 66
        $this->resourceObject = $ro;
108 66
        if (! in_array(strtolower($method), [self::GET, self::POST, self::PUT, self::PATCH, self::DELETE, self::HEAD, self::OPTIONS], true)) {
109 1
            throw new MethodException($method, 400);
110
        }
111 66
        $this->method = $method;
112 66
        $this->query = $query;
113 66
        $this->links = $links;
114 66
        $this->linker = $linker;
115 66
    }
116
117
    /**
118
     * @return string
119
     */
120 6
    public function __toString()
121
    {
122
        try {
123 6
            $this->invoke();
124
125 5
            return (string) $this->result;
126 1
        } catch (\Exception $e) {
127 1
            error_log($e);
128
129 1
            return '';
130
        }
131
    }
132
133
    /**
134
     * {@inheritdoc}
135
     */
136 36
    public function __invoke(array $query = null)
137
    {
138 36
        if ($query !== null) {
139 6
            $this->query = array_merge($this->query, $query);
140
        }
141 36
        $result = $this->linker ? $result = $this->linker->invoke($this) : $this->invoker->invoke($this);
142
143 34
        return $result;
144
    }
145
146
    /**
147
     *{@inheritdoc}
148
     *
149
     * @throws OutOfBoundsException
150
     */
151 1
    public function offsetSet($offset, $value)
152
    {
153 1
        throw new OutOfBoundsException(__METHOD__ . ' is unavailable.', 400);
154
    }
155
156
    /**
157
     * {@inheritdoc}
158
     *
159
     * @throws OutOfBoundsException
160
     */
161 1
    public function offsetUnset($offset)
162
    {
163 1
        unset($offset);
164 1
        throw new OutOfBoundsException(__METHOD__ . ' is unavailable.', 400);
165
    }
166
167
    /**
168
     * {@inheritdoc}
169
     */
170 20
    public function request()
171
    {
172 20
        if ($this->in !== 'eager') {
173 8
            return $this;
174
        }
175 13
        $this->result = $this->invoke();
176
177 12
        return $this->result;
178
    }
179
180
    /**
181
     * {@inheritdoc}
182
     *
183
     * @throws OutOfBoundsException
184
     */
185 2
    public function offsetGet($offset)
186
    {
187 2
        $this->invoke();
188 2
        if (! isset($this->result->body[$offset])) {
189 1
            throw new OutOfBoundsException("[$offset] for object[" . get_class($this->result) . ']', 400);
190
        }
191
192 1
        return $this->result->body[$offset];
193
    }
194
195
    /**
196
     * {@inheritdoc}
197
     */
198 2
    public function offsetExists($offset)
199
    {
200 2
        $this->invoke();
201
202 2
        return isset($this->result->body[$offset]);
203
    }
204
205
    /**
206
     * {@inheritdoc}
207
     */
208 1
    public function getIterator()
209
    {
210 1
        $this->invoke();
211 1
        $isArray = (is_array($this->result->body) || $this->result->body instanceof \Traversable);
212 1
        $iterator = $isArray ? new \ArrayIterator($this->result->body) : new \ArrayIterator([]);
213
214 1
        return $iterator;
215
    }
216
217
    /**
218
     * {@inheritdoc}
219
     */
220 2
    public function hash()
221
    {
222 2
        return md5(get_class($this->resourceObject) . $this->method . serialize($this->query) . serialize($this->links));
223
    }
224
225
    /**
226
     * @return ResourceObject
227
     */
228 24
    private function invoke()
229
    {
230 24
        if ($this->result === null) {
231
            /* @noinspection ImplicitMagicMethodCallInspection */
232 24
            $this->result = $this->__invoke();
233
        }
234
235 22
        return $this->result;
236
    }
237
238
    /**
239
     * @inheritDoc
240
     */
241
    public function serialize()
242
    {
243
        return serialize($this->invoke());
244
    }
245
246
    /**
247
     * @inheritDoc
248
     */
249
    public function unserialize($serialized)
250
    {
251
        return unserialize($serialized);
252
    }
253
254
}
255