1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Backpack\CRUD\app\Library\Components; |
4
|
|
|
|
5
|
|
|
use Backpack\CRUD\app\Library\Components\Attributes\BackpackAttribute; |
6
|
|
|
use Backpack\CRUD\app\Library\Components\Interfaces\AttributeInterface; |
7
|
|
|
use Backpack\CRUD\app\Library\Components\Interfaces\SmartComponentInterface; |
8
|
|
|
use Illuminate\Support\Collection; |
9
|
|
|
use Illuminate\Support\Facades\Validator; |
10
|
|
|
|
11
|
|
|
class AttributeCollection |
12
|
|
|
{ |
13
|
|
|
protected Collection $items; |
14
|
|
|
|
15
|
|
|
public function __construct(array|string|Collection $attributes, |
16
|
|
|
private CollectionRepository $collectionRepository, |
17
|
|
|
ValidationRules|array $rules = [], |
18
|
|
|
array $defaults = [] |
19
|
|
|
) { |
20
|
|
|
$attributes = is_string($attributes) ? collect(['name' => $attributes]) : collect($attributes); |
|
|
|
|
21
|
|
|
$rules = is_array($rules) ? new ValidationRules($rules) : $rules; |
|
|
|
|
22
|
|
|
|
23
|
|
|
$defaults = collect($defaults)->mapWithKeys(function ($default, $attribute) { |
24
|
|
|
if (is_a($default, AttributeInterface::class, true)) { |
25
|
|
|
return [$default::getAttributeName() => $default]; |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
return [$attribute => $default]; |
29
|
|
|
})->toArray(); |
30
|
|
|
|
31
|
|
|
$this->items = $this->buildAttributes($attributes, $rules, $defaults); |
32
|
|
|
|
33
|
|
|
assert($this->hasAttribute('name'), 'Component name cant be empty.'); |
34
|
|
|
assert(is_string($this->getAttributeValue('name')), 'Component name must be a valid string.'); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
private function buildAttributes(Collection $attributes, ValidationRules $rules, array $defaults): Collection |
38
|
|
|
{ |
39
|
|
|
return $attributes->mapWithKeys(function ($attribute, $key) use ($rules, $defaults) { |
40
|
|
|
if (! is_a($attribute, AttributeInterface::class, true)) { |
41
|
|
|
return [$key => new BackpackAttribute($key, $attribute, $defaults[$key] ?? null, $rules->for($key))]; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
return [$attribute::getAttributeName() => $attribute]; |
45
|
|
|
}); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
public function setCollectionManager(CollectionRepository $collection) |
49
|
|
|
{ |
50
|
|
|
$this->collectionRepository = $collection; |
51
|
|
|
|
52
|
|
|
return $collection; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
public function hasAttribute($attribute) |
56
|
|
|
{ |
57
|
|
|
return $this->items->has($attribute); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
public function getAttributeValue($attribute, $default = null) |
61
|
|
|
{ |
62
|
|
|
return $this->items->has($attribute) ? $this->items->get($attribute, $default)->value() : $default; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
public function getCollectionRepository() |
66
|
|
|
{ |
67
|
|
|
return $this->collectionRepository; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
public function replaceAttributes(AttributeCollection $attributes) |
71
|
|
|
{ |
72
|
|
|
$this->attributes = $attributes->toArray(); |
|
|
|
|
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
public function setValidationRules(array $rules) |
76
|
|
|
{ |
77
|
|
|
$rules = new ValidationRules($rules); |
78
|
|
|
|
79
|
|
|
$this->items->each(function ($item, $key) use ($rules) { |
80
|
|
|
if (! is_string($key) && is_a($item, AttributeInterface::class, true)) { |
81
|
|
|
$key = $item::getAttributeName(); |
82
|
|
|
} |
83
|
|
|
if (is_string($rules->for($key) ?? [])) { |
84
|
|
|
dd($key, $rules); |
85
|
|
|
} |
86
|
|
|
$item->setRules($rules->for($key) ?? []); |
87
|
|
|
}); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
public function validate($attributes = null, $rules = null) |
91
|
|
|
{ |
92
|
|
|
$attributes = $attributes ?? $this->toArray(); |
93
|
|
|
|
94
|
|
|
$rules = $rules ?? $this->rules; |
|
|
|
|
95
|
|
|
|
96
|
|
|
$validator = Validator::make($attributes, $rules)->stopOnFirstFailure(); |
97
|
|
|
|
98
|
|
|
if ($validator->fails()) { |
99
|
|
|
throw new \Exception($validator->errors()->first()); |
100
|
|
|
} |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
public function getItems() |
104
|
|
|
{ |
105
|
|
|
return $this->items; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
public function addAttribute($attribute, $value) |
109
|
|
|
{ |
110
|
|
|
$item = $this->hasAttribute($attribute) ? $this->items->get($attribute)->setValue($value) : new BackpackAttribute($attribute, $value); |
111
|
|
|
$item->validate($value); |
112
|
|
|
$this->updateItem($item); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
public function addCollectionOfAttributes(SmartComponentInterface $item) |
116
|
|
|
{ |
117
|
|
|
$this->collectionRepository->addItem($item->getAttribute('name'), $item->getAttributesArray()); |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
private function updateItem(AttributeInterface|BackpackAttribute $item) |
121
|
|
|
{ |
122
|
|
|
$this->items[$item->attribute] = $item; |
|
|
|
|
123
|
|
|
$this->collectionRepository->setItemAttributes($this->getAttributeValue('name'), $this->toArray()); |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
public function deleteItem(string $name) |
127
|
|
|
{ |
128
|
|
|
$this->items->forget($name); |
129
|
|
|
$this->collectionRepository->removeItem($name); |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
public function toArray() |
133
|
|
|
{ |
134
|
|
|
return $this->items->mapWithKeys(function ($item, $key) { |
135
|
|
|
return [$key => $item->value()]; |
136
|
|
|
})->toArray(); |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
public function setAttributeDefaults(array $defaults) |
140
|
|
|
{ |
141
|
|
|
foreach ($defaults as $key => $default) { |
142
|
|
|
if (! is_string($key) && is_a($default, AttributeInterface::class, true)) { |
143
|
|
|
$key = $default::getAttributeName(); |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
if (! $this->items->has($key)) { |
147
|
|
|
if (is_a($default, AttributeInterface::class, true)) { |
148
|
|
|
$this->items[$key] = $default::make($key, $default::getDefault($this), $default, []); |
149
|
|
|
|
150
|
|
|
continue; |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
if ($default instanceof \Closure) { |
154
|
|
|
$this->items[$key] = new BackpackAttribute($key, $default($this), $default, []); |
155
|
|
|
|
156
|
|
|
continue; |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
$this->items[$key] = new BackpackAttribute($key, $default, $default, []); |
160
|
|
|
} |
161
|
|
|
} |
162
|
|
|
} |
163
|
|
|
} |
164
|
|
|
|