1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
/* |
6
|
|
|
* This file is part of the Sonata Project package. |
7
|
|
|
* |
8
|
|
|
* (c) Thomas Rabaix <[email protected]> |
9
|
|
|
* |
10
|
|
|
* For the full copyright and license information, please view the LICENSE |
11
|
|
|
* file that was distributed with this source code. |
12
|
|
|
*/ |
13
|
|
|
|
14
|
|
|
namespace Sonata\AdminBundle\Datagrid; |
15
|
|
|
|
16
|
|
|
use Sonata\AdminBundle\Admin\AdminInterface; |
17
|
|
|
use Sonata\AdminBundle\Admin\FieldDescriptionInterface; |
18
|
|
|
use Sonata\AdminBundle\Builder\DatagridBuilderInterface; |
19
|
|
|
use Sonata\AdminBundle\Filter\FilterInterface; |
20
|
|
|
use Sonata\AdminBundle\Mapper\BaseMapper; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* This class is use to simulate the Form API. |
24
|
|
|
* |
25
|
|
|
* @final since sonata-project/admin-bundle 3.52 |
26
|
|
|
* |
27
|
|
|
* @author Thomas Rabaix <[email protected]> |
28
|
|
|
*/ |
29
|
|
|
class DatagridMapper extends BaseMapper |
30
|
|
|
{ |
31
|
|
|
/** |
32
|
|
|
* @var DatagridInterface |
33
|
|
|
*/ |
34
|
|
|
protected $datagrid; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @var DatagridBuilderInterface |
38
|
|
|
*/ |
39
|
|
|
protected $builder; |
40
|
|
|
|
41
|
|
|
public function __construct( |
42
|
|
|
DatagridBuilderInterface $datagridBuilder, |
43
|
|
|
DatagridInterface $datagrid, |
44
|
|
|
AdminInterface $admin |
45
|
|
|
) { |
46
|
|
|
parent::__construct($datagridBuilder, $admin); |
47
|
|
|
$this->datagrid = $datagrid; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @param FieldDescriptionInterface|string $name |
52
|
|
|
* @param array<string, mixed> $filterOptions |
53
|
|
|
* @param array<string, mixed>|null $fieldOptions |
54
|
|
|
* @param array<string, mixed> $fieldDescriptionOptions |
55
|
|
|
* |
56
|
|
|
* @throws \LogicException |
57
|
|
|
*/ |
58
|
|
|
public function add( |
59
|
|
|
$name, |
60
|
|
|
?string $type = null, |
61
|
|
|
array $filterOptions = [], |
62
|
|
|
?string $fieldType = null, |
63
|
|
|
?array $fieldOptions = null, |
64
|
|
|
array $fieldDescriptionOptions = [] |
65
|
|
|
): self { |
66
|
|
|
if (null !== $fieldOptions) { |
67
|
|
|
$filterOptions['field_options'] = $fieldOptions; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
if (null !== $fieldType) { |
71
|
|
|
$filterOptions['field_type'] = $fieldType; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
if ($name instanceof FieldDescriptionInterface) { |
75
|
|
|
$fieldDescription = $name; |
76
|
|
|
$fieldDescription->mergeOptions($filterOptions); |
77
|
|
|
} elseif (\is_string($name)) { |
78
|
|
|
if ($this->admin->hasFilterFieldDescription($name)) { |
79
|
|
|
throw new \LogicException(sprintf( |
80
|
|
|
'Duplicate field name "%s" in datagrid mapper. Names should be unique.', |
81
|
|
|
$name |
82
|
|
|
)); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
if (!isset($filterOptions['field_name'])) { |
86
|
|
|
$filterOptions['field_name'] = substr(strrchr('.'.$name, '.'), 1); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
$fieldDescription = $this->admin->getModelManager()->getNewFieldDescriptionInstance( |
90
|
|
|
$this->admin->getClass(), |
91
|
|
|
$name, |
92
|
|
|
array_merge($filterOptions, $fieldDescriptionOptions) |
93
|
|
|
); |
94
|
|
|
} else { |
95
|
|
|
throw new \TypeError( |
96
|
|
|
'Unknown field name in datagrid mapper.' |
|
|
|
|
97
|
|
|
.' Field name should be either of FieldDescriptionInterface interface or string.' |
98
|
|
|
); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
if (!isset($fieldDescriptionOptions['role']) || $this->admin->isGranted($fieldDescriptionOptions['role'])) { |
102
|
|
|
// add the field with the DatagridBuilder |
103
|
|
|
$this->builder->addFilter($this->datagrid, $type, $fieldDescription, $this->admin); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
return $this; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
public function get(string $name): FilterInterface |
110
|
|
|
{ |
111
|
|
|
return $this->datagrid->getFilter($name); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
public function has(string $key): bool |
115
|
|
|
{ |
116
|
|
|
return $this->datagrid->hasFilter($key); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
final public function keys(): array |
120
|
|
|
{ |
121
|
|
|
return array_keys($this->datagrid->getFilters()); |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
public function remove(string $key): self |
125
|
|
|
{ |
126
|
|
|
$this->admin->removeFilterFieldDescription($key); |
127
|
|
|
$this->datagrid->removeFilter($key); |
128
|
|
|
|
129
|
|
|
return $this; |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
public function reorder(array $keys): self |
133
|
|
|
{ |
134
|
|
|
$this->datagrid->reorderFilters($keys); |
135
|
|
|
|
136
|
|
|
return $this; |
137
|
|
|
} |
138
|
|
|
} |
139
|
|
|
|
This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.
If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.
In this case you can add the
@ignore
PhpDoc annotation to the duplicate definition and it will be ignored.