1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Psi\Component\Grid\Metadata; |
6
|
|
|
|
7
|
|
|
final class GridMetadata |
8
|
|
|
{ |
9
|
|
|
private $name; |
10
|
|
|
private $columns; |
11
|
|
|
private $pageSize; |
12
|
|
|
private $filters; |
13
|
|
|
private $actions; |
14
|
|
|
private $query; |
15
|
|
|
private $classMetadata; |
16
|
|
|
|
17
|
|
|
public function __construct(string $name, array $columns, array $filters, array $actions, int $pageSize, string $query = null) |
18
|
|
|
{ |
19
|
|
|
$this->name = $name; |
20
|
|
|
$this->columns = $columns; |
21
|
|
|
$this->filters = $filters; |
22
|
|
|
$this->actions = $actions; |
23
|
|
|
$this->pageSize = $pageSize; |
24
|
|
|
$this->query = $query; |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
public function getName(): string |
28
|
|
|
{ |
29
|
|
|
return $this->name; |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
public function getColumns(): array |
33
|
|
|
{ |
34
|
|
|
return $this->columns; |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
public function filterColumns(\Closure $closure) |
38
|
|
|
{ |
39
|
|
|
$instance = clone $this; |
40
|
|
|
$instance->columns = array_filter($this->columns, $closure); |
41
|
|
|
|
42
|
|
|
return $instance; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
public function getPageSize(): int |
46
|
|
|
{ |
47
|
|
|
return $this->pageSize; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
public function getFilters(): array |
51
|
|
|
{ |
52
|
|
|
return $this->filters; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
public function filterFilters(\Closure $closure) |
56
|
|
|
{ |
57
|
|
|
$instance = clone $this; |
58
|
|
|
$instance->filters = array_filter($this->filters, $closure); |
59
|
|
|
|
60
|
|
|
return $instance; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
public function getActions(): array |
64
|
|
|
{ |
65
|
|
|
return $this->actions; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
public function filterActions(\Closure $closure) |
69
|
|
|
{ |
70
|
|
|
$instance = clone $this; |
71
|
|
|
$instance->actions = array_filter($this->actions, $closure); |
72
|
|
|
|
73
|
|
|
return $instance; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
public function attachClassMetadata(ClassMetadata $classMetadata) |
77
|
|
|
{ |
78
|
|
|
if ($this->classMetadata) { |
79
|
|
|
throw new \RuntimeException( |
80
|
|
|
'Class metadata has already been attached to grid metadata.' |
81
|
|
|
); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
$this->classMetadata = $classMetadata; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
public function getClassMetadata() |
88
|
|
|
{ |
89
|
|
|
if (!$this->classMetadata) { |
90
|
|
|
throw new \RuntimeException('Class metadata has not been attached to grid.'); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
return $this->classMetadata; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
public function hasQuery(): bool |
97
|
|
|
{ |
98
|
|
|
return null !== $this->query; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
public function getQuery(): string |
102
|
|
|
{ |
103
|
|
|
return $this->query; |
104
|
|
|
} |
105
|
|
|
} |
106
|
|
|
|