chadicus /
marvel-api-client
These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
| 1 | <?php |
||
| 2 | namespace Chadicus\Marvel\Api\Assets; |
||
| 3 | |||
| 4 | use Chadicus\Marvel\Api\Adapter\AdapterInterface; |
||
| 5 | use Chadicus\Marvel\Api\Client; |
||
| 6 | use Chadicus\Marvel\Api\RequestInterface; |
||
| 7 | use Chadicus\Marvel\Api\Response; |
||
| 8 | |||
| 9 | /** |
||
| 10 | * Adapter that returns multiple items. |
||
| 11 | */ |
||
| 12 | final class CollectionAdapter implements AdapterInterface |
||
| 13 | { |
||
| 14 | /** |
||
| 15 | * Simulate sending a request to the API. |
||
| 16 | * |
||
| 17 | * @param RequestInterface $request The request. |
||
| 18 | * |
||
| 19 | * @return ResponseInterface |
||
| 20 | */ |
||
| 21 | public function send(RequestInterface $request) |
||
| 22 | { |
||
| 23 | $allResults = [ |
||
| 24 | ['id' => 0, 'title' => 'a title for comic 0', 'resourceURI' => Client::BASE_URL . 'comics/0'], |
||
| 25 | ['id' => 1, 'title' => 'a title for comic 1', 'resourceURI' => Client::BASE_URL . 'comics/1'], |
||
| 26 | ['id' => 2, 'title' => 'a title for comic 2', 'resourceURI' => Client::BASE_URL . 'comics/2'], |
||
| 27 | ['id' => 3, 'title' => 'a title for comic 3', 'resourceURI' => Client::BASE_URL . 'comics/3'], |
||
| 28 | ['id' => 4, 'title' => 'a title for comic 4', 'resourceURI' => Client::BASE_URL . 'comics/4'], |
||
| 29 | ]; |
||
| 30 | |||
| 31 | $queryString = parse_url($request->getUrl(), PHP_URL_QUERY); |
||
| 32 | $queryParams = []; |
||
| 33 | parse_str($queryString, $queryParams); |
||
| 34 | |||
| 35 | $offset = (int)$queryParams['offset']; |
||
| 36 | $limit = (int)$queryParams['limit']; |
||
| 37 | $results = array_slice($allResults, $offset, $limit); |
||
| 38 | $count = count($results); |
||
| 39 | return new Response( |
||
|
0 ignored issues
–
show
|
|||
| 40 | 200, |
||
| 41 | ['Content-type' => 'application/json', 'etag' => 'an etag'], |
||
| 42 | [ |
||
| 43 | 'code' => 200, |
||
| 44 | 'status' => 'ok', |
||
| 45 | 'etag' => 'an etag', |
||
| 46 | 'data' => [ |
||
| 47 | 'offset' => $offset, |
||
| 48 | 'limit' => $limit, |
||
| 49 | 'total' => 5, |
||
| 50 | 'count' => $count, |
||
| 51 | 'results' => $results, |
||
| 52 | ], |
||
| 53 | ] |
||
| 54 | ); |
||
| 55 | } |
||
| 56 | } |
||
| 57 |
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_functionexpects aPostobject, and outputs the author of the post. The base classPostreturns a simple string and outputting a simple string will work just fine. However, the child classBlogPostwhich is a sub-type ofPostinstead decided to return anobject, and is therefore violating the SOLID principles. If aBlogPostwere passed tomy_function, PHP would not complain, but ultimately fail when executing thestrtouppercall in its body.