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
|
38 |
|
public function __construct(array $arguments = [], array $config = [], bool $hasChanged = false) |
27
|
|
|
{ |
28
|
38 |
|
$this->arguments = $arguments; |
29
|
38 |
|
$this->config = $config; |
30
|
38 |
|
$this->hasChanged = $hasChanged; |
31
|
38 |
|
} |
32
|
|
|
|
33
|
14 |
|
public function __get(string $key): Collection |
34
|
|
|
{ |
35
|
14 |
|
$items = []; |
36
|
14 |
|
foreach ($this->arguments as $argument) { |
37
|
14 |
|
$items[] = $argument->{$key}; |
38
|
|
|
} |
39
|
|
|
|
40
|
14 |
|
return new Collection($items, $this->config[$key]->children); |
41
|
|
|
} |
42
|
|
|
|
43
|
18 |
|
public function set(array $data): void |
44
|
|
|
{ |
45
|
18 |
|
foreach ($this->arguments as $argument) { |
46
|
18 |
|
$argument->set($data); |
47
|
|
|
} |
48
|
18 |
|
} |
49
|
|
|
|
50
|
14 |
|
public function filter(array $filters): FilteredCollection |
51
|
|
|
{ |
52
|
14 |
|
$filteredData = []; |
53
|
14 |
|
if ($this->areAllArgumentsCollections()) { |
54
|
4 |
|
foreach ($this->arguments as $argument) { |
55
|
4 |
|
$filteredItems = $this->filterItems($argument->arguments, $filters); |
|
|
|
|
56
|
|
|
|
57
|
4 |
|
$filteredData[] = new FilteredCollection($filteredItems, $this->config); |
58
|
|
|
} |
59
|
|
|
} else { |
60
|
10 |
|
$filteredItems = $this->filterItems($this->arguments, $filters); |
61
|
|
|
|
62
|
10 |
|
$filteredData = $filteredItems; |
63
|
|
|
} |
64
|
|
|
|
65
|
14 |
|
return new FilteredCollection($filteredData, $this->config); |
66
|
|
|
} |
67
|
|
|
|
68
|
24 |
|
public function jsonSerialize(): array |
69
|
|
|
{ |
70
|
24 |
|
$items = []; |
71
|
24 |
|
foreach ($this->arguments as $item) { |
72
|
24 |
|
if ($item->hasChanged()) { |
73
|
24 |
|
$items[] = $item->jsonSerialize(); |
74
|
|
|
} |
75
|
|
|
} |
76
|
|
|
|
77
|
24 |
|
return $items; |
78
|
|
|
} |
79
|
|
|
|
80
|
14 |
|
private function areAllArgumentsCollections(): bool |
81
|
|
|
{ |
82
|
14 |
|
return (!empty($this->arguments[0]) && $this->arguments[0] instanceof Collection); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
private function filterItems(array $arguments, array $filters): array |
86
|
|
|
{ |
87
|
14 |
|
return array_filter($arguments, function ($item) use ($filters) { |
88
|
14 |
|
foreach ($filters as $filterKey => $filterValue) { |
89
|
14 |
|
if (!($item->{$filterKey} == $filterValue)) { |
90
|
14 |
|
return false; |
91
|
|
|
} |
92
|
|
|
} |
93
|
|
|
|
94
|
14 |
|
return true; |
95
|
14 |
|
}); |
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@property
annotation 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.