Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Problem with home made wrappers #3

Open
camillebaronnet opened this issue Apr 8, 2020 · 3 comments
Open

Problem with home made wrappers #3

camillebaronnet opened this issue Apr 8, 2020 · 3 comments
Labels
bug Something isn't working good first issue Good for newcomers

Comments

@camillebaronnet
Copy link
Contributor

public static function setApi(Api $api)

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 camillebaronnet changed the title Problem with homemade wrapper Problem with home made wrappers Apr 8, 2020
@camillebaronnet camillebaronnet added the bug Something isn't working label Apr 8, 2020
@camillebaronnet camillebaronnet added the good first issue Good for newcomers label Apr 22, 2020
@camillebaronnet
Copy link
Contributor Author

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
Copy link

guice commented Jul 21, 2020

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
Copy link

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working good first issue Good for newcomers
Projects
None yet
Development

No branches or pull requests

3 participants