65 lines
1.6 KiB
PHP
Raw Normal View History

2017-09-14 22:21:00 +03:00
<?php
2018-06-10 02:48:51 +03:00
namespace App\Transformers\Common;
2017-09-14 22:21:00 +03:00
2017-10-16 21:02:32 +03:00
use App\Transformers\Setting\Category;
use App\Transformers\Setting\Tax;
2018-06-10 02:48:51 +03:00
use App\Models\Common\Item as Model;
2017-09-14 22:21:00 +03:00
use League\Fractal\TransformerAbstract;
class Item extends TransformerAbstract
{
/**
* @var array
*/
2020-12-24 01:28:38 +03:00
protected $defaultIncludes = ['taxes', 'category'];
2017-09-14 22:21:00 +03:00
/**
2017-10-09 11:59:58 +03:00
* @param Model $model
2017-09-14 22:21:00 +03:00
* @return array
*/
public function transform(Model $model)
{
return [
'id' => $model->id,
'company_id' => $model->company_id,
'name' => $model->name,
'description' => $model->description,
'sale_price' => $model->sale_price,
'purchase_price' => $model->purchase_price,
'category_id' => $model->category_id,
2020-12-24 01:28:38 +03:00
'tax_ids' => $model->tax_ids,
2017-09-14 22:21:00 +03:00
'picture' => $model->picture,
'enabled' => $model->enabled,
2019-11-16 10:21:14 +03:00
'created_at' => $model->created_at ? $model->created_at->toIso8601String() : '',
'updated_at' => $model->updated_at ? $model->updated_at->toIso8601String() : '',
2017-09-14 22:21:00 +03:00
];
}
/**
2017-10-09 11:59:58 +03:00
* @param Model $model
* @return mixed
2017-09-14 22:21:00 +03:00
*/
2020-12-24 01:28:38 +03:00
public function includeTaxes(Model $model)
2017-09-14 22:21:00 +03:00
{
2020-12-24 01:28:38 +03:00
if (!$model->taxes) {
2017-10-09 11:59:58 +03:00
return $this->null();
}
2021-01-02 11:53:48 +03:00
return $this->collection($model->taxes, new Tax());
2017-09-14 22:21:00 +03:00
}
/**
2017-10-09 11:59:58 +03:00
* @param Model $model
* @return mixed
2017-09-14 22:21:00 +03:00
*/
public function includeCategory(Model $model)
{
2017-10-09 11:59:58 +03:00
if (!$model->category) {
return $this->null();
}
2017-09-14 22:21:00 +03:00
return $this->item($model->category, new Category());
}
}