1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Psi\Component\Grid; |
4
|
|
|
|
5
|
|
|
use Psi\Component\Grid\Cell\View\SelectView; |
6
|
|
|
use Psi\Component\Grid\Metadata\GridMetadata; |
7
|
|
|
use Psi\Component\Grid\View\ActionBar; |
8
|
|
|
use Psi\Component\ObjectAgent\AgentInterface; |
9
|
|
|
use Psi\Component\ObjectAgent\Query\Query; |
10
|
|
|
|
11
|
|
|
class Grid |
12
|
|
|
{ |
13
|
|
|
private $agent; |
14
|
|
|
private $gridContext; |
15
|
|
|
private $gridMetadata; |
16
|
|
|
private $actionPerformer; |
17
|
|
|
|
18
|
|
|
public function __construct( |
19
|
|
|
GridViewFactory $gridViewFactory, |
20
|
|
|
ActionPerformer $actionPerformer, |
21
|
|
|
AgentInterface $agent, |
22
|
|
|
GridContext $gridContext, |
23
|
|
|
GridMetadata $gridMetadata |
24
|
|
|
) { |
25
|
|
|
$this->agent = $agent; |
26
|
|
|
$this->gridContext = $gridContext; |
27
|
|
|
$this->gridMetadata = $gridMetadata; |
28
|
|
|
$this->gridViewFactory = $gridViewFactory; |
|
|
|
|
29
|
|
|
$this->actionPerformer = $actionPerformer; |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
public function createView() |
33
|
|
|
{ |
34
|
|
|
return $this->gridViewFactory->createView( |
35
|
|
|
$this->agent, |
36
|
|
|
$this->gridContext, |
37
|
|
|
$this->gridMetadata |
38
|
|
|
); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
public function performActionFromPostData(array $postData) |
42
|
|
|
{ |
43
|
|
|
$required = [ |
44
|
|
|
ActionBar::INPUT_NAME, |
45
|
|
|
SelectView::INPUT_NAME, |
46
|
|
|
]; |
47
|
|
|
|
48
|
|
View Code Duplication |
if ($diff = array_diff($required, array_keys($postData))) { |
|
|
|
|
49
|
|
|
throw new \InvalidArgumentException(sprintf( |
50
|
|
|
'Expected all keys "%s" in post data, but (only) got "%s"', |
51
|
|
|
implode('", "', $required), implode('", "', array_keys($postData)) |
52
|
|
|
)); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
$actionName = $postData[ActionBar::INPUT_NAME]; |
56
|
|
|
$selectedIdentifiers = array_keys($postData[SelectView::INPUT_NAME]); |
57
|
|
|
|
58
|
|
|
$this->performAction($actionName, $selectedIdentifiers); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
public function performAction(string $actionName, array $selectedIdentifiers) |
62
|
|
|
{ |
63
|
|
|
$this->actionPerformer->perform( |
64
|
|
|
$this->agent, |
65
|
|
|
$this->gridMetadata, |
66
|
|
|
$actionName, |
67
|
|
|
$selectedIdentifiers |
68
|
|
|
); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
private function getNumberOfRecords(Query $query) |
|
|
|
|
72
|
|
|
{ |
73
|
|
|
if (false === $this->agent->getCapabilities()->canQueryCount()) { |
74
|
|
|
return; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
return $this->agent->queryCount($query); |
78
|
|
|
} |
79
|
|
|
} |
80
|
|
|
|
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: