Completed
Push — master ( f12ef6...78c2b1 )
by
unknown
04:45
created

MultiGroupWidget::form()   A

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
namespace Charcoal\Admin\Widget;
4
5
use Charcoal\Admin\Ui\ObjectContainerInterface;
6
use Charcoal\Admin\Ui\ObjectContainerTrait;
7
use Charcoal\Factory\FactoryInterface;
8
use RuntimeException;
9
10
use Charcoal\Admin\AdminWidget;
11
12
// From Pimple
13
use Pimple\Container;
14
15
// From 'charcoal-ui'
16
use Charcoal\Ui\Form\FormInterface;
17
use Charcoal\Ui\Form\FormTrait;
18
use Charcoal\Ui\Layout\LayoutAwareInterface;
19
use Charcoal\Ui\Layout\LayoutAwareTrait;
20
use Charcoal\Ui\PrioritizableInterface;
21
22
/**
23
 * Class TemplateAttachmentWidget
24
 */
25
class MultiGroupWidget extends AdminWidget implements
26
    FormInterface,
27
    LayoutAwareInterface,
28
    ObjectContainerInterface
29
{
30
    use FormTrait;
31
    use LayoutAwareTrait;
32
    use ObjectContainerTrait;
33
34
    /**
35
     * The cache of snake-cased words.
36
     *
37
     * @var array
38
     */
39
    protected static $snakeCache = [];
40
41
    /**
42
     * The cache of camel-cased words.
43
     *
44
     * @var array
45
     */
46
    protected static $camelCache = [];
47
48
    /**
49
     * @var string
50
     */
51
    protected $controllerIdent;
52
53
    /**
54
     * @var FactoryInterface
55
     */
56
    protected $widgetFactory;
57
58
    /**
59
     * @var boolean
60
     */
61
    protected $isMergingData;
62
63
    /**
64
     * @var array|mixed
65
     */
66
    protected $formGroups;
67
68
    /**
69
     * @var FormWidget
70
     */
71
    protected $form;
72
73
    /**
74
     * Comparison function used by {@see uasort()}.
75
     *
76
     * @param  PrioritizableInterface $a Sortable entity A.
77
     * @param  PrioritizableInterface $b Sortable entity B.
78
     * @return integer Sorting value: -1 or 1.
79
     */
80 View Code Duplication
    protected function sortItemsByPriority(
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
81
        PrioritizableInterface $a,
82
        PrioritizableInterface $b
83
    ) {
84
        $priorityA = $a->priority();
85
        $priorityB = $b->priority();
86
87
        if ($priorityA === $priorityB) {
88
            return 0;
89
        }
90
91
        return ($priorityA < $priorityB) ? (-1) : 1;
92
    }
93
94
    /**
95
     * @param  Container $container The DI container.
96
     * @return void
97
     */
98
    protected function setDependencies(Container $container)
99
    {
100
        parent::setDependencies($container);
101
102
        $this->setWidgetFactory($container['widget/factory']);
103
104
        // Satisfies FormInterface
105
        $this->setFormGroupFactory($container['form/group/factory']);
106
    }
107
108
    /**
109
     * @return string
110
     */
111
    public function defaultGroupType()
112
    {
113
        return 'charcoal/admin/widget/form-group/generic';
114
    }
115
116
    /**
117
     * Set the object's form groups.
118
     *
119
     * @param array $groups A collection of group structures.
120
     * @return FormInterface Chainable
121
     */
122
    public function setGroups(array $groups)
123
    {
124
        $this->groups       = [];
125
        $obj                = $this->obj();
126
        $objMetadata        = $obj->metadata();
127
        $adminMetadata      = (isset($objMetadata['admin']) ? $objMetadata['admin'] : null);
128
129
        if (isset($adminMetadata['form_groups'])) {
130
            $extraFormGroups = array_intersect(
131
                array_keys($groups),
132
                array_keys($adminMetadata['form_groups'])
133
            );
134
            foreach ($extraFormGroups as $groupIdent) {
135
                $groups[$groupIdent] = array_replace_recursive(
136
                    $adminMetadata['form_groups'][$groupIdent],
137
                    $groups[$groupIdent]
138
                );
139
140
                $this->addGroup($groupIdent, $groups[$groupIdent]);
141
            }
142
        }
143
144
        return $this;
145
    }
146
147
    /**
148
     * Set an widget factory.
149
     *
150
     * @param FactoryInterface $factory The factory to create widgets.
151
     * @return self
152
     */
153
    protected function setWidgetFactory(FactoryInterface $factory)
154
    {
155
        $this->widgetFactory = $factory;
156
157
        return $this;
158
    }
159
160
    /**
161
     * Retrieve the widget factory.
162
     *
163
     * @throws RuntimeException If the widget factory was not previously set.
164
     * @return FactoryInterface
165
     */
166
    public function widgetFactory()
167
    {
168
        if (!isset($this->widgetFactory)) {
169
            throw new RuntimeException(sprintf(
170
                'Widget Factory is not defined for "%s"',
171
                get_class($this)
172
            ));
173
        }
174
175
        return $this->widgetFactory;
176
    }
177
178
    /**
179
     * @return FormWidget
180
     */
181
    public function form()
182
    {
183
        return $this->form;
184
    }
185
186
    /**
187
     * @param FormInterface $form Form for MultiGroupWidget.
188
     * @return self
189
     */
190
    public function setForm(FormInterface $form)
191
    {
192
        $this->form = $form;
0 ignored issues
show
Documentation Bug introduced by
$form is of type object<Charcoal\Ui\Form\FormInterface>, but the property $form was declared to be of type object<Charcoal\Admin\Widget\FormWidget>. Are you sure that you always receive this specific sub-class here, or does it make sense to add an instanceof check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a given class or a super-class is assigned to a property that is type hinted more strictly.

Either this assignment is in error or an instanceof check should be added for that assignment.

class Alien {}

class Dalek extends Alien {}

class Plot
{
    /** @var  Dalek */
    public $villain;
}

$alien = new Alien();
$plot = new Plot();
if ($alien instanceof Dalek) {
    $plot->villain = $alien;
}
Loading history...
193
194
        return $this;
195
    }
196
197
    /**
198
     * @return mixed
199
     */
200
    public function formGroups()
201
    {
202
        return $this->formGroups;
203
    }
204
205
    /**
206
     * @param mixed $formGroups FormGroups for MultiGroupWidget.
207
     * @return self
208
     */
209
    public function setFormGroups($formGroups)
210
    {
211
        $this->formGroups = $formGroups;
212
213
        return $this;
214
    }
215
216
    /**
217
     * So that the formTrait can access the current From widget.
218
     *
219
     * @return FormWidget
220
     */
221
    protected function formWidget()
222
    {
223
        return $this->form();
224
    }
225
}
226