Weble / RevisoApi

5 stars 3 forks source link

Retrive/Search Provinces failes #22

Open kamyweb opened 5 days ago

kamyweb commented 5 days ago

webleit/revisoapi version 2.0.0 with illuminate/collections v8.83.27 on PHP 8.1.27

$reviso->provinces->get();

launch an exception:

PHP Fatal error:  Uncaught Error: Object of class stdClass could not be converted to string in /opt/project/vendor/illuminate/collections/Collection.php:508
Stack trace:
#0 /opt/project/vendor/webleit/revisoapi/src/Collection.php(17): Illuminate\Support\Collection->keyBy(Object(Closure))
#1 /opt/project/vendor/webleit/revisoapi/src/Endpoint/ListEndpoint.php(48): Weble\RevisoApi\Collection::create(Object(Weble\RevisoApi\Client), Object(stdClass), 'countryCode')
#2 /opt/project/vendor/webleit/revisoapi/src/Endpoint/Endpoint.php(43): Weble\RevisoApi\Endpoint\ListEndpoint->get()

Problem is inside illuminate/collections/Collection.php on function keyBy that try to cast the StdClass object as string.

public function keyBy($keyBy)
    {
        $keyBy = $this->valueRetriever($keyBy);

        $results = [];

        foreach ($this->items as $key => $item) {
            $resolvedKey = $keyBy($item, $key);

            if (is_object($resolvedKey)) {
                $resolvedKey = (string) $resolvedKey;
            }

            $results[$resolvedKey] = $item;
        }

        return new static($results);
    }

The json received from reviso is the following:

{
  "collection" : [ {
    "code" : "1",
    "name" : "Araba/Álava",
    "countryCode" : {
      "code" : "ES",
      "self" : "https://rest.reviso.com/country-codes/ES"
    },
    "provinceNumber" : 1,
    "self" : "https://rest.reviso.com/provinces/ES/1"
  }, ..... ],
  "metaData" : { },
  "pagination" : {
    "maxPageSizeAllowed" : 10000,
    "pageSize" : 20,
    "skipPages" : 0,
    "results" : 166,
    "resultsWithoutFilter" : 166,
    "firstPage" : "https://rest.reviso.com/provinces?skippages=0&pagesize=20",
    "lastPage" : "https://rest.reviso.com/provinces?skippages=8&pagesize=20",
    "nextPage" : "https://rest.reviso.com/provinces?skippages=1&pagesize=20"
  },
  "self" : "https://rest.reviso.com/provinces?skippages=0&pagesize=20"
}

The same if I try to filter/search a single province. $reviso->provinces->where('code', '=', 'MI')->get();

How can I retrive information for this endpoint?

Skullbock commented 4 days ago

Hi, that's an endpoint we don't yet use. We deduce how to read from the api endpoints from the json resources schema reviso provides, and automatically deduce how they work

Provinces is an experimental endpoint according to reviso, and it does seem that it doesn't follow the rest of the endpoints. If you can / want you can PR a fix. This consists in creating a dedicated Endpoint class that fixes the differences, usually either the route lists method or the resource key one.

You can check the ProjectsEndpoint class for an example