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