invoiceninja/app/Transformers/ProductTransformer.php

81 lines
2.2 KiB
PHP
Raw Normal View History

2019-04-03 11:09:22 +11:00
<?php
2019-05-11 13:32:07 +10:00
/**
* Invoice Ninja (https://invoiceninja.com)
*
* @link https://github.com/invoiceninja/invoiceninja source repository
*
* @copyright Copyright (c) 2019. Invoice Ninja LLC (https://invoiceninja.com)
*
* @license https://opensource.org/licenses/AAL
*/
2019-04-03 11:09:22 +11:00
namespace App\Transformers;
2019-04-03 12:17:21 +11:00
use App\Models\Company;
2019-04-03 11:09:22 +11:00
use App\Models\Product;
2019-04-03 12:17:21 +11:00
use App\Models\User;
2019-04-03 11:09:22 +11:00
use App\Utils\Traits\MakesHash;
class ProductTransformer extends EntityTransformer
{
use MakesHash;
2019-04-03 12:17:21 +11:00
protected $defaultIncludes = [
];
/**
* @var array
*/
protected $availableIncludes = [
'company',
'user'
];
/**
* @param Product $product
*
* @return \League\Fractal\Resource\Collection
*/
public function includeUser(Product $product)
{
$transformer = new UserTransformer($this->serializer);
return $this->includeItem($product->user, $transformer, User::class);
}
/**
* @param Product $product
*
* @return \League\Fractal\Resource\Collection
*/
public function includeCompany(Product $product)
{
$transformer = new CompanyTransformer($this->serializer);
return $this->includeItem($product->company, $transformer, Company::class);
}
2019-04-03 11:09:22 +11:00
public function transform(Product $product)
{
return [
'id' => $this->encodePrimaryKey($product->id),
'product_key' => $product->product_key,
'notes' => $product->notes,
'cost' => (float) $product->cost,
2019-09-04 22:01:19 +10:00
'quantity' => (float) ($product->quantity ?: 0.0),
2019-04-03 11:09:22 +11:00
'tax_name1' => $product->tax_name1 ?: '',
'tax_rate1' => (float) $product->tax_rate1,
'tax_name2' => $product->tax_name2 ?: '',
'tax_rate2' => (float) $product->tax_rate2,
'updated_at' => $product->updated_at,
'archived_at' => $product->deleted_at,
'custom_value1' => $product->custom_value1 ?: '',
'custom_value2' => $product->custom_value2 ?: '',
'custom_value3' => $product->custom_value2 ?: '',
'custom_value4' => $product->custom_value2 ?: '',
'is_deleted' => (bool) $product->is_deleted,
];
}
}