These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
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\FieldDescriptionCollection; |
||
18 | use Sonata\AdminBundle\Admin\FieldDescriptionInterface; |
||
19 | use Sonata\AdminBundle\Builder\ListBuilderInterface; |
||
20 | use Sonata\AdminBundle\Mapper\BaseMapper; |
||
21 | |||
22 | /** |
||
23 | * This class is used to simulate the Form API. |
||
24 | * |
||
25 | * @author Thomas Rabaix <[email protected]> |
||
26 | */ |
||
27 | class ListMapper extends BaseMapper |
||
28 | { |
||
29 | /** |
||
30 | * @var FieldDescriptionCollection |
||
31 | */ |
||
32 | protected $list; |
||
33 | |||
34 | public function __construct( |
||
35 | ListBuilderInterface $listBuilder, |
||
36 | FieldDescriptionCollection $list, |
||
37 | AdminInterface $admin |
||
38 | ) { |
||
39 | parent::__construct($listBuilder, $admin); |
||
40 | $this->list = $list; |
||
41 | } |
||
42 | |||
43 | /** |
||
44 | * @param string $name |
||
45 | * @param string|null $type |
||
46 | * |
||
47 | * @return $this |
||
48 | */ |
||
49 | public function addIdentifier($name, $type = null, array $fieldDescriptionOptions = []) |
||
50 | { |
||
51 | $fieldDescriptionOptions['identifier'] = true; |
||
52 | |||
53 | if (!isset($fieldDescriptionOptions['route']['name'])) { |
||
54 | $routeName = ($this->admin->hasAccess('edit') && $this->admin->hasRoute('edit')) ? 'edit' : 'show'; |
||
55 | $fieldDescriptionOptions['route']['name'] = $routeName; |
||
56 | } |
||
57 | |||
58 | if (!isset($fieldDescriptionOptions['route']['parameters'])) { |
||
59 | $fieldDescriptionOptions['route']['parameters'] = []; |
||
60 | } |
||
61 | |||
62 | return $this->add($name, $type, $fieldDescriptionOptions); |
||
63 | } |
||
64 | |||
65 | /** |
||
66 | * @param string $name |
||
67 | * @param string|null $type |
||
68 | * |
||
69 | * @throws \RuntimeException |
||
70 | * |
||
71 | * @return $this |
||
72 | */ |
||
73 | public function add($name, $type = null, array $fieldDescriptionOptions = []) |
||
74 | { |
||
75 | // Ensure batch and action pseudo-fields are tagged as virtual |
||
76 | if (\in_array($type, ['actions', 'batch', 'select'], true)) { |
||
77 | $fieldDescriptionOptions['virtual_field'] = true; |
||
78 | } |
||
79 | |||
80 | if ($name instanceof FieldDescriptionInterface) { |
||
81 | $fieldDescription = $name; |
||
82 | $fieldDescription->mergeOptions($fieldDescriptionOptions); |
||
83 | } elseif (\is_string($name)) { |
||
84 | if ($this->admin->hasListFieldDescription($name)) { |
||
85 | throw new \RuntimeException(sprintf( |
||
86 | 'Duplicate field name "%s" in list mapper. Names should be unique.', |
||
87 | $name |
||
88 | )); |
||
89 | } |
||
90 | |||
91 | $fieldDescription = $this->admin->getModelManager()->getNewFieldDescriptionInstance( |
||
92 | $this->admin->getClass(), |
||
93 | $name, |
||
94 | $fieldDescriptionOptions |
||
95 | ); |
||
96 | } else { |
||
97 | throw new \RuntimeException( |
||
98 | 'Unknown field name in list mapper. ' |
||
99 | .'Field name should be either of FieldDescriptionInterface interface or string.' |
||
100 | ); |
||
101 | } |
||
102 | |||
103 | if (null === $fieldDescription->getLabel()) { |
||
104 | $fieldDescription->setOption( |
||
105 | 'label', |
||
106 | $this->admin->getLabelTranslatorStrategy()->getLabel($fieldDescription->getName(), 'list', 'label') |
||
107 | ); |
||
108 | } |
||
109 | |||
110 | if (isset($fieldDescriptionOptions['header_style'])) { |
||
111 | @trigger_error( |
||
0 ignored issues
–
show
|
|||
112 | 'The "header_style" option is deprecated, please, use "header_class" option instead.', |
||
113 | E_USER_DEPRECATED |
||
114 | ); |
||
115 | } |
||
116 | |||
117 | // add the field with the FormBuilder |
||
118 | $this->builder->addField($this->list, $type, $fieldDescription, $this->admin); |
||
119 | |||
120 | return $this; |
||
121 | } |
||
122 | |||
123 | public function get($name) |
||
124 | { |
||
125 | return $this->list->get($name); |
||
126 | } |
||
127 | |||
128 | public function has($key) |
||
129 | { |
||
130 | return $this->list->has($key); |
||
131 | } |
||
132 | |||
133 | public function remove($key) |
||
134 | { |
||
135 | $this->admin->removeListFieldDescription($key); |
||
136 | $this->list->remove($key); |
||
137 | |||
138 | return $this; |
||
139 | } |
||
140 | |||
141 | final public function keys() |
||
142 | { |
||
143 | return array_keys($this->list->getElements()); |
||
144 | } |
||
145 | |||
146 | public function reorder(array $keys) |
||
147 | { |
||
148 | $this->list->reorder($keys); |
||
149 | |||
150 | return $this; |
||
151 | } |
||
152 | } |
||
153 |
If you suppress an error, we recommend checking for the error condition explicitly: