1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Psi\Component\Grid; |
6
|
|
|
|
7
|
|
|
use Metadata\MetadataFactory; |
8
|
|
|
use Psi\Component\Grid\Metadata\GridMetadata; |
9
|
|
|
use Psi\Component\ObjectAgent\AgentFinder; |
10
|
|
|
use Psi\Component\ObjectAgent\AgentInterface; |
11
|
|
|
use Psi\Component\ObjectAgent\Query\Query; |
12
|
|
|
use Psi\Component\Grid\Grid; |
13
|
|
|
use Psi\Component\Grid\GridViewFactory; |
14
|
|
|
|
15
|
|
|
class GridFactory |
16
|
|
|
{ |
17
|
|
|
private $agentFinder; |
18
|
|
|
private $metadataFactory; |
19
|
|
|
private $cellFactory; |
20
|
|
|
private $filterFactory; |
21
|
|
|
private $actionPerformer; |
22
|
|
|
|
23
|
|
|
public function __construct( |
24
|
|
|
AgentFinder $agentFinder, |
25
|
|
|
MetadataFactory $metadataFactory, |
26
|
|
|
GridViewFactory $gridViewFactory, |
27
|
|
|
ActionPerformer $actionPerformer |
28
|
|
|
) { |
29
|
|
|
$this->agentFinder = $agentFinder; |
30
|
|
|
$this->metadataFactory = $metadataFactory; |
31
|
|
|
$this->gridViewFactory = $gridViewFactory; |
|
|
|
|
32
|
|
|
$this->actionPerformer = $actionPerformer; |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
public function loadGrid(string $classFqn, array $context): Grid |
36
|
|
|
{ |
37
|
|
|
$context = new GridContext($classFqn, $context); |
38
|
|
|
|
39
|
|
|
try { |
40
|
|
|
return $this->doLoadGrid($context); |
41
|
|
|
} catch (\Exception $exception) { |
42
|
|
|
throw new \InvalidArgumentException(sprintf( |
43
|
|
|
'Could not load grid for class "%s"', $classFqn |
44
|
|
|
), 0, $exception); |
45
|
|
|
} |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
private function doLoadGrid(GridContext $context): Grid |
49
|
|
|
{ |
50
|
|
|
if (null === $metadata = $this->metadataFactory->getMetadataForClass($context->getClassFqn())) { |
51
|
|
|
throw new \InvalidArgumentException('Could not locate grid metadata'); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
// find the agent and get the grid metadata. |
55
|
|
|
$agent = $this->agentFinder->findFor($context->getClassFqn()); |
56
|
|
|
$gridMetadata = $this->resolveGridMetadata($metadata->getGrids(), $context->getVariant()); |
57
|
|
|
|
58
|
|
|
return new Grid( |
59
|
|
|
$this->gridViewFactory, |
60
|
|
|
$this->actionPerformer, |
61
|
|
|
$agent, |
62
|
|
|
$context, |
63
|
|
|
$gridMetadata |
64
|
|
|
); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
private function resolveGridMetadata(array $grids, string $variant = null) |
68
|
|
|
{ |
69
|
|
|
if (empty($grids)) { |
70
|
|
|
throw new \InvalidArgumentException('No grid variants are available'); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
// if no explicit grid variant is requested, return the first one that |
74
|
|
|
// was defined. |
75
|
|
|
if (null === $variant) { |
76
|
|
|
return reset($grids); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
if (!isset($grids[$variant])) { |
80
|
|
|
throw new \InvalidArgumentException(sprintf( |
81
|
|
|
'Unknown grid variant "%s", available variants: "%s"', |
82
|
|
|
implode('", "', array_keys($grids)) |
83
|
|
|
)); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
return $grids[$variant]; |
87
|
|
|
} |
88
|
|
|
} |
89
|
|
|
|
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: