|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Softonic\GraphQL\Mutation; |
|
4
|
|
|
|
|
5
|
|
|
use Softonic\GraphQL\Mutation\Traits\MutationObjectHandler; |
|
6
|
|
|
|
|
7
|
|
|
class FilteredCollection implements MutationObject, \JsonSerializable |
|
8
|
|
|
{ |
|
9
|
|
|
use MutationObjectHandler; |
|
10
|
|
|
|
|
11
|
|
|
/** |
|
12
|
|
|
* @var array<Item> |
|
13
|
|
|
*/ |
|
14
|
|
|
protected $arguments = []; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* @var array |
|
18
|
|
|
*/ |
|
19
|
|
|
protected $config; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* @var bool |
|
23
|
|
|
*/ |
|
24
|
|
|
private $hasChanged = false; |
|
25
|
|
|
|
|
26
|
58 |
|
public function __construct(array $arguments = [], array $config = [], bool $hasChanged = false) |
|
27
|
|
|
{ |
|
28
|
58 |
|
$this->arguments = $arguments; |
|
29
|
58 |
|
$this->config = $config; |
|
30
|
58 |
|
$this->hasChanged = $hasChanged; |
|
31
|
58 |
|
} |
|
32
|
|
|
|
|
33
|
20 |
|
public function __get(string $key): Collection |
|
34
|
|
|
{ |
|
35
|
20 |
|
$items = []; |
|
36
|
20 |
|
foreach ($this->arguments as $argument) { |
|
37
|
20 |
|
$items[] = $argument->{$key}; |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
20 |
|
return new Collection($items, $this->config[$key]->children); |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
24 |
|
public function set(array $data): void |
|
44
|
|
|
{ |
|
45
|
24 |
|
foreach ($this->arguments as $argument) { |
|
46
|
24 |
|
$argument->set($data); |
|
47
|
|
|
} |
|
48
|
24 |
|
} |
|
49
|
|
|
|
|
50
|
22 |
|
public function filter(array $filters): FilteredCollection |
|
51
|
|
|
{ |
|
52
|
22 |
|
$filteredData = []; |
|
53
|
22 |
|
if ($this->areAllArgumentsCollections()) { |
|
54
|
8 |
|
foreach ($this->arguments as $argument) { |
|
55
|
8 |
|
$filteredItems = $this->filterItems($argument->arguments, $filters); |
|
|
|
|
|
|
56
|
|
|
|
|
57
|
8 |
|
$filteredData[] = new FilteredCollection($filteredItems, $this->config); |
|
58
|
|
|
} |
|
59
|
|
|
} else { |
|
60
|
16 |
|
$filteredData = $this->filterItems($this->arguments, $filters); |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
22 |
|
return new FilteredCollection($filteredData, $this->config); |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
22 |
|
private function areAllArgumentsCollections(): bool |
|
67
|
|
|
{ |
|
68
|
22 |
|
return (!empty($this->arguments[0]) && $this->arguments[0] instanceof Collection); |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
private function filterItems(array $arguments, array $filters): array |
|
72
|
|
|
{ |
|
73
|
22 |
|
$filteredItems = array_filter($arguments, function ($item) use ($filters) { |
|
74
|
22 |
|
foreach ($filters as $filterKey => $filterValue) { |
|
75
|
22 |
|
if (!($item->{$filterKey} == $filterValue)) { |
|
76
|
22 |
|
return false; |
|
77
|
|
|
} |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
22 |
|
return true; |
|
81
|
22 |
|
}); |
|
82
|
|
|
|
|
83
|
22 |
|
return array_values($filteredItems); |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
36 |
|
public function jsonSerialize(): array |
|
87
|
|
|
{ |
|
88
|
36 |
|
$items = []; |
|
89
|
36 |
|
foreach ($this->arguments as $item) { |
|
90
|
36 |
|
if ($item->hasChanged()) { |
|
91
|
36 |
|
$items[] = $item->jsonSerialize(); |
|
92
|
|
|
} |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
36 |
|
return $items; |
|
96
|
|
|
} |
|
97
|
|
|
} |
|
98
|
|
|
|
Since your code implements the magic setter
_set, this function will be called for any write access on an undefined variable. You can add the@propertyannotation to your class or interface to document the existence of this variable.Since the property has write access only, you can use the @property-write annotation instead.
Of course, you may also just have mistyped another name, in which case you should fix the error.
See also the PhpDoc documentation for @property.