1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Softonic\GraphQL\Mutation; |
4
|
|
|
|
5
|
|
|
use Softonic\GraphQL\Traits\JsonSerializer; |
6
|
|
|
|
7
|
|
|
class FilteredCollection implements MutationObject, \JsonSerializable |
8
|
|
|
{ |
9
|
|
|
use JsonSerializer; |
10
|
|
|
|
11
|
|
|
protected $arguments = []; |
12
|
|
|
|
13
|
|
|
protected $config; |
14
|
|
|
|
15
|
|
|
private $hasChanged = false; |
16
|
|
|
|
17
|
40 |
|
public function __construct(array $arguments = [], array $config = [], bool $hasChanged = false) |
18
|
|
|
{ |
19
|
40 |
|
$this->arguments = $arguments; |
20
|
40 |
|
$this->config = $config; |
21
|
40 |
|
$this->hasChanged = $hasChanged; |
22
|
40 |
|
} |
23
|
|
|
|
24
|
16 |
|
public function __get(string $key) |
25
|
|
|
{ |
26
|
16 |
|
$items = []; |
27
|
|
|
|
28
|
16 |
|
foreach ($this->arguments as $argument) { |
29
|
|
|
|
30
|
16 |
|
$items[] = $argument->{$key}; |
31
|
|
|
} |
32
|
|
|
|
33
|
16 |
|
return new Collection($items, $this->config[$key]->children); |
34
|
|
|
} |
35
|
|
|
|
36
|
20 |
|
public function set(array $data) |
37
|
|
|
{ |
38
|
20 |
|
foreach ($this->arguments as $argument) { |
39
|
20 |
|
if ($argument instanceof Collection) { |
40
|
10 |
|
$argument->set($data); |
41
|
|
|
} else { |
42
|
20 |
|
$this->setItemData($argument, $data); |
43
|
|
|
} |
44
|
|
|
} |
45
|
20 |
|
} |
46
|
|
|
|
47
|
16 |
|
public function filter(array $filters) |
48
|
|
|
{ |
49
|
16 |
|
$filteredData = []; |
50
|
16 |
|
if (!empty($this->arguments[0]) && $this->arguments[0] instanceof Collection) { |
51
|
6 |
|
foreach ($this->arguments as $argument) { |
52
|
6 |
|
$argumentCopy = clone $argument; |
53
|
|
|
|
54
|
6 |
|
$filteredItems = $this->filterItems($argumentCopy->arguments, $filters); |
55
|
|
|
|
56
|
6 |
|
$argumentCopy->arguments = $filteredItems; |
57
|
|
|
|
58
|
6 |
|
$filteredData[] = $argumentCopy; |
59
|
|
|
} |
60
|
|
|
} else { |
61
|
12 |
|
$filteredItems = $this->filterItems($this->arguments, $filters); |
62
|
|
|
|
63
|
12 |
|
$filteredData = $filteredItems; |
64
|
|
|
} |
65
|
|
|
|
66
|
16 |
|
return new FilteredCollection($filteredData, $this->config); |
67
|
|
|
} |
68
|
|
|
|
69
|
28 |
|
public function hasChanged(): bool |
70
|
|
|
{ |
71
|
28 |
|
foreach ($this->arguments as $argument) { |
72
|
28 |
|
if ($argument instanceof MutationObject && $argument->hasChanged()) { |
73
|
26 |
|
$this->hasChanged = true; |
74
|
|
|
} |
75
|
|
|
} |
76
|
|
|
|
77
|
28 |
|
return $this->hasChanged; |
78
|
|
|
} |
79
|
|
|
|
80
|
26 |
|
public function jsonSerialize(): array |
81
|
|
|
{ |
82
|
26 |
|
$items = []; |
83
|
26 |
|
foreach ($this->arguments as $item) { |
84
|
26 |
|
if ($item->hasChanged()) { |
85
|
26 |
|
if ($item instanceof \JsonSerializable) { |
|
|
|
|
86
|
26 |
|
$items[] = $item->jsonSerialize(); |
87
|
|
|
} else { |
88
|
|
|
$items[] = $item; |
89
|
|
|
} |
90
|
|
|
} |
91
|
|
|
} |
92
|
|
|
|
93
|
26 |
|
return $items; |
94
|
|
|
} |
95
|
|
|
|
96
|
20 |
|
private function setItemData(Item $item, array $data) |
97
|
|
|
{ |
98
|
20 |
|
foreach ($data as $key => $value) { |
99
|
20 |
|
$item->{$key} = $value; |
100
|
|
|
} |
101
|
20 |
|
} |
102
|
|
|
|
103
|
|
|
private function filterItems(array $arguments, array $filters): array |
104
|
|
|
{ |
105
|
16 |
|
return array_filter($arguments, function ($item) use ($filters) { |
106
|
16 |
|
foreach ($filters as $filterKey => $filterValue) { |
107
|
16 |
|
if (!$this->matchesFilter($item, $filterKey, $filterValue)) { |
108
|
16 |
|
return false; |
109
|
|
|
} |
110
|
|
|
} |
111
|
|
|
|
112
|
16 |
|
return true; |
113
|
16 |
|
}); |
114
|
|
|
} |
115
|
|
|
|
116
|
16 |
|
private function matchesFilter($item, string $filterKey, $filterValue): bool |
117
|
|
|
{ |
118
|
16 |
|
return (is_null($filterValue) && is_null($item->{$filterKey}) |
119
|
16 |
|
|| !is_null($item->{$filterKey}) && $item->{$filterKey} == $filterValue); |
120
|
|
|
} |
121
|
|
|
} |
122
|
|
|
|
This error could be the result of:
1. Missing dependencies
PHP Analyzer uses your
composer.json
file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects thecomposer.json
to be in the root folder of your repository.Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the
require
orrequire-dev
section?2. Missing use statement
PHP does not complain about undefined classes in
ìnstanceof
checks. For example, the following PHP code will work perfectly fine:If you have not tested against this specific condition, such errors might go unnoticed.