CristalTeam / php-api-wrapper

:rainbow: Work with APIs like with Laravel Eloquent or Doctrine (no longer a dream)
MIT License
117 stars 32 forks source link

Simple change to allow for APIs that do not singlarize/pluralize endpoints... #17

Closed empower-josh closed 3 years ago

empower-josh commented 3 years ago

I am using an API that uses the same endpoint for single entity references as it does for multiple entity references. IE. the endpoint is /users/ regardless of whether I am fetching one by ID, or all records. This seemed to pose a challenge for php-api-wrapper. I did find a simple fix though:

in Model.php:

    public function getEntities(): string
    {
+++        if ($this->doNotPluralize) {
+++            return $this->entity;
+++        }
        if (substr($this->entity, -1) === 'y') {
            return rtrim($this->entity, 'y').'ies';
        }

By adding this, then setting $entity = 'users', and adding 'protected $doNotPluralize = 1' to my model class, it keeps php-api-wrapper from munging the entity name when constructing the uri for the endpoint.

empower-josh commented 3 years ago

DOH. Turns out that it worked as expected by simply using the plural form form of the entity name without these changes.