1 | <?php |
||
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 (\array_key_exists('identifier', $fieldDescriptionOptions) && !\is_bool($fieldDescriptionOptions['identifier'])) { |
||
81 | throw new \InvalidArgumentException(sprintf('Value for "identifier" option must be boolean, %s given.', \gettype($fieldDescriptionOptions['identifier']))); |
||
82 | } |
||
83 | |||
84 | if ($name instanceof FieldDescriptionInterface) { |
||
85 | $fieldDescription = $name; |
||
86 | $fieldDescription->mergeOptions($fieldDescriptionOptions); |
||
87 | } elseif (\is_string($name)) { |
||
88 | if ($this->admin->hasListFieldDescription($name)) { |
||
89 | throw new \RuntimeException(sprintf( |
||
90 | 'Duplicate field name "%s" in list mapper. Names should be unique.', |
||
91 | $name |
||
92 | )); |
||
93 | } |
||
94 | |||
95 | $fieldDescription = $this->admin->getModelManager()->getNewFieldDescriptionInstance( |
||
96 | $this->admin->getClass(), |
||
97 | $name, |
||
98 | $fieldDescriptionOptions |
||
99 | ); |
||
100 | } else { |
||
101 | throw new \RuntimeException( |
||
102 | 'Unknown field name in list mapper. ' |
||
103 | .'Field name should be either of FieldDescriptionInterface interface or string.' |
||
104 | ); |
||
105 | } |
||
106 | |||
107 | if (null === $fieldDescription->getLabel()) { |
||
108 | $fieldDescription->setOption( |
||
109 | 'label', |
||
110 | $this->admin->getLabelTranslatorStrategy()->getLabel($fieldDescription->getName(), 'list', 'label') |
||
111 | ); |
||
112 | } |
||
113 | |||
114 | if (isset($fieldDescriptionOptions['header_style'])) { |
||
115 | @trigger_error( |
||
|
|||
116 | 'The "header_style" option is deprecated, please, use "header_class" option instead.', |
||
117 | E_USER_DEPRECATED |
||
118 | ); |
||
119 | } |
||
120 | |||
121 | if (!isset($fieldDescriptionOptions['role']) || $this->admin->isGranted($fieldDescriptionOptions['role'])) { |
||
122 | // add the field with the FormBuilder |
||
123 | $this->builder->addField($this->list, $type, $fieldDescription, $this->admin); |
||
124 | } |
||
125 | |||
126 | return $this; |
||
127 | } |
||
128 | |||
129 | public function get($name) |
||
133 | |||
134 | public function has($key) |
||
138 | |||
139 | public function remove($key) |
||
146 | |||
147 | final public function keys() |
||
151 | |||
152 | public function reorder(array $keys) |
||
158 | } |
||
159 |
If you suppress an error, we recommend checking for the error condition explicitly: