|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Softonic\GraphQL\DataObjects; |
|
4
|
|
|
|
|
5
|
|
|
use Softonic\GraphQL\DataObjects\Interfaces\DataObject; |
|
6
|
|
|
use Softonic\GraphQL\DataObjects\Mutation\FilteredCollection; |
|
7
|
|
|
use Softonic\GraphQL\DataObjects\Mutation\MutationObject; |
|
8
|
|
|
use Softonic\GraphQL\DataObjects\Traits\ObjectHandler; |
|
9
|
|
|
|
|
10
|
|
|
abstract class AbstractCollection implements DataObject, \IteratorAggregate, \JsonSerializable |
|
11
|
|
|
{ |
|
12
|
|
|
use ObjectHandler; |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* @var array |
|
16
|
|
|
*/ |
|
17
|
|
|
protected $arguments = []; |
|
18
|
|
|
|
|
19
|
106 |
|
public function __construct(array $arguments = []) |
|
20
|
|
|
{ |
|
21
|
106 |
|
$this->arguments = $arguments; |
|
22
|
106 |
|
} |
|
23
|
|
|
|
|
24
|
10 |
|
public function count(): int |
|
25
|
|
|
{ |
|
26
|
10 |
|
$count = 0; |
|
27
|
10 |
|
foreach ($this->arguments as $argument) { |
|
28
|
8 |
|
if ($argument instanceof AbstractCollection) { |
|
29
|
6 |
|
$count += $argument->count(); |
|
30
|
6 |
|
} elseif ($argument instanceof AbstractItem) { |
|
31
|
6 |
|
++$count; |
|
32
|
|
|
} |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
10 |
|
return $count; |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
78 |
|
public function getIterator(): \RecursiveIteratorIterator |
|
39
|
|
|
{ |
|
40
|
78 |
|
return new \RecursiveIteratorIterator(new CollectionIterator($this->arguments)); |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
8 |
|
public function isEmpty(): bool |
|
44
|
|
|
{ |
|
45
|
8 |
|
return $this->count() === 0; |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
2 |
|
public function has(string $key): bool |
|
49
|
|
|
{ |
|
50
|
2 |
|
foreach ($this->arguments as $argument) { |
|
51
|
2 |
|
if ($argument->has($key)) { |
|
52
|
2 |
|
return true; |
|
53
|
|
|
} |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
2 |
|
return false; |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
2 |
|
public function hasItem(array $itemData): bool |
|
60
|
|
|
{ |
|
61
|
2 |
|
foreach ($this->arguments as $argument) { |
|
62
|
2 |
|
$method = $argument instanceof FilteredCollection ? 'hasItem' : 'exists'; |
|
63
|
|
|
|
|
64
|
2 |
|
if ($argument->$method($itemData)) { |
|
65
|
2 |
|
return true; |
|
66
|
|
|
} |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
2 |
|
return false; |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
48 |
|
public function hasChildren(): bool |
|
73
|
|
|
{ |
|
74
|
48 |
|
foreach ($this->arguments as $argument) { |
|
75
|
48 |
|
if ($argument instanceof MutationObject) { |
|
76
|
48 |
|
return true; |
|
77
|
|
|
} |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
4 |
|
return false; |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
34 |
|
public function filter(array $filters): AbstractCollection |
|
84
|
|
|
{ |
|
85
|
34 |
|
$filteredData = []; |
|
86
|
34 |
|
if ($this->areAllArgumentsCollections()) { |
|
87
|
8 |
|
foreach ($this->arguments as $argument) { |
|
88
|
8 |
|
$filteredData[] = $argument->filter($filters); |
|
89
|
|
|
} |
|
90
|
|
|
} else { |
|
91
|
34 |
|
$filteredData = $this->filterItems($this->arguments, $filters); |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
34 |
|
return $this->buildFilteredCollection($filteredData); |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
abstract protected function buildFilteredCollection($data); |
|
98
|
|
|
|
|
99
|
34 |
|
private function areAllArgumentsCollections(): bool |
|
100
|
|
|
{ |
|
101
|
34 |
|
return (!empty($this->arguments[0]) && $this->arguments[0] instanceof AbstractCollection); |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
private function filterItems(array $arguments, array $filters): array |
|
105
|
|
|
{ |
|
106
|
34 |
|
$filteredItems = array_filter($arguments, function ($item) use ($filters) { |
|
107
|
34 |
|
foreach ($filters as $filterKey => $filterValue) { |
|
108
|
34 |
|
if (!($item->{$filterKey} == $filterValue)) { |
|
109
|
34 |
|
return false; |
|
110
|
|
|
} |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
32 |
|
return true; |
|
114
|
34 |
|
}); |
|
115
|
|
|
|
|
116
|
34 |
|
return array_values($filteredItems); |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
abstract public function jsonSerialize(): array; |
|
120
|
|
|
} |
|
121
|
|
|
|