2022-11-10 02:54:02 -08:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Models\Labels;
|
|
|
|
|
|
|
|
use App\Models\Asset;
|
|
|
|
use Illuminate\Support\Collection;
|
|
|
|
|
|
|
|
class Field {
|
|
|
|
protected Collection $options;
|
|
|
|
public function getOptions() { return $this->options; }
|
|
|
|
public function setOptions($options) {
|
|
|
|
$tempCollect = collect($options);
|
|
|
|
if (!$tempCollect->contains(fn($o) => !is_subclass_of($o, FieldOption::class))) {
|
|
|
|
$this->options = $options;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public function toArray(Asset $asset) { return Field::makeArray($this, $asset); }
|
|
|
|
|
|
|
|
/* Statics */
|
|
|
|
|
|
|
|
public static function makeArray(Field $field, Asset $asset) {
|
|
|
|
return $field->getOptions()
|
2023-10-02 16:19:20 -07:00
|
|
|
// filter out any FieldOptions that are accidentally null
|
2023-10-02 16:14:20 -07:00
|
|
|
->filter()
|
2022-11-10 02:54:02 -08:00
|
|
|
->map(fn($option) => $option->toArray($asset))
|
|
|
|
->filter(fn($result) => $result['value'] != null);
|
|
|
|
}
|
|
|
|
|
|
|
|
public static function makeString(Field $option) {
|
|
|
|
return implode('|', $option->getOptions());
|
|
|
|
}
|
|
|
|
|
|
|
|
public static function fromString(string $theString) {
|
|
|
|
$field = new Field();
|
|
|
|
$field->options = collect(explode('|', $theString))
|
|
|
|
->filter(fn($optionString) => !empty($optionString))
|
|
|
|
->map(fn($optionString) => FieldOption::fromString($optionString));
|
|
|
|
return $field;
|
|
|
|
}
|
2023-10-02 16:14:20 -07:00
|
|
|
}
|