| 1 | <?php |
||
| 2 | |||
| 3 | namespace VGirol\JsonApiFaker\Laravel\Factory; |
||
| 4 | |||
| 5 | use Illuminate\Database\Eloquent\Model; |
||
| 6 | use VGirol\JsonApiFaker\Exception\JsonApiFakerException; |
||
| 7 | |||
| 8 | /** |
||
| 9 | * An abstract factory for resources (resource object or resource identifer). |
||
| 10 | */ |
||
| 11 | trait IsResource |
||
| 12 | { |
||
| 13 | 1 | use HasModel; |
|
| 14 | |||
| 15 | /** |
||
| 16 | * Class constructor. |
||
| 17 | * |
||
| 18 | * @param Model|null $model |
||
| 19 | * @param string|null $resourceType |
||
| 20 | * |
||
| 21 | * @return void |
||
| 22 | * @throws JsonApiFakerException |
||
| 23 | */ |
||
| 24 | 60 | public function __construct($model = null, ?string $resourceType = null) |
|
| 25 | { |
||
| 26 | 60 | if (($model !== null) && ($resourceType !== null)) { |
|
| 27 | 45 | $this->setValues($model, $resourceType); |
|
| 28 | } |
||
| 29 | 60 | } |
|
| 30 | |||
| 31 | /** |
||
| 32 | * Set the model and the resource type. |
||
| 33 | * |
||
| 34 | * @param Model $model |
||
| 35 | * @param string $resourceType |
||
| 36 | * |
||
| 37 | * @return static |
||
| 38 | * @throws JsonApiFakerException |
||
| 39 | */ |
||
| 40 | 54 | public function setValues($model, string $resourceType) |
|
| 41 | { |
||
| 42 | 54 | return $this->setModel($model) |
|
| 43 | 51 | ->setId($model->getKey()) |
|
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||
| 44 | 51 | ->setResourceType($resourceType); |
|
| 45 | } |
||
| 46 | } |
||
| 47 |