1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Cerbero\FluentApi; |
4
|
|
|
|
5
|
|
|
use Cerbero\FluentApi\Requests\Request; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* Abstract implementation of an API resource. |
9
|
|
|
* |
10
|
|
|
*/ |
11
|
|
|
abstract class AbstractResource extends VersionableRequestMaker |
12
|
|
|
{ |
13
|
|
|
/** |
14
|
|
|
* The resource HTTP verb. |
15
|
|
|
* |
16
|
|
|
* @var string|null |
17
|
|
|
*/ |
18
|
|
|
protected $verb; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* The HTTP call options. |
22
|
|
|
* |
23
|
|
|
* @var array |
24
|
|
|
*/ |
25
|
|
|
protected $options = []; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Retrieve the resource endpoint. |
29
|
|
|
* |
30
|
|
|
* @return string |
31
|
|
|
*/ |
32
|
|
|
abstract public function getEndpoint(); |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Retrieve the request to pass through resources. |
36
|
|
|
* |
37
|
|
|
* @return Cerbero\FluentApi\Requests\Request |
38
|
|
|
*/ |
39
|
|
|
public function getRequest() |
40
|
|
|
{ |
41
|
|
|
return $this->request; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Update the given request with the resource options. |
46
|
|
|
* |
47
|
|
|
* @param Cerbero\FluentApi\Requests\Request $request |
48
|
|
|
* @return Cerbero\FluentApi\Requests\Request |
49
|
|
|
*/ |
50
|
|
|
public function updateOptions(Request $request) |
51
|
|
|
{ |
52
|
|
|
return $this->request = $request->setOptions($this->getOptions()); |
|
|
|
|
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Fill the given request with the resource details. |
57
|
|
|
* |
58
|
|
|
* @param Cerbero\FluentApi\Requests\Request $request |
59
|
|
|
* @return Cerbero\FluentApi\Requests\Request |
60
|
|
|
*/ |
61
|
|
|
public function fillRequest(Request $request) |
62
|
|
|
{ |
63
|
|
|
return $this->request = $this->updateOptions($request) |
|
|
|
|
64
|
|
|
->setVerb($this->getVerb()) |
65
|
|
|
->setEndpoint($this->getEndpoint()); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* Retrieve the resource HTTP verb. |
70
|
|
|
* |
71
|
|
|
* @return string |
72
|
|
|
*/ |
73
|
|
|
public function getVerb() |
74
|
|
|
{ |
75
|
|
|
return $this->verb; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* Set the HTTP call options. |
80
|
|
|
* |
81
|
|
|
* @param array $options |
82
|
|
|
* @return $this |
83
|
|
|
*/ |
84
|
|
|
public function setOptions(array $options) |
85
|
|
|
{ |
86
|
|
|
$this->options = array_merge_recursive($this->getOptions(), $options); |
87
|
|
|
|
88
|
|
|
return $this; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* Retrieve the HTTP call options. |
93
|
|
|
* |
94
|
|
|
* @return array |
95
|
|
|
*/ |
96
|
|
|
public function getOptions() |
97
|
|
|
{ |
98
|
|
|
return $this->options; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* Set a singular HTTP call option. |
103
|
|
|
* |
104
|
|
|
* @param string $option |
105
|
|
|
* @param mixed $value |
106
|
|
|
* @return $this |
107
|
|
|
*/ |
108
|
|
|
public function setOption($option, $value) |
109
|
|
|
{ |
110
|
|
|
if (is_array($value)) { |
111
|
|
|
$value = array_merge((array) $this->getOption($option), $value); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
$this->options[$option] = $value; |
115
|
|
|
|
116
|
|
|
return $this; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* Retrieve a singular HTTP call option. |
121
|
|
|
* |
122
|
|
|
* @param string $option |
123
|
|
|
* @return mixed |
124
|
|
|
*/ |
125
|
|
|
public function getOption($option) |
126
|
|
|
{ |
127
|
|
|
if (isset($this->options[$option])) { |
128
|
|
|
return $this->options[$option]; |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
return null; |
132
|
|
|
} |
133
|
|
|
} |
134
|
|
|
|
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..