1 | <?php |
||
26 | abstract class ApiClientContext implements ApiClientContextInterface |
||
27 | { |
||
28 | /** |
||
29 | * @var string |
||
30 | */ |
||
31 | protected $baseUri; |
||
32 | |||
33 | /** |
||
34 | * @var ClientInterface |
||
35 | */ |
||
36 | private $client; |
||
37 | |||
38 | /** |
||
39 | * @var array |
||
40 | */ |
||
41 | private $headers = []; |
||
42 | |||
43 | /** |
||
44 | * @var array |
||
45 | */ |
||
46 | private $placeHolders = []; |
||
47 | |||
48 | /** |
||
49 | * @var RequestInterface |
||
50 | */ |
||
51 | private $request; |
||
52 | |||
53 | /** |
||
54 | * @var ResponseInterface |
||
55 | */ |
||
56 | private $response; |
||
57 | |||
58 | public function setBaseUri(string $baseUri): ApiClientContextInterface |
||
64 | |||
65 | public function setClient(ClientInterface $client): ApiClientContextInterface |
||
71 | |||
72 | protected function getHeaders(): array |
||
76 | |||
77 | protected function addHeader(string $name, string $value): ApiClientContextInterface |
||
102 | |||
103 | protected function removeHeader(string $name): ApiClientContextInterface |
||
111 | |||
112 | /** |
||
113 | * Add place holder for replacement. |
||
114 | * |
||
115 | * you can specify placeholders, which will |
||
116 | * be replaced in URL, request or response body. |
||
117 | */ |
||
118 | protected function addPlaceholder(string $key, string $value): ApiClientContextInterface |
||
124 | |||
125 | /** |
||
126 | * Removes a placeholder identified by $key. |
||
127 | */ |
||
128 | protected function removePlaceHolder(string $key): ApiClientContext |
||
136 | |||
137 | protected function getRequest(): RequestInterface |
||
141 | |||
142 | /** |
||
143 | * @return ResponseInterface |
||
144 | */ |
||
145 | protected function getResponse() |
||
149 | |||
150 | /** |
||
151 | * @throws PsrClientExceptionInterface |
||
152 | * @throws ClientExceptionInterface |
||
153 | */ |
||
154 | protected function sendRequest(string $method, string $uri, array $headers = [], ?string $body = null) |
||
168 | |||
169 | /** |
||
170 | * Replaces placeholders in provided text. |
||
171 | */ |
||
172 | protected function replacePlaceHolder(string $string): string |
||
180 | |||
181 | /** |
||
182 | * Prepare URL by replacing placeholders and trimming slashes. |
||
183 | */ |
||
184 | protected function prepareUrl(string $url): string |
||
188 | |||
189 | protected function getClient(): ClientInterface |
||
197 | } |
||
198 |
If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.
Let’s take a look at an example:
Our function
my_function
expects aPost
object, and outputs the author of the post. The base classPost
returns a simple string and outputting a simple string will work just fine. However, the child classBlogPost
which is a sub-type ofPost
instead decided to return anobject
, and is therefore violating the SOLID principles. If aBlogPost
were passed tomy_function
, PHP would not complain, but ultimately fail when executing thestrtoupper
call in its body.