mirror of
https://github.com/snipe/snipe-it.git
synced 2024-12-30 07:59:50 -08:00
934afa036f
Shift automatically applies the Laravel coding style - which uses the PSR-2 coding style as a base with some minor additions. You may customize the adopted coding style by adding your own [PHP CS Fixer][1] `.php_cs` config file to your project root. Feel free to use [Shift's Laravel ruleset][2] to help you get started. [1]: https://github.com/FriendsOfPHP/PHP-CS-Fixer [2]: https://gist.github.com/laravel-shift/cab527923ed2a109dda047b97d53c200
76 lines
2.4 KiB
PHP
76 lines
2.4 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Transformers;
|
|
|
|
use App\Helpers\Helper;
|
|
use App\Models\CustomField;
|
|
use Illuminate\Database\Eloquent\Collection;
|
|
|
|
class CustomFieldsTransformer
|
|
{
|
|
public function transformCustomFields(Collection $fields, $total)
|
|
{
|
|
$array = [];
|
|
foreach ($fields as $field) {
|
|
$array[] = self::transformCustomField($field);
|
|
}
|
|
|
|
return (new DatatablesTransformer)->transformDatatables($array, $total);
|
|
}
|
|
|
|
/**
|
|
* Builds up an array of formatted custom fields
|
|
* @param Collection $fields
|
|
* @param int $modelId
|
|
* @param int $total
|
|
* @return array
|
|
*/
|
|
public function transformCustomFieldsWithDefaultValues(Collection $fields, $modelId, $total)
|
|
{
|
|
$array = [];
|
|
|
|
foreach ($fields as $field) {
|
|
$array[] = self::transformCustomFieldWithDefaultValue($field, $modelId);
|
|
}
|
|
|
|
return (new DatatablesTransformer)->transformDatatables($array, $total);
|
|
}
|
|
|
|
public function transformCustomField(CustomField $field)
|
|
{
|
|
$array = [
|
|
'id' => $field->id,
|
|
'name' => e($field->name),
|
|
'db_column_name' => e($field->db_column_name()),
|
|
'format' => e($field->format),
|
|
'field_values' => ($field->field_values) ? e($field->field_values) : null,
|
|
'field_values_array' => ($field->field_values) ? explode("\r\n", e($field->field_values)) : null,
|
|
'type' => e($field->element),
|
|
'required' => $field->pivot ? $field->pivot->required : false,
|
|
'created_at' => Helper::getFormattedDateObject($field->created_at, 'datetime'),
|
|
'updated_at' => Helper::getFormattedDateObject($field->updated_at, 'datetime'),
|
|
];
|
|
|
|
return $array;
|
|
}
|
|
|
|
/**
|
|
* Returns the core data for a field, including the default value it has
|
|
* when attributed to a certain model
|
|
*
|
|
* @param CustomField $field
|
|
* @param int $modelId
|
|
* @return array
|
|
*/
|
|
public function transformCustomFieldWithDefaultValue(CustomField $field, $modelId)
|
|
{
|
|
return [
|
|
'id' => $field->id,
|
|
'name' => e($field->name),
|
|
'type' => e($field->element),
|
|
'field_values_array' => ($field->field_values) ? explode("\r\n", e($field->field_values)) : null,
|
|
'default_value' => $field->defaultValue($modelId),
|
|
];
|
|
}
|
|
}
|