1 | <?php |
||
11 | class PageApi |
||
12 | { |
||
13 | /** |
||
14 | * Guzzle HTTP interface. |
||
15 | * |
||
16 | * @var \GuzzleHTTP\Client |
||
17 | */ |
||
18 | protected $http; |
||
19 | |||
20 | /** |
||
21 | * A cache adapter for storing results. |
||
22 | * |
||
23 | * @var \Journey\Cache\CacheAdapterInterface |
||
24 | */ |
||
25 | protected $cache; |
||
26 | |||
27 | /** |
||
28 | * Number of seconds responses should be cached for. |
||
29 | * |
||
30 | * @var integer |
||
31 | */ |
||
32 | protected $ttl; |
||
33 | |||
34 | /** |
||
35 | * Initialize the page api methods. |
||
36 | */ |
||
37 | 7 | public function __construct(RequestInterface $request, array $config) |
|
48 | |||
49 | /** |
||
50 | * Get a particular page from the api. |
||
51 | * |
||
52 | * @param string $path path of the page |
||
53 | * @return array|false |
||
54 | */ |
||
55 | 4 | public function getPage($path) |
|
59 | |||
60 | /** |
||
61 | * Get a particular page from the api. |
||
62 | * |
||
63 | * @param array $ids unique ids of pages to fetch data for. |
||
64 | * @return array|false |
||
65 | */ |
||
66 | public function getPagesById($ids) |
||
73 | |||
74 | /** |
||
75 | * Get a list of pages of a particular type from the api. |
||
76 | * |
||
77 | * @param string $type type of pages to get. |
||
78 | * @return array |
||
79 | */ |
||
80 | public function getPagesByType($type) |
||
84 | |||
85 | /** |
||
86 | * Clear the cache of a given endpoint immediately. |
||
87 | * |
||
88 | * @param string $method get/post/put |
||
89 | * @param string $url endpoint to hit |
||
90 | * @return $this |
||
91 | */ |
||
92 | public function clearEndpoint($method, $url) |
||
98 | |||
99 | /** |
||
100 | * Call a particular api endpoint. |
||
101 | * |
||
102 | * @return \Psr\Http\Message\ResponseInterface |
||
103 | */ |
||
104 | 7 | public function call($method, $url) |
|
123 | } |
||
124 |
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.