| 1 | <?php |
||
| 5 | abstract class Field |
||
| 6 | { |
||
| 7 | /** @var \Illuminate\Database\Eloquent\Model */ |
||
| 8 | protected $model; |
||
| 9 | |||
| 10 | /** @var string|array */ |
||
| 11 | protected $value; |
||
| 12 | |||
| 13 | 9 | public function __construct($value, $model) |
|
| 14 | { |
||
| 15 | 9 | $this->model = $model; |
|
| 16 | 9 | $this->value = $this->parseValue($value); |
|
| 17 | 9 | } |
|
| 18 | |||
| 19 | /** @param array|string $attributes */ |
||
| 20 | 9 | protected function parseAttributesWithKeys($attributes): array |
|
| 21 | { |
||
| 22 | 9 | $result = []; |
|
| 23 | 9 | if (is_array($attributes)) { |
|
| 24 | 3 | foreach ($attributes as $key => $field) { |
|
| 25 | 3 | $value = $this->model->getAttribute($field); |
|
| 26 | 3 | if (is_numeric($key)) { |
|
| 27 | 3 | $result[$field] = $value; |
|
| 28 | } else { |
||
| 29 | 3 | $result[$key] = $value; |
|
| 30 | } |
||
| 31 | } |
||
| 32 | } else { |
||
| 33 | 9 | $result[$attributes] = $this->model->getAttribute($attributes); |
|
| 34 | } |
||
| 35 | |||
| 36 | 9 | return $result; |
|
| 37 | } |
||
| 38 | |||
| 39 | /** |
||
| 40 | * @param array|string $attributes |
||
| 41 | * @return array|string |
||
| 42 | */ |
||
| 43 | 9 | protected function parseAttributes($attributes) |
|
| 44 | { |
||
| 45 | 9 | $result = []; |
|
| 46 | 9 | if (is_array($attributes)) { |
|
| 47 | 6 | foreach ($attributes as $key => $field) { |
|
| 48 | 6 | $result[] = $this->model->getAttribute($field); |
|
| 49 | } |
||
| 50 | } else { |
||
| 51 | 3 | return $this->model->getAttribute($attributes); |
|
| 52 | } |
||
| 53 | |||
| 54 | 6 | return $result; |
|
| 55 | } |
||
| 56 | |||
| 57 | /** @return mixed */ |
||
| 58 | 9 | public function getValue() |
|
| 59 | { |
||
| 60 | 9 | return $this->value; |
|
| 61 | } |
||
| 62 | |||
| 63 | /** |
||
| 64 | * @param array|string $value |
||
| 65 | * @return mixed |
||
| 66 | */ |
||
| 67 | abstract protected function parseValue($value); |
||
| 68 | } |
||
| 69 |