1 | <?php |
||
29 | class ShowMapper extends BaseGroupedMapper |
||
30 | { |
||
31 | protected $list; |
||
32 | |||
33 | /** |
||
34 | * @var ShowBuilderInterface |
||
35 | */ |
||
36 | protected $builder; |
||
37 | |||
38 | public function __construct( |
||
39 | ShowBuilderInterface $showBuilder, |
||
40 | FieldDescriptionCollection $list, |
||
41 | AdminInterface $admin |
||
42 | ) { |
||
43 | parent::__construct($showBuilder, $admin); |
||
44 | $this->list = $list; |
||
45 | } |
||
46 | |||
47 | /** |
||
48 | * @param FieldDescriptionInterface|string $name |
||
49 | * |
||
50 | * @throws \LogicException |
||
51 | */ |
||
52 | public function add($name, ?string $type = null, array $fieldDescriptionOptions = []): self |
||
53 | { |
||
54 | if (!$this->shouldApply()) { |
||
55 | return $this; |
||
56 | } |
||
57 | |||
58 | if ($name instanceof FieldDescriptionInterface) { |
||
59 | $fieldDescription = $name; |
||
60 | $fieldDescription->mergeOptions($fieldDescriptionOptions); |
||
61 | } elseif (\is_string($name)) { |
||
62 | if (!$this->admin->hasShowFieldDescription($name)) { |
||
63 | $fieldDescription = $this->admin->getModelManager()->getNewFieldDescriptionInstance( |
||
64 | $this->admin->getClass(), |
||
65 | $name, |
||
66 | $fieldDescriptionOptions |
||
67 | ); |
||
68 | } else { |
||
69 | throw new \LogicException(sprintf( |
||
70 | 'Duplicate field name "%s" in show mapper. Names should be unique.', |
||
71 | $name |
||
72 | )); |
||
73 | } |
||
74 | } else { |
||
75 | throw new \TypeError( |
||
76 | 'Unknown field name in show mapper.' |
||
|
|||
77 | .' Field name should be either of FieldDescriptionInterface interface or string.' |
||
78 | ); |
||
79 | } |
||
80 | |||
81 | $fieldKey = ($name instanceof FieldDescriptionInterface) ? $name->getName() : $name; |
||
82 | |||
83 | $this->addFieldToCurrentGroup($fieldKey); |
||
84 | |||
85 | if (null === $fieldDescription->getLabel()) { |
||
86 | $fieldDescription->setOption('label', $this->admin->getLabelTranslatorStrategy()->getLabel($fieldDescription->getName(), 'show', 'label')); |
||
87 | } |
||
88 | |||
89 | $fieldDescription->setOption('safe', $fieldDescription->getOption('safe', false)); |
||
90 | |||
91 | if (!isset($fieldDescriptionOptions['role']) || $this->admin->isGranted($fieldDescriptionOptions['role'])) { |
||
92 | // add the field with the FormBuilder |
||
93 | $this->builder->addField($this->list, $type, $fieldDescription, $this->admin); |
||
94 | } |
||
95 | |||
96 | return $this; |
||
97 | } |
||
98 | |||
99 | public function get(string $name): FieldDescriptionInterface |
||
100 | { |
||
101 | return $this->list->get($name); |
||
102 | } |
||
103 | |||
104 | public function has(string $key): bool |
||
105 | { |
||
106 | return $this->list->has($key); |
||
107 | } |
||
108 | |||
109 | public function remove(string $key): self |
||
110 | { |
||
111 | $this->admin->removeShowFieldDescription($key); |
||
112 | $this->list->remove($key); |
||
113 | |||
114 | return $this; |
||
115 | } |
||
116 | |||
117 | /** |
||
118 | * Removes a group. |
||
119 | * |
||
120 | * @param string $group The group to delete |
||
121 | * @param string $tab The tab the group belongs to, defaults to 'default' |
||
122 | * @param bool $deleteEmptyTab Whether or not the parent Tab should be deleted too, |
||
123 | * when the deleted group leaves the tab empty after deletion |
||
124 | */ |
||
125 | public function removeGroup(string $group, string $tab = 'default', bool $deleteEmptyTab = false): self |
||
126 | { |
||
127 | $groups = $this->getGroups(); |
||
128 | |||
129 | // When the default tab is used, the tabname is not prepended to the index in the group array |
||
130 | if ('default' !== $tab) { |
||
131 | $group = sprintf('%s.%s', $tab, $group); |
||
132 | } |
||
133 | |||
134 | if (isset($groups[$group])) { |
||
135 | foreach ($groups[$group]['fields'] as $field) { |
||
136 | $this->remove($field); |
||
137 | } |
||
138 | } |
||
139 | unset($groups[$group]); |
||
140 | |||
141 | $tabs = $this->getTabs(); |
||
142 | $key = array_search($group, $tabs[$tab]['groups'], true); |
||
143 | |||
144 | if (false !== $key) { |
||
145 | unset($tabs[$tab]['groups'][$key]); |
||
146 | } |
||
147 | if ($deleteEmptyTab && 0 === \count($tabs[$tab]['groups'])) { |
||
148 | unset($tabs[$tab]); |
||
149 | } |
||
150 | |||
151 | $this->setTabs($tabs); |
||
152 | $this->setGroups($groups); |
||
153 | |||
154 | return $this; |
||
155 | } |
||
156 | |||
157 | final public function keys(): array |
||
158 | { |
||
159 | return array_keys($this->list->getElements()); |
||
160 | } |
||
161 | |||
162 | public function reorder(array $keys): self |
||
163 | { |
||
164 | $this->admin->reorderShowGroup($this->getCurrentGroupName(), $keys); |
||
165 | |||
166 | return $this; |
||
167 | } |
||
168 | |||
169 | protected function getGroups(): array |
||
170 | { |
||
171 | return $this->admin->getShowGroups(); |
||
172 | } |
||
173 | |||
174 | protected function setGroups(array $groups): void |
||
178 | |||
179 | protected function getTabs(): array |
||
180 | { |
||
181 | return $this->admin->getShowTabs(); |
||
182 | } |
||
183 | |||
184 | protected function setTabs(array $tabs): void |
||
185 | { |
||
186 | $this->admin->setShowTabs($tabs); |
||
187 | } |
||
188 | |||
189 | protected function getName(): string |
||
193 | } |
||
194 |
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.