|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Leonidas\Library\System; |
|
4
|
|
|
|
|
5
|
|
|
use Contracts\Collection\CollectionInterface; |
|
6
|
|
|
|
|
7
|
|
|
class SystemModelCollection implements CollectionInterface |
|
8
|
|
|
{ |
|
9
|
|
|
protected string $entityType; |
|
10
|
|
|
|
|
11
|
|
|
protected string $idKey; |
|
12
|
|
|
|
|
13
|
|
|
protected string $slugKey; |
|
14
|
|
|
|
|
15
|
|
|
protected array $items; |
|
16
|
|
|
|
|
17
|
|
|
public function __construct(string $entityType, string $idKey, string $slugKey, ...$items) |
|
18
|
|
|
{ |
|
19
|
|
|
$this->entityType = $entityType; |
|
20
|
|
|
$this->idKey = $idKey; |
|
21
|
|
|
$this->slugKey = $slugKey; |
|
22
|
|
|
$this->items = $items; |
|
23
|
|
|
} |
|
24
|
|
|
|
|
25
|
|
|
public function all(): array |
|
26
|
|
|
{ |
|
27
|
|
|
return $this->items; |
|
28
|
|
|
} |
|
29
|
|
|
|
|
30
|
|
|
public function has(string $item): bool |
|
31
|
|
|
{ |
|
32
|
|
|
return isset($this->items[$item]); |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
public function remove(string $item): void |
|
|
|
|
|
|
36
|
|
|
{ |
|
37
|
|
|
// |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
public function select(string $property): array |
|
41
|
|
|
{ |
|
42
|
|
|
return array_map(function ($object) use ($property) { |
|
43
|
|
|
return $object->{$property}; |
|
44
|
|
|
}, $this->items); |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
public function getIds(): array |
|
48
|
|
|
{ |
|
49
|
|
|
return $this->select($this->idKey); |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
public function getMeta(string $metaKey): array |
|
53
|
|
|
{ |
|
54
|
|
|
$meta = []; |
|
55
|
|
|
|
|
56
|
|
|
foreach ($this->items as $object) { |
|
57
|
|
|
$id = $object->{$this->idKey}; |
|
58
|
|
|
|
|
59
|
|
|
$meta[$id] = $this->getObjectMetadata($object, $metaKey); |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
return $meta; |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
public function isEmpty(): bool |
|
66
|
|
|
{ |
|
67
|
|
|
return empty($this->items); |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
public function sortByMeta(string $metaKey) |
|
71
|
|
|
{ |
|
72
|
|
|
$orderArray = []; |
|
73
|
|
|
|
|
74
|
|
|
$collection = $this->items; |
|
75
|
|
|
$id = $this->idKey; |
|
76
|
|
|
|
|
77
|
|
|
foreach ($collection as $item) { |
|
78
|
|
|
$orderArray[$item->{$id}] = $this->getObjectMetadata($item, $metaKey); |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
usort($collection, $this->sortByMetaCallback($orderArray)); |
|
82
|
|
|
|
|
83
|
|
|
return $collection; |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
public function diff(CollectionInterface $collection): CollectionInterface |
|
87
|
|
|
{ |
|
88
|
|
|
$items = array_udiff( |
|
89
|
|
|
$this->all(), |
|
90
|
|
|
$collection->all(), |
|
91
|
|
|
$this->diffCallback() |
|
92
|
|
|
); |
|
93
|
|
|
|
|
94
|
|
|
return new static( |
|
95
|
|
|
$this->idKey, |
|
96
|
|
|
$this->nameKey, |
|
|
|
|
|
|
97
|
|
|
$this->slugKey, |
|
98
|
|
|
$this->entityType, |
|
99
|
|
|
$items |
|
100
|
|
|
); |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
protected function getObjectMetadata(object $object, string $metaKey) |
|
104
|
|
|
{ |
|
105
|
|
|
return get_metadata($this->entityType, $object->{$this->idKey}, $metaKey, true); |
|
|
|
|
|
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
protected function diffCallback(): callable |
|
109
|
|
|
{ |
|
110
|
|
|
return function ($object1, $object2) { |
|
111
|
|
|
return $object1->{$this->idKey} - $object2->{$this->idKey}; |
|
112
|
|
|
}; |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
/** |
|
116
|
|
|
* @param array $orderArray Associative array with object ids as keys and |
|
117
|
|
|
* desired order as values |
|
118
|
|
|
* |
|
119
|
|
|
* @return callable |
|
120
|
|
|
*/ |
|
121
|
|
|
protected function sortByMetaCallback(array $orderArray): callable |
|
122
|
|
|
{ |
|
123
|
|
|
return function ($a, $b) use ($orderArray) { |
|
124
|
|
|
// Set value to 0 if one is not provided |
|
125
|
|
|
$a = (int) $orderArray[$a->{$this->idKey}] ?? 0; |
|
126
|
|
|
$b = (int) $orderArray[$b->{$this->idKey}] ?? 0; |
|
127
|
|
|
|
|
128
|
|
|
if ($a === $b) { |
|
129
|
|
|
return 0; |
|
130
|
|
|
} |
|
131
|
|
|
|
|
132
|
|
|
if ($a < $b && $a === 0) { |
|
133
|
|
|
return 1; |
|
134
|
|
|
} |
|
135
|
|
|
|
|
136
|
|
|
if ($a > $b && $b === 0) { |
|
137
|
|
|
return -1; |
|
138
|
|
|
} |
|
139
|
|
|
|
|
140
|
|
|
return $a > $b ? 1 : -1; |
|
141
|
|
|
}; |
|
142
|
|
|
} |
|
143
|
|
|
} |
|
144
|
|
|
|
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.