CristalTeam / php-api-wrapper

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

Problem with home made wrappers #3

Open camillebaronnet opened 4 years ago

camillebaronnet commented 4 years ago

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 :

camillebaronnet commented 4 years ago

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);
}
guice commented 4 years ago

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));
    }
}
jorbascrumps commented 1 year ago

@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.