Completed
Push — master ( bc462e...3c2183 )
by Daniel
07:13
created

GridMetadata::hasQuery()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

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