|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Psi\Component\Grid; |
|
4
|
|
|
|
|
5
|
|
|
use Psi\Component\Grid\Metadata\GridMetadata; |
|
6
|
|
|
use Psi\Component\ObjectAgent\AgentInterface; |
|
7
|
|
|
use Psi\Component\ObjectAgent\Query\Query; |
|
8
|
|
|
|
|
9
|
|
|
class GridViewFactory |
|
10
|
|
|
{ |
|
11
|
|
|
public function __construct( |
|
12
|
|
|
CellFactory $cellFactory, |
|
13
|
|
|
FilterBarFactory $filterFactory |
|
14
|
|
|
) { |
|
15
|
|
|
$this->cellFactory = $cellFactory; |
|
|
|
|
|
|
16
|
|
|
$this->filterFactory = $filterFactory; |
|
|
|
|
|
|
17
|
|
|
} |
|
18
|
|
|
|
|
19
|
|
|
public function createView(AgentInterface $agent, GridContext $gridContext, GridMetadata $gridMetadata) |
|
20
|
|
|
{ |
|
21
|
|
|
// create the filter form based on the metadata and submit any data. |
|
22
|
|
|
$filterForm = $this->filterFactory->createForm($gridMetadata, $agent->getCapabilities()); |
|
23
|
|
|
$filterForm->submit($gridContext->getFilter()); |
|
24
|
|
|
|
|
25
|
|
|
// create the query and get the data collection from the object-agent. |
|
26
|
|
|
$query = Query::create( |
|
27
|
|
|
$gridContext->getClassFqn(), |
|
28
|
|
|
$this->filterFactory->createExpression($gridMetadata, $filterForm->getData()), |
|
29
|
|
|
$gridContext->getOrderings(), |
|
30
|
|
|
$gridContext->getPageOffset(), |
|
31
|
|
|
$gridContext->getPageSize() |
|
32
|
|
|
); |
|
33
|
|
|
$collection = new \IteratorIterator($agent->query($query)); |
|
34
|
|
|
|
|
35
|
|
|
return new View\Grid( |
|
36
|
|
|
$gridContext->getClassFqn(), |
|
37
|
|
|
$gridMetadata->getName(), |
|
38
|
|
|
new View\Table($this->cellFactory, $gridMetadata, $collection, $gridContext), |
|
39
|
|
|
new View\Paginator($gridContext, count($collection), $this->getNumberOfRecords($agent, $query)), |
|
40
|
|
|
new View\FilterBar($filterForm->createView(), $gridContext), |
|
41
|
|
|
new View\ActionBar($gridMetadata) |
|
42
|
|
|
); |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
private function getNumberOfRecords(AgentInterface $agent, Query $query) |
|
46
|
|
|
{ |
|
47
|
|
|
if (false === $agent->getCapabilities()->canQueryCount()) { |
|
48
|
|
|
return; |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
return $agent->queryCount($query); |
|
52
|
|
|
} |
|
53
|
|
|
} |
|
54
|
|
|
|
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: