1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Softonic\GraphQL\DataObjects\Mutation; |
4
|
|
|
|
5
|
|
|
use Softonic\GraphQL\DataObjects\AbstractCollection; |
6
|
|
|
use Softonic\GraphQL\DataObjects\Mutation\Traits\MutationObjectHandler; |
7
|
|
|
use Softonic\GraphQL\Exceptions\InaccessibleArgumentException; |
8
|
|
|
|
9
|
|
|
class FilteredCollection extends AbstractCollection implements MutationObject |
10
|
|
|
{ |
11
|
|
|
use MutationObjectHandler; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* @var array |
15
|
|
|
*/ |
16
|
|
|
protected $config; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* @var bool |
20
|
|
|
*/ |
21
|
|
|
protected $hasChanged = false; |
22
|
|
|
|
23
|
84 |
|
public function __construct(array $arguments = [], array $config = [], bool $hasChanged = false) |
24
|
|
|
{ |
25
|
84 |
|
parent::__construct($arguments); |
26
|
|
|
|
27
|
84 |
|
$this->config = $config; |
28
|
84 |
|
$this->hasChanged = $hasChanged; |
29
|
84 |
|
} |
30
|
|
|
|
31
|
|
|
|
32
|
40 |
|
public function __get(string $key): Collection |
33
|
|
|
{ |
34
|
40 |
|
if (empty($this->arguments)) { |
35
|
2 |
|
throw InaccessibleArgumentException::fromEmptyArguments($key); |
36
|
|
|
} |
37
|
|
|
|
38
|
38 |
|
$items = []; |
39
|
38 |
|
foreach ($this->arguments as $argument) { |
40
|
38 |
|
$items[] = $argument->{$key}; |
41
|
|
|
} |
42
|
|
|
|
43
|
38 |
|
return new Collection($items, $this->config[$key]->children); |
44
|
|
|
} |
45
|
|
|
|
46
|
24 |
|
public function set(array $data): void |
47
|
|
|
{ |
48
|
24 |
|
foreach ($this->arguments as $argument) { |
49
|
24 |
|
$argument->set($data); |
50
|
|
|
} |
51
|
24 |
|
} |
52
|
|
|
|
53
|
44 |
|
public function jsonSerialize(): array |
54
|
|
|
{ |
55
|
44 |
|
if (!$this->hasChildren()) { |
56
|
|
|
return []; |
57
|
|
|
} |
58
|
|
|
|
59
|
44 |
|
$items = []; |
60
|
44 |
|
foreach ($this->arguments as $item) { |
61
|
44 |
|
if ($item->hasChanged()) { |
62
|
44 |
|
$items[] = $item->jsonSerialize(); |
63
|
|
|
} |
64
|
|
|
} |
65
|
|
|
|
66
|
44 |
|
return $items; |
67
|
|
|
} |
68
|
|
|
|
69
|
22 |
|
protected function buildFilteredCollection($data) |
70
|
|
|
{ |
71
|
22 |
|
return new FilteredCollection($data, $this->config); |
72
|
|
|
} |
73
|
|
|
|
74
|
4 |
|
public function __unset($key): void |
75
|
|
|
{ |
76
|
4 |
|
foreach ($this->arguments as $argument) { |
77
|
4 |
|
unset($argument->{$key}); |
78
|
|
|
} |
79
|
4 |
|
} |
80
|
|
|
|
81
|
2 |
|
public function remove(Item $item): void |
82
|
|
|
{ |
83
|
2 |
|
foreach ($this->arguments as $key => $argument) { |
84
|
2 |
|
if ($argument instanceof Collection) { |
85
|
2 |
|
$argument->remove($item); |
86
|
2 |
|
} elseif ($argument === $item) { |
87
|
2 |
|
unset($this->arguments[$key]); |
88
|
|
|
} |
89
|
|
|
} |
90
|
2 |
|
} |
91
|
|
|
} |
92
|
|
|
|