|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace LaravelFlare\Flare\Admin\Attributes; |
|
4
|
|
|
|
|
5
|
|
|
use Illuminate\Support\Collection; |
|
6
|
|
|
use LaravelFlare\Flare\Admin\Attributes\BaseAttribute; |
|
7
|
|
|
|
|
8
|
|
|
class AttributeCollection extends Collection |
|
9
|
|
|
{ |
|
10
|
|
|
/** |
|
11
|
|
|
* Attribute Manager Instance |
|
12
|
|
|
* |
|
13
|
|
|
* @var |
|
14
|
|
|
*/ |
|
15
|
|
|
protected $attributeManager; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* The items contained in the collection. |
|
19
|
|
|
* |
|
20
|
|
|
* @var array |
|
21
|
|
|
*/ |
|
22
|
|
|
protected $items = []; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* Create a new collection. |
|
26
|
|
|
* |
|
27
|
|
|
* @param mixed $items |
|
28
|
|
|
* |
|
29
|
|
|
* @return void |
|
|
|
|
|
|
30
|
|
|
*/ |
|
31
|
|
|
public function __construct($items = []) |
|
32
|
|
|
{ |
|
33
|
|
|
if (!is_array($items) || func_num_args() > 1) { |
|
34
|
|
|
$items = func_get_args(); |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
$this->attributeManager = new AttributeManager(); |
|
38
|
|
|
$this->items = $items; |
|
39
|
|
|
|
|
40
|
|
|
$this->formatFields(); |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* Attempt to reformat the current attribute items array |
|
45
|
|
|
* into the most usable format (an Attribute Collection). |
|
46
|
|
|
* |
|
47
|
|
|
* @return void |
|
48
|
|
|
*/ |
|
49
|
|
|
public function formatFields() |
|
50
|
|
|
{ |
|
51
|
|
|
$items = $this->items; |
|
52
|
|
|
$formattedItems = []; |
|
53
|
|
|
|
|
54
|
|
|
foreach ($items as $name => $inner) { |
|
55
|
|
|
$formattedItems[$name] = $this->formatInnerField($name, $inner); |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
$this->items = $formattedItems; |
|
59
|
|
|
|
|
60
|
|
|
return $this; |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* Allows adding fields to the Attribute Collection |
|
65
|
|
|
* |
|
66
|
|
|
* @param array $items |
|
67
|
|
|
*/ |
|
68
|
|
|
public function add($items = []) |
|
69
|
|
|
{ |
|
70
|
|
|
if (!is_array($items) || func_num_args() > 1) { |
|
71
|
|
|
$items = func_get_args(); |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
$this->push($this->formatInnerField(null, $items)); |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* Format an Inner Field which can either be in the format |
|
79
|
|
|
* of an Array, an instance of BaseAttribute or even an |
|
80
|
|
|
* AttributeCollection object (which contains more!). |
|
81
|
|
|
* |
|
82
|
|
|
* @param mixed $name |
|
83
|
|
|
* @param mixed $inner |
|
84
|
|
|
* |
|
85
|
|
|
* @return mixed |
|
86
|
|
|
*/ |
|
87
|
|
|
protected function formatInnerField($name = null, $inner = []) |
|
88
|
|
|
{ |
|
89
|
|
|
if ($inner instanceof BaseAttribute) { |
|
90
|
|
|
return $inner; |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
if ($inner instanceof AttributeCollection) { |
|
94
|
|
|
$formattedItems = []; |
|
95
|
|
|
|
|
96
|
|
|
foreach ($inner->toArray() as $name => $inner) { |
|
97
|
|
|
$formattedItems[$name] = $this->formatInnerField($inner); |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
return $formattedItems; |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
if (is_scalar($inner) && $this->attributeManager->attributeTypeExists($inner)) { |
|
104
|
|
|
return $this->attributeManager->createAttribute($inner, $name, []); |
|
|
|
|
|
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
if (is_array($inner) && array_key_exists('type', $inner) && $this->attributeManager->attributeTypeExists($inner['type'])) { |
|
108
|
|
|
$type = $inner['type']; |
|
109
|
|
|
array_forget($inner, 'type'); |
|
110
|
|
|
return $this->attributeManager->createAttribute($type, $name, $inner); |
|
|
|
|
|
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
if (is_array($inner)) { |
|
114
|
|
|
return new AttributeCollection($inner); |
|
115
|
|
|
} |
|
116
|
|
|
} |
|
117
|
|
|
} |
|
118
|
|
|
|
Adding a
@returnannotation to a constructor is not recommended, since a constructor does not have a meaningful return value.Please refer to the PHP core documentation on constructors.