1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Psi\Component\Grid; |
4
|
|
|
|
5
|
|
|
use Psi\Component\ObjectAgent\AgentInterface; |
6
|
|
|
use Psi\Component\Grid\GridContext; |
7
|
|
|
use Psi\Component\Grid\Metadata\GridMetadata; |
8
|
|
|
use Psi\Component\ObjectAgent\Query\Query; |
9
|
|
|
|
10
|
|
|
class GridViewFactory |
11
|
|
|
{ |
12
|
|
|
public function __construct( |
13
|
|
|
CellFactory $cellFactory, |
14
|
|
|
FilterBarFactory $filterFactory |
15
|
|
|
) |
16
|
|
|
{ |
17
|
|
|
$this->cellFactory = $cellFactory; |
|
|
|
|
18
|
|
|
$this->filterFactory = $filterFactory; |
|
|
|
|
19
|
|
|
} |
20
|
|
|
|
21
|
|
|
public function createView(AgentInterface $agent, GridContext $gridContext, GridMetadata $gridMetadata) |
22
|
|
|
{ |
23
|
|
|
// create the filter form based on the metadata and submit any data. |
24
|
|
|
$filterForm = $this->filterFactory->createForm($gridMetadata, $agent->getCapabilities()); |
25
|
|
|
$filterForm->submit($gridContext->getFilter()); |
26
|
|
|
|
27
|
|
|
// create the query and get the data collection from the object-agent. |
28
|
|
|
$query = Query::create( |
29
|
|
|
$gridContext->getClassFqn(), |
30
|
|
|
$this->filterFactory->createExpression($gridMetadata, $filterForm->getData()), |
31
|
|
|
$gridContext->getOrderings(), |
32
|
|
|
$gridContext->getPageOffset(), |
33
|
|
|
$gridContext->getPageSize() |
34
|
|
|
); |
35
|
|
|
$collection = new \IteratorIterator($agent->query($query)); |
36
|
|
|
|
37
|
|
|
return new View\Grid( |
38
|
|
|
$gridContext->getClassFqn(), |
39
|
|
|
$gridMetadata->getName(), |
40
|
|
|
new View\Table($this->cellFactory, $gridMetadata, $collection, $gridContext), |
41
|
|
|
new View\Paginator($gridContext, count($collection), $this->getNumberOfRecords($agent, $query)), |
42
|
|
|
new View\FilterBar($filterForm->createView(), $gridContext), |
43
|
|
|
new View\ActionBar($gridMetadata) |
44
|
|
|
); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
private function getNumberOfRecords(AgentInterface $agent, Query $query) |
48
|
|
|
{ |
49
|
|
|
if (false === $agent->getCapabilities()->canQueryCount()) { |
50
|
|
|
return; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
return $agent->queryCount($query); |
54
|
|
|
} |
55
|
|
|
} |
56
|
|
|
|
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: