Last active
June 16, 2021 03:21
-
-
Save basherr/338f1ca5e96141f9c0e9934be59f344e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App\Connectors\Zest\ApiModels; | |
use App\ApiModels\Category as BaseCategory; | |
/** | |
* class Category | |
* | |
* Convert API response to App\ApiModels\Category | |
* | |
* @package App\Connectors\Zest\ApiModels | |
*/ | |
class Category extends BaseCategory | |
{ | |
/** | |
* Fill the class properties using API response | |
* | |
* @param \Illuminate\Support\Collection $category | |
* @return $this | |
*/ | |
public function hydrate($category) | |
{ | |
return $this->setCode($category->get('code')) | |
->setName($category->get('name')) | |
->setImage($category->get('image')) | |
->setLink($category->get('link')) | |
->setDescription($category->get('description')); | |
} | |
} | |
?> | |
///////////////////////////// BASE CATEGORY API MODEL ////////////////////////////// | |
<?php | |
namespace App\ApiModels; | |
/** | |
* class Category | |
* | |
* Represent the Category type for the shopbot | |
* | |
* @package App\ApiModels | |
*/ | |
abstract class Category extends BaseApiModel | |
{ | |
/** | |
* @var string | |
*/ | |
protected $code; | |
/** | |
* @var string | |
*/ | |
protected $name; | |
/** | |
* @var string | |
*/ | |
protected $image; | |
/** | |
* @var string | |
*/ | |
protected $link; | |
/** | |
* @var string | |
*/ | |
protected $description; | |
/** | |
* @var float|string | |
*/ | |
protected $total; | |
/** | |
* @var App\ApiModels\ProductsCollection | |
*/ | |
protected $products; | |
} | |
?> | |
///////////////////////// THE DEFAULT BASE API MODEL FOR ALL /////////////////////////////////// | |
<?php | |
namespace App\ApiModels; | |
use App\Traits\CanAccessProperties; | |
/** | |
* class BaseApiModel | |
* | |
* The base class for all the models | |
* | |
* @package App\ApiModels | |
*/ | |
abstract class BaseApiModel | |
{ | |
use CanAccessProperties; | |
/** | |
* Create an object from API response | |
* | |
* @param mixed $incoming | |
* @return mixed | |
*/ | |
public abstract function hydrate($incoming); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I think we should have a couple of extra methods in
AbstractApiModel
:set
- lets you set multiple properties in one gocopyProperties
- receives an instance & a list of property names, and mirrors those properties out of the passed instanceYour
hydrate
docs should be updated to return$this
so it can be chained.Now your hydrate can look like this:
This makes the code easier to read & cuts out all the boilerplate