| Total Complexity | 5 |
| Total Lines | 80 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
| 1 | <?php |
||
| 8 | class HasOne extends BelongsTo |
||
| 9 | { |
||
| 10 | use HasColumns; |
||
|
|
|||
| 11 | |||
| 12 | /** @var null|array */ |
||
| 13 | protected $only; |
||
| 14 | |||
| 15 | /** @var null|array */ |
||
| 16 | protected $except; |
||
| 17 | |||
| 18 | /** |
||
| 19 | * @param array $only |
||
| 20 | * |
||
| 21 | * @return self |
||
| 22 | */ |
||
| 23 | public function only(array $only): self |
||
| 24 | { |
||
| 25 | $this->only = $only; |
||
| 26 | |||
| 27 | return $this; |
||
| 28 | } |
||
| 29 | |||
| 30 | /** |
||
| 31 | * @param array $except |
||
| 32 | * |
||
| 33 | * @return self |
||
| 34 | */ |
||
| 35 | public function except(array $except): self |
||
| 36 | { |
||
| 37 | $this->except = $except; |
||
| 38 | |||
| 39 | return $this; |
||
| 40 | } |
||
| 41 | |||
| 42 | /** |
||
| 43 | * @return array |
||
| 44 | */ |
||
| 45 | protected function onEdit(): array |
||
| 46 | { |
||
| 47 | $relation = $this->model->{$this->id()}(); |
||
| 48 | |||
| 49 | $columns = $this->relatedColumns($relation->getRelated()) |
||
| 50 | ->each(function ($field) { |
||
| 51 | $field->setId( |
||
| 52 | "{$this->id()}.{$field->id()}" |
||
| 53 | ); |
||
| 54 | }); |
||
| 55 | |||
| 56 | return [ |
||
| 57 | 'columns' => $columns, |
||
| 58 | ]; |
||
| 59 | } |
||
| 60 | |||
| 61 | /** |
||
| 62 | * @return array |
||
| 63 | */ |
||
| 64 | protected function onIndex(): array |
||
| 65 | { |
||
| 66 | $relation = $this->model->{$this->id()}(); |
||
| 67 | |||
| 68 | $columns = $this->relatedColumns($relation->getRelated()) |
||
| 69 | ->filter(function ($field) { |
||
| 70 | return !$field instanceof Textarea; |
||
| 71 | }); |
||
| 72 | |||
| 73 | return [ |
||
| 74 | 'columns' => $columns, |
||
| 75 | 'related' => $this->model->{$this->id()}, |
||
| 76 | ]; |
||
| 77 | } |
||
| 78 | |||
| 79 | /** |
||
| 80 | * @param $related |
||
| 81 | * @return \Terranet\Administrator\Collection\Mutable |
||
| 82 | */ |
||
| 83 | protected function relatedColumns($related): Mutable |
||
| 88 | } |
||
| 89 | } |
||
| 90 |