1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Rinvex\Attributes\Support; |
6
|
|
|
|
7
|
|
|
use Illuminate\Database\Eloquent\Model; |
8
|
|
|
use Rinvex\Attributes\Models\Attribute; |
9
|
|
|
use Illuminate\Support\Collection as BaseCollection; |
10
|
|
|
use Illuminate\Database\Eloquent\Collection as EloquentCollection; |
11
|
|
|
|
12
|
|
|
class ValueCollection extends EloquentCollection |
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* The entity this value collection belongs to. |
16
|
|
|
* |
17
|
|
|
* @var \Illuminate\Database\Eloquent\Model |
18
|
|
|
*/ |
19
|
|
|
protected $entity; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* The attribute this value collection is storing. |
23
|
|
|
* |
24
|
|
|
* @var \Rinvex\Attributes\Models\Attribute |
25
|
|
|
*/ |
26
|
|
|
protected $attribute; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Link the entity and attribute to this value collection. |
30
|
|
|
* |
31
|
|
|
* @param \Illuminate\Database\Eloquent\Model $entity |
32
|
|
|
* @param $attribute |
33
|
|
|
* |
34
|
|
|
* @return $this |
35
|
|
|
*/ |
36
|
|
|
public function link($entity, $attribute) |
37
|
|
|
{ |
38
|
|
|
$this->entity = $entity; |
39
|
|
|
$this->attribute = $attribute; |
40
|
|
|
|
41
|
|
|
return $this; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Add new values to the value collection. |
46
|
|
|
* |
47
|
|
|
* @param \Illuminate\Support\Collection|array $values |
48
|
|
|
* |
49
|
|
|
* @return $this |
50
|
|
|
*/ |
51
|
|
|
public function add($values) |
52
|
|
|
{ |
53
|
|
|
if (! is_array($values) && ! $values instanceof BaseCollection) { |
54
|
|
|
$values = func_get_args(); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
// Once we have made sure our input is an array of values, we will convert |
58
|
|
|
// them into value model objects (if no model instances are given). When |
59
|
|
|
// done we will just push all values into the current collection items. |
60
|
|
|
foreach ($values as $value) { |
61
|
|
|
$this->push($this->buildValue($value)); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
return $this; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Replace current values with the given values. |
69
|
|
|
* |
70
|
|
|
* @param \Illuminate\Support\Collection|array $values |
71
|
|
|
* |
72
|
|
|
* @return $this |
73
|
|
|
*/ |
74
|
|
|
public function replace($values) |
75
|
|
|
{ |
76
|
|
|
if (! is_array($values) && ! $values instanceof BaseCollection) { |
77
|
|
|
$values = func_get_args(); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
// Trash the current values |
81
|
|
|
$this->trashCurrentItems(); |
82
|
|
|
|
83
|
|
|
// Build valid instances of the given values based on attribute data type |
84
|
|
|
$this->items = $this->buildValues($values); |
85
|
|
|
|
86
|
|
|
return $this; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* Trash the current values by queuing into entity object, |
91
|
|
|
* these trashed values will physically deleted on entity save. |
92
|
|
|
* |
93
|
|
|
* @return void |
94
|
|
|
*/ |
95
|
|
|
protected function trashCurrentItems(): void |
96
|
|
|
{ |
97
|
|
|
$trash = $this->entity->getEntityAttributeValueTrash(); |
98
|
|
|
|
99
|
|
|
foreach ($this->items as $value) { |
100
|
|
|
$trash->push($value); |
101
|
|
|
} |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* Build a value instance from the given input. |
106
|
|
|
* |
107
|
|
|
* @param mixed $value |
108
|
|
|
* |
109
|
|
|
* @return \Illuminate\Database\Eloquent\Model |
110
|
|
|
*/ |
111
|
|
|
protected function buildValue($value): Model |
112
|
|
|
{ |
113
|
|
|
if ($value instanceof Model || is_null($value)) { |
114
|
|
|
return $value; |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
$model = Attribute::getTypeModel($this->attribute->getAttribute('type')); |
118
|
|
|
$instance = new $model(); |
119
|
|
|
|
120
|
|
|
$instance->setAttribute('entity_id', $this->entity->getKey()); |
121
|
|
|
$instance->setAttribute('entity_type', $this->entity->getMorphClass()); |
122
|
|
|
$instance->setAttribute($this->attribute->getForeignKey(), $this->attribute->getKey()); |
123
|
|
|
$instance->setAttribute('content', $value); |
124
|
|
|
|
125
|
|
|
return $instance; |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* Build value instances from the given array. |
130
|
|
|
* |
131
|
|
|
* @param \Illuminate\Support\Collection|array $values |
132
|
|
|
* |
133
|
|
|
* @return \Rinvex\Attributes\Models\Value[] |
134
|
|
|
*/ |
135
|
|
|
protected function buildValues($values) |
136
|
|
|
{ |
137
|
|
|
$result = []; |
138
|
|
|
|
139
|
|
|
// We will iterate through the entire array of values transforming every |
140
|
|
|
// item into the data type object linked to this collection. Any null |
141
|
|
|
// value will be omitted here in order to avoid storing NULL values. |
142
|
|
|
foreach ($values as $value) { |
143
|
|
|
if (! is_null($value)) { |
144
|
|
|
$result[] = $this->buildValue($value); |
145
|
|
|
} |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
return $result; |
149
|
|
|
} |
150
|
|
|
} |
151
|
|
|
|