|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Psi\Component\Grid; |
|
6
|
|
|
|
|
7
|
|
|
use Psi\Component\Grid\Metadata\GridMetadata; |
|
8
|
|
|
use Psi\Component\Grid\View\Grid as GridView; |
|
9
|
|
|
use Psi\Component\ObjectAgent\AgentInterface; |
|
10
|
|
|
use Psi\Component\ObjectAgent\Query\Composite; |
|
11
|
|
|
use Psi\Component\ObjectAgent\Query\Query; |
|
12
|
|
|
|
|
13
|
|
|
class GridViewFactory |
|
14
|
|
|
{ |
|
15
|
|
|
/** |
|
16
|
|
|
* @var ColumnFactory |
|
17
|
|
|
*/ |
|
18
|
|
|
private $columnFactory; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* @var FilterFactory |
|
22
|
|
|
*/ |
|
23
|
|
|
private $filterFactory; |
|
24
|
|
|
|
|
25
|
|
|
public function __construct( |
|
26
|
|
|
ColumnFactory $columnFactory, |
|
27
|
|
|
FilterBarFactoryInterface $filterFactory, |
|
28
|
|
|
QueryFactory $queryFactory |
|
29
|
|
|
) { |
|
30
|
|
|
$this->columnFactory = $columnFactory; |
|
31
|
|
|
$this->filterFactory = $filterFactory; |
|
|
|
|
|
|
32
|
|
|
$this->queryFactory = $queryFactory; |
|
|
|
|
|
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
public function createView(AgentInterface $agent, GridContext $gridContext, GridMetadata $gridMetadata): GridView |
|
36
|
|
|
{ |
|
37
|
|
|
// create the filter form based on the metadata and submit any data. |
|
38
|
|
|
$filterForm = $this->filterFactory->createForm($gridMetadata, $agent->getCapabilities()); |
|
39
|
|
|
|
|
40
|
|
|
if ($gridContext->getFilter()) { |
|
41
|
|
|
$filterForm->submit($gridContext->getFilter()); |
|
42
|
|
|
|
|
43
|
|
|
if (false === $filterForm->isValid()) { |
|
44
|
|
|
foreach ($filterForm->getErrors(true) as $name => $error) { |
|
45
|
|
|
$message[] = sprintf( |
|
|
|
|
|
|
46
|
|
|
'%s %s', |
|
47
|
|
|
$error->getOrigin()->getPropertyPath(), |
|
48
|
|
|
$error->getMessage() |
|
49
|
|
|
); |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
throw new \InvalidArgumentException(sprintf( |
|
53
|
|
|
'Invalid filter form: ' . implode(', ', $message) |
|
54
|
|
|
)); |
|
55
|
|
|
} |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
$criteria = $this->filterFactory->createExpression($gridMetadata, $filterForm->getData()); |
|
59
|
|
|
|
|
60
|
|
|
if ($criteria instanceof Composite && empty($criteria->getExpressions())) { |
|
61
|
|
|
$criteria = null; |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
$criteria = [ |
|
65
|
|
|
'criteria' => $criteria, |
|
66
|
|
|
'orderings' => $this->resolveOrderings($gridContext->getOrderings(), $gridMetadata), |
|
67
|
|
|
'firstResult' => $gridContext->getPageOffset(), |
|
68
|
|
|
'maxResults' => $gridContext->isPaginated() ? $gridContext->getPageSize() : null, |
|
69
|
|
|
]; |
|
70
|
|
|
|
|
71
|
|
|
if ($gridMetadata->hasQuery()) { |
|
72
|
|
|
$query = $this->queryFactory->createQuery($agent->getCanonicalClassFqn($gridContext->getClassFqn()), $gridMetadata->getQuery()); |
|
73
|
|
|
$criteria['selects'] = $query->getSelects(); |
|
74
|
|
|
$criteria['joins'] = $query->getJoins(); |
|
75
|
|
|
|
|
76
|
|
|
if ($query->hasExpression()) { |
|
77
|
|
|
if (null === $criteria['criteria']) { |
|
78
|
|
|
$criteria['criteria'] = $query->getExpression(); |
|
79
|
|
|
} else { |
|
80
|
|
|
// filter and user criterias need to be combined |
|
81
|
|
|
$criteria['criteria'] = new Composite(Composite::AND, [$query->getExpression(), $criteria['criteria']]); |
|
82
|
|
|
} |
|
83
|
|
|
} |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
// create the query and get the data collection from the object-agent. |
|
87
|
|
|
$query = Query::create($gridContext->getClassFqn(), $criteria); |
|
88
|
|
|
$collection = new \IteratorIterator($agent->query($query)); |
|
89
|
|
|
|
|
90
|
|
|
return new View\Grid( |
|
91
|
|
|
$gridContext->getClassFqn(), |
|
92
|
|
|
$gridMetadata->getName(), |
|
93
|
|
|
new View\Table($this->columnFactory, $gridMetadata, $gridContext, $collection, $gridContext), |
|
|
|
|
|
|
94
|
|
|
new View\Paginator($gridContext, count($collection), $this->getNumberOfRecords($agent, $query)), |
|
95
|
|
|
new View\FilterBar($filterForm->createView(), $gridContext), |
|
96
|
|
|
new View\ActionBar($gridMetadata) |
|
97
|
|
|
); |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
private function getNumberOfRecords(AgentInterface $agent, Query $query) |
|
101
|
|
|
{ |
|
102
|
|
|
if (false === $agent->getCapabilities()->canQueryCount()) { |
|
103
|
|
|
return; |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
return $agent->queryCount($query); |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
private function resolveOrderings(array $orderings, GridMetadata $metadata) |
|
110
|
|
|
{ |
|
111
|
|
|
$columns = $metadata->getColumns(); |
|
112
|
|
|
$realOrderings = []; |
|
113
|
|
|
|
|
114
|
|
|
foreach ($orderings as $columnName => $direction) { |
|
115
|
|
|
if (!isset($columns[$columnName])) { |
|
116
|
|
|
throw new \RuntimeException(sprintf( |
|
117
|
|
|
'Invalid column "%s"', $columnName |
|
118
|
|
|
)); |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
$column = $columns[$columnName]; |
|
122
|
|
|
$options = $column->getOptions(); |
|
123
|
|
|
|
|
124
|
|
|
if (!isset($options['sort_field'])) { |
|
125
|
|
|
$options['sort_field'] = $columnName; |
|
126
|
|
|
} |
|
127
|
|
|
|
|
128
|
|
|
$realOrderings[$options['sort_field']] = $direction; |
|
129
|
|
|
} |
|
130
|
|
|
|
|
131
|
|
|
return $realOrderings; |
|
132
|
|
|
} |
|
133
|
|
|
} |
|
134
|
|
|
|
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..