Completed
Push — master ( e519e0...173396 )
by
unknown
25:55
created

MultiGroupWidget::storageProperty()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 11
rs 9.9
c 0
b 0
f 0
cc 2
nc 2
nop 0
1
<?php
2
3
namespace Charcoal\Admin\Widget;
4
5
use RuntimeException;
6
7
// from 'charcoal-admin'
8
use Charcoal\Admin\Ui\ObjectContainerInterface;
9
use Charcoal\Admin\Ui\ObjectContainerTrait;
10
use Charcoal\Admin\AdminWidget;
11
12
// form 'charcoal-factory'
13
use Charcoal\Factory\FactoryInterface;
14
15
// from 'charcoal-property'
16
use Charcoal\Property\ModelStructureProperty;
17
use Charcoal\Property\PropertyInterface;
18
use Charcoal\Property\StructureProperty;
19
20
// From Pimple
21
use Pimple\Container;
22
23
// From 'charcoal-ui'
24
use Charcoal\Ui\Form\FormInterface;
25
use Charcoal\Ui\Form\FormTrait;
26
use Charcoal\Ui\Layout\LayoutAwareInterface;
27
use Charcoal\Ui\Layout\LayoutAwareTrait;
28
use Charcoal\Ui\PrioritizableInterface;
29
30
/**
31
 * Class TemplateAttachmentWidget
32
 */
33
class MultiGroupWidget extends AdminWidget implements
34
    FormInterface,
35
    LayoutAwareInterface,
36
    ObjectContainerInterface
37
{
38
    use FormTrait;
39
    use LayoutAwareTrait;
40
    use ObjectContainerTrait;
41
42
    /**
43
     * The cache of snake-cased words.
44
     *
45
     * @var array
46
     */
47
    protected static $snakeCache = [];
48
49
    /**
50
     * The cache of camel-cased words.
51
     *
52
     * @var array
53
     */
54
    protected static $camelCache = [];
55
56
    /**
57
     * @var string
58
     */
59
    protected $controllerIdent;
60
61
    /**
62
     * @var FactoryInterface
63
     */
64
    protected $widgetFactory;
65
66
    /**
67
     * @var boolean
68
     */
69
    protected $isMergingData;
70
71
    /**
72
     * @var array|mixed
73
     */
74
    protected $formGroups;
75
76
    /**
77
     * @var array|mixed
78
     */
79
    protected $widgetMetadata;
80
81
    /**
82
     * @var array|mixed
83
     */
84
    protected $extraFormGroups;
85
86
    /**
87
     * The form group's storage target.
88
     *
89
     * @var ModelStructureProperty|null
90
     */
91
    protected $storageProperty;
92
93
    /**
94
     * Comparison function used by {@see uasort()}.
95
     *
96
     * @param  PrioritizableInterface $a Sortable entity A.
97
     * @param  PrioritizableInterface $b Sortable entity B.
98
     * @return integer Sorting value: -1 or 1.
99
     */
100 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...
101
        PrioritizableInterface $a,
102
        PrioritizableInterface $b
103
    ) {
104
        $priorityA = $a->priority();
105
        $priorityB = $b->priority();
106
107
        if ($priorityA === $priorityB) {
108
            return 0;
109
        }
110
111
        return ($priorityA < $priorityB) ? (-1) : 1;
112
    }
113
114
    /**
115
     * @param array $data The widget data.
116
     * @return self
117
     */
118
    public function setData(array $data)
119
    {
120
        parent::setData($data);
121
122
        $this->mergeDataSources($data);
123
124
        return $this;
125
    }
126
127
    /**
128
     * @param  Container $container The DI container.
129
     * @return void
130
     */
131
    protected function setDependencies(Container $container)
132
    {
133
        parent::setDependencies($container);
134
135
        $this->setWidgetFactory($container['widget/factory']);
136
137
        // Satisfies FormInterface
138
        $this->setFormGroupFactory($container['form/group/factory']);
139
    }
140
141
    /**
142
     * @return string
143
     */
144
    public function defaultGroupType()
145
    {
146
        return 'charcoal/admin/widget/form-group/generic';
147
    }
148
149
    /**
150
     * Retrieve the default data sources (when setting data on an entity).
151
     *
152
     * @return string[]
153
     */
154
    protected function defaultDataSources()
155
    {
156
        return [self::DATA_SOURCE_OBJECT];
157
    }
158
159
    /**
160
     * Set an widget factory.
161
     *
162
     * @param FactoryInterface $factory The factory to create widgets.
163
     * @return self
164
     */
165
    protected function setWidgetFactory(FactoryInterface $factory)
166
    {
167
        $this->widgetFactory = $factory;
168
169
        return $this;
170
    }
171
172
    /**
173
     * Retrieve the widget factory.
174
     *
175
     * @throws RuntimeException If the widget factory was not previously set.
176
     * @return FactoryInterface
177
     */
178
    public function widgetFactory()
179
    {
180
        if (!isset($this->widgetFactory)) {
181
            throw new RuntimeException(sprintf(
182
                'Widget Factory is not defined for "%s"',
183
                get_class($this)
184
            ));
185
        }
186
187
        return $this->widgetFactory;
188
    }
189
190
    /**
191
     * @return mixed
192
     */
193
    public function formGroups()
194
    {
195
        return $this->formGroups;
196
    }
197
198
    /**
199
     * @param mixed $formGroups FormGroups for MultiGroupWidget.
200
     * @return self
201
     */
202
    public function setFormGroups($formGroups)
203
    {
204
        $this->formGroups = $formGroups;
205
206
        return $this;
207
    }
208
209
    /**
210
     * @return array|mixed
211
     */
212
    public function widgetMetadata()
213
    {
214
        return $this->widgetMetadata;
215
    }
216
217
    /**
218
     * @param array|mixed $widgetMetadata WidgetMetadata for MultiGroupWidget.
219
     * @return self
220
     */
221
    public function setWidgetMetadata($widgetMetadata)
222
    {
223
        if (is_string($widgetMetadata) && $this->view()) {
224
            $widgetMetadata = $this->view()->renderTemplate($widgetMetadata, $this->obj());
225
226
            if ($widgetMetadata !== '') {
227
                $widgetMetadata = json_decode($widgetMetadata, true);
228
            } else {
229
                $widgetMetadata = null;
230
            }
231
        }
232
233
        $this->widgetMetadata = $widgetMetadata;
234
235
        return $this;
236
    }
237
238
    /**
239
     * Set the form group's storage target.
240
     *
241
     * Must be a property of the form's object model that will receive an associative array
242
     * of the form group's data.
243
     *
244
     * @param  string|ModelStructureProperty $propertyIdent The property identifier—or instance—of a storage property.
245
     * @throws \InvalidArgumentException If the property identifier is not a string.
246
     * @throws \UnexpectedValueException If a property is invalid.
247
     * @return self
248
     */
249
    public function setStorageProperty($propertyIdent)
250
    {
251
        $property = null;
252
        if ($propertyIdent instanceof PropertyInterface) {
253
            $property      = $propertyIdent;
254
            $propertyIdent = $property->ident();
255
        } elseif (!is_string($propertyIdent)) {
256
            throw new \InvalidArgumentException(
257
                'Storage Property identifier must be a string'
258
            );
259
        }
260
261
        $obj = $this->obj();
262
        if (!$obj->hasProperty($propertyIdent)) {
263
            throw new \UnexpectedValueException(sprintf(
264
                'The "%1$s" property is not defined on [%2$s]',
265
                $propertyIdent,
266
                get_class($obj)
267
            ));
268
        }
269
270
        if ($property === null) {
271
            $property = $obj->property($propertyIdent);
272
        }
273
274
        if ($property instanceof StructureProperty) {
275
            $this->storageProperty = $property;
0 ignored issues
show
Documentation Bug introduced by
It seems like $property of type object<Charcoal\Property\StructureProperty> is incompatible with the declared type object<Charcoal\Property...StructureProperty>|null of property $storageProperty.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
276
        } else {
277
            throw new \UnexpectedValueException(sprintf(
278
                '"%s" [%s] is not a model structure property on [%s].',
279
                $propertyIdent,
280
                (is_object($property) ? get_class($property) : gettype($property)),
281
                (is_object($obj) ? get_class($obj) : gettype($obj))
282
            ));
283
        }
284
285
        return $this;
286
    }
287
288
    /**
289
     * Retrieve the form group's storage property master.
290
     *
291
     * @throws RuntimeException If the storage property was not previously set.
292
     * @return ModelStructureProperty
293
     */
294
    public function storageProperty()
295
    {
296
        if ($this->storageProperty === null) {
297
            throw new RuntimeException(sprintf(
298
                'Storage property owner is not defined for "%s"',
299
                get_class($this)
300
            ));
301
        }
302
303
        return $this->storageProperty;
304
    }
305
}
306