snipe-it/app/Http/Transformers/PredefinedKitsTransformer.php

82 lines
2.4 KiB
PHP
Raw Normal View History

2018-10-31 06:06:38 -07:00
<?php
namespace App\Http\Transformers;
use App\Models\PredefinedKit;
use App\Models\SnipeModel;
use Gate;
use Illuminate\Database\Eloquent\Collection;
2018-10-31 06:06:38 -07:00
2019-02-23 11:44:03 -08:00
/**
* transforms collection of models to array with simple typres
*
* @author [D. Minaev] [<dmitriy.minaev.v@gmail.com>]
* @return array
*/
2018-10-31 06:06:38 -07:00
class PredefinedKitsTransformer
{
public function transformPredefinedKits (Collection $kits, $total)
{
$array = array();
foreach ($kits as $kit) {
$array[] = self::transformPredefinedKit($kit);
}
return (new DatatablesTransformer)->transformDatatables($array, $total);
}
public function transformPredefinedKit (PredefinedKit $kit)
{
$array = [
'id' => (int) $kit->id,
'name' => e($kit->name)
];
$permissions_array['available_actions'] = [
'update' => Gate::allows('update', PredefinedKit::class),
'delete' => Gate::allows('delete', PredefinedKit::class),
2018-11-13 09:33:32 -08:00
'checkout' => Gate::allows('checkout', PredefinedKit::class) ? true : false,
2018-10-31 06:06:38 -07:00
// 'clone' => Gate::allows('create', PredefinedKit::class),
// 'restore' => Gate::allows('create', PredefinedKit::class),
];
2018-11-13 09:33:32 -08:00
$array['user_can_checkout'] = true;
2018-10-31 06:06:38 -07:00
$array += $permissions_array;
return $array;
}
2019-02-23 11:44:03 -08:00
/**
* transform collection of any elemets attached to kit
* @return array
*/
2018-10-31 06:06:38 -07:00
public function transformElements(Collection $elements, $total) {
$array = array();
foreach ($elements as $element) {
$array[] = self::transformElement($element);
}
return (new DatatablesTransformer)->transformDatatables($array, $total);
}
public function transformElement(SnipeModel $element) {
$array = [
'id' => (int) $element->id,
'pivot_id' => (int) $element->pivot->id,
'owner_id' => (int) $element->pivot->kit_id,
'quantity' => (int) $element->pivot->quantity,
'name' => e($element->name)
];
$permissions_array['available_actions'] = [
'update' => Gate::allows('update', PredefinedKit::class),
'delete' => Gate::allows('delete', PredefinedKit::class),
];
$array += $permissions_array;
return $array;
}
public function transformPredefinedKitsDatatable($kits) {
return (new DatatablesTransformer)->transformDatatables($kits);
}
}