Open camillebaronnet opened 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);
}
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 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.
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 :
public static function setApi(\stdClass $api)
interface instead of