57 lines
1.9 KiB
PHP
57 lines
1.9 KiB
PHP
<?php
|
|
|
|
namespace App\Transformers\Objects;
|
|
|
|
use App\Models\Objects\Field;
|
|
use App\Models\Objects\FieldType;
|
|
use App\Models\Objects\NirObject;
|
|
use App\Transformers\Assets\AssetTransformer;
|
|
use League\Fractal\Resource\Collection;
|
|
use League\Fractal\TransformerAbstract;
|
|
|
|
class ObjectPropertyTransformer extends TransformerAbstract {
|
|
protected NirObject $object;
|
|
|
|
public function __construct(NirObject $object) {
|
|
$this->object = $object;
|
|
}
|
|
|
|
protected array $defaultIncludes = [
|
|
|
|
];
|
|
|
|
protected array $availableIncludes = [
|
|
'value', 'options'
|
|
];
|
|
|
|
public function transform(Field $model): array {
|
|
return [
|
|
'id' => $model->uuid,
|
|
'type' => $model->type,
|
|
'name' => $model->name,
|
|
'title' => $model->title,
|
|
'required' => boolval($model->required),
|
|
'multiple' => boolval($model->multiple),
|
|
'filterable' => boolval($model->filterable),
|
|
'hidden' => boolval($model->hidden),
|
|
'related' => $model->params['related'] ?? null,
|
|
'appearance' => $model->params['appearance'] ?? null
|
|
];
|
|
}
|
|
|
|
public function includeValue(Field $model) {
|
|
if ($value = $model->getValue($this->object->id)) {
|
|
if ($model->type === FieldType::RELATION) return $this->collection($value->filter(function($val) {return !is_null($val);}), $model->transformer);
|
|
elseif (in_array($model->type, [FieldType::DOCUMENT, FieldType::IMAGE])) return $this->collection($value->filter(function($val) {return !is_null($val);}), new AssetTransformer());
|
|
else return $this->primitive(['data' => $model->getValue($this->object->id)]);
|
|
}
|
|
return null;
|
|
}
|
|
|
|
public function includeOptions(Field $model): ?Collection {
|
|
return $model->options ? $this->collection($model->options, $model->transformer) : null;
|
|
}
|
|
|
|
|
|
}
|