php-api-wrapper icon indicating copy to clipboard operation
php-api-wrapper copied to clipboard

Problem with home made wrappers

Open camillebaronnet opened this issue 4 years ago • 3 comments

https://github.com/CristalTeam/php-api-wrapper/blob/abd025cd9f4388652089d3063cd70972d1a73108/src/Model.php#L76

When your implement a wrapper without any dependencies with the core API, we cannot inject this instance into the Model.

Two ways to resove it :

  • Have a public static function setApi(\stdClass $api) interface instead of
  • Or create a specific interface to type any API Wrapper

camillebaronnet avatar Apr 08 '20 14:04 camillebaronnet

Another way is to rework the Wrapper by supporting a pattern like the Repository/Paginator inspired by the Symfony Bridge instead of "Api Wrapper". Repository and Wrapper are very similar.

By using a Repository instead of Wrapper. Each repositories can be abstract and contractualized by an interface that would look like this:

interface Repository
{
    public function findOne($key);

    public function findBy(array $params): Paginator;

    public function update($key, array $attributes): array;

    public function create($attributes): array;

    public function delete($key);
}

camillebaronnet avatar Jul 02 '20 11:07 camillebaronnet

When your implement a wrapper without any dependencies with the core API, we cannot inject this instance into the Model.

I solved this by overload the __construct() on a base wrapper:

use Cristal\ApiWrapper\Bridges\Laravel\Model;

class BaseApiModel extends Model
{
    public function __construct($fill = [], $exists = false)
    {
        parent::__construct($fill, $exists);
        self::setApi(app(MyApiRepository::class));
    }
}

guice avatar Jul 21 '20 19:07 guice

@guice I know this is an old post but I just came across it and it helped me clean up some hacky code so I wanted to say thank you.

jorbascrumps avatar Dec 14 '22 03:12 jorbascrumps