ShowMapper::getGroups()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
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\Show;
15
16
use Sonata\AdminBundle\Admin\AdminInterface;
17
use Sonata\AdminBundle\Admin\FieldDescriptionCollection;
18
use Sonata\AdminBundle\Admin\FieldDescriptionInterface;
19
use Sonata\AdminBundle\Builder\ShowBuilderInterface;
20
use Sonata\AdminBundle\Mapper\BaseGroupedMapper;
21
22
/**
23
 * This class is used 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 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.'
0 ignored issues
show
Unused Code introduced by
The call to TypeError::__construct() has too many arguments starting with 'Unknown field name in s...e interface or string.'.

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.

Loading history...
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
175
    {
176
        $this->admin->setShowGroups($groups);
177
    }
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
190
    {
191
        return 'show';
192
    }
193
}
194