megawubs / trakt-api-wrapper

A Object Oriented PHP wrapper for the trakt api
34 stars 19 forks source link

Shows summary or get return a array with 1 index #27

Closed ghost closed 8 years ago

ghost commented 8 years ago

This is a question.

Why summary function return a single element array and not an object ?

fyi Documentation

megawubs commented 8 years ago

That's because the Wubs\Trakt\Response\Handlers\DefaultResponseHandler has the following handle method:

<?php
  /**
     * @param ResponseInterface $response
     * @param ClientInterface|GuzzleHttp\ClientInterface $client
     * @return Collection
     * @internal param ClientInterface $client
     */
public function handle(ResponseInterface $response, \GuzzleHttp\ClientInterface $client)
    {
        $json = $this->getJson($response);

       $json = $this->getJson($response);
        return (is_array($json)) ? collect($json) : collect([$json]);
    }

The DefaultResponseHandler class should always return a collection in order to adhere to it's return type and prevent unwanted results. If you expect a collection in consuming code but get an object, your code will break. Allways returning a collection, even with one item, will ensure polymorphism, aka, it can be used anywhere and give the expected result.