|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Backpack\CRUD\app\Library\Components; |
|
4
|
|
|
|
|
5
|
|
|
use Backpack\CRUD\app\Library\Components\Interfaces\SmartCollectionInterface; |
|
6
|
|
|
use Backpack\CRUD\app\Library\Components\Interfaces\SmartComponentInterface; |
|
7
|
|
|
|
|
8
|
|
|
class BaseComponent implements SmartComponentInterface |
|
9
|
|
|
{ |
|
10
|
|
|
public function __construct(protected SmartCollectionInterface $attributes) |
|
11
|
|
|
{ |
|
12
|
|
|
} |
|
13
|
|
|
|
|
14
|
|
|
public static function makeOf(SmartCollectionInterface $attributes) |
|
15
|
|
|
{ |
|
16
|
|
|
return new static($attributes); |
|
17
|
|
|
} |
|
18
|
|
|
|
|
19
|
|
|
public static function make(string|array $name): SmartComponentInterface |
|
20
|
|
|
{ |
|
21
|
|
|
$attributes = new BaseCollection($name, static::attributes(), static::rules(), static::defaults(), static::blocked()); |
|
22
|
|
|
|
|
23
|
|
|
return static::makeOf($attributes); |
|
24
|
|
|
} |
|
25
|
|
|
|
|
26
|
|
|
protected function remove() |
|
27
|
|
|
{ |
|
28
|
|
|
$this->attributes->deleteItem($this->getName()); |
|
|
|
|
|
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
|
|
public function __call($name, $arguments) |
|
32
|
|
|
{ |
|
33
|
|
|
if (! method_exists($this, $name)) { |
|
34
|
|
|
if ($this->isAttributeBlocked($name)) { |
|
35
|
|
|
throw new \Exception("Attribute {$name} cannot be changed in this component."); |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
$this->attributes->setAttribute($name, $arguments[0] ?? null); |
|
39
|
|
|
|
|
40
|
|
|
return $this; |
|
41
|
|
|
} |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
private function isAttributeBlocked(string $attribute): bool |
|
45
|
|
|
{ |
|
46
|
|
|
return in_array($attribute, $this->blocked()); |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
public static function defaults(): array |
|
50
|
|
|
{ |
|
51
|
|
|
return static::attributes(); |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
public static function rules(): array |
|
55
|
|
|
{ |
|
56
|
|
|
return static::attributes(); |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
public static function blocked(): array |
|
60
|
|
|
{ |
|
61
|
|
|
return static::attributes(); |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
public static function attributes(): array |
|
65
|
|
|
{ |
|
66
|
|
|
return []; |
|
67
|
|
|
} |
|
68
|
|
|
} |
|
69
|
|
|
|