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 | 12 | public function __construct($value, $model) |
|
14 | { |
||
15 | 12 | $this->model = $model; |
|
16 | 12 | $this->value = $this->parseValue($value); |
|
17 | 12 | } |
|
18 | |||
19 | /** @param array|string $attributes */ |
||
20 | 12 | protected function parseAttributesWithKeys($attributes): array |
|
21 | { |
||
22 | 12 | $result = []; |
|
23 | 12 | 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 | $result[$key] = $value; |
||
30 | } |
||
31 | } |
||
32 | } else { |
||
33 | 12 | $result[$attributes] = $this->model->getAttribute($attributes); |
|
34 | } |
||
35 | |||
36 | 12 | 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 | 12 | public function getValue() |
|
59 | { |
||
60 | 12 | return $this->value; |
|
61 | } |
||
62 | |||
63 | /** |
||
64 | * @param array|string $value |
||
65 | * @return mixed |
||
66 | */ |
||
67 | abstract protected function parseValue($value); |
||
68 | } |
||
69 |