Completed
Pull Request — master (#2)
by
unknown
14:01
created

FormGroupTrait::conditionalLogicUnmet()   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\Ui\FormGroup;
4
5
use InvalidArgumentException;
6
7
// From 'charcoal-ui'
8
use Charcoal\Ui\Form\FormInterface;
9
use Charcoal\Ui\FormInput\FormInputBuilder;
10
use Charcoal\Ui\FormInput\FormInputInterface;
11
use Charcoal\Ui\PrioritizableInterface;
12
13
/**
14
 * Provides an implementation of {@see \Charcoal\Ui\FormGroup\FormGroupInterface}.
15
 */
16
trait FormGroupTrait
17
{
18
    /**
19
     * Store a reference to the parent form widget.
20
     *
21
     * @var FormInterface
22
     */
23
    protected $form;
24
25
    /**
26
     * The group's collection of fields.
27
     *
28
     * @var FormInputInterface[]
29
     */
30
    private $inputs = [];
31
32
    /**
33
     * The input callback; called on every input.
34
     *
35
     * Callable signature: `function(FormInputInterface $input)`
36
     *
37
     * @var callable
38
     */
39
    private $inputCallback;
40
41
    /**
42
     * Store the builder instance for the current class.
43
     *
44
     * @var FormInputBuilder
45
     */
46
    protected $formInputBuilder;
47
48
    /**
49
     * The L10N display mode.
50
     *
51
     * @var string
52
     */
53
    private $l10nMode;
54
55
    /**
56
     * The group's identifier.
57
     *
58
     * @var string
59
     */
60
    private $ident;
61
62
    /**
63
     * The required Acl permissions fetch from form group.
64
     *
65
     * @var string[] $requiredAclPermissions
66
     */
67
    private $requiredAclPermissions = [];
68
69
    /**
70
     * One or many CSS classes for tab form group.
71
     *
72
     * @var string
73
     */
74
    private $tabCssClasses;
75
76
    /**
77
     * @var boolean
78
     */
79
    private $isHidden;
80
81
    /**
82
     * @var array|null
83
     */
84
    private $conditionalLogic;
85
86
    /**
87
     * @var array|null
88
     */
89
    private $rawConditionalLogic;
90
91
    /**
92
     * @var boolean
93
     */
94
    private $conditionalLogicUnmet;
95
96
    /**
97
     * Comparison function used by {@see uasort()}.
98
     *
99
     * @param  PrioritizableInterface $a Sortable entity A.
100
     * @param  PrioritizableInterface $b Sortable entity B.
101
     * @return integer Sorting value: -1 or 1.
102
     */
103
    abstract protected function sortItemsByPriority(
104
        PrioritizableInterface $a,
105
        PrioritizableInterface $b
106
    );
107
108
    /**
109
     * @param FormInputBuilder $builder The builder, to create customized form input objects.
110
     * @return FormGroupInterface Chainable
111
     */
112
    protected function setFormInputBuilder(FormInputBuilder $builder)
113
    {
114
        $this->formInputBuilder = $builder;
115
116
        return $this;
117
    }
118
119
    /**
120
     * @param callable $cb The input callback.
121
     * @return FormGroupInterface Chainable
122
     */
123
    public function setInputCallback(callable $cb)
124
    {
125
        $this->inputCallback = $cb;
126
127
        return $this;
128
    }
129
130
    /**
131
     * @param FormInterface $form The parent form object.
132
     * @return FormGroupInterface Chainable
133
     */
134
    public function setForm(FormInterface $form)
135
    {
136
        $this->form = $form;
137
138
        return $this;
139
    }
140
141
    /**
142
     * @return FormInterface
143
     */
144
    public function form()
145
    {
146
        return $this->form;
147
    }
148
149
    /**
150
     * @param string $mode The l10n mode.
151
     * @return FormGroupInterface Chainable
152
     */
153
    public function setL10nMode($mode)
154
    {
155
        $this->l10nMode = $mode;
156
157
        return $this;
158
    }
159
160
    /**
161
     * @return string
162
     */
163
    public function l10nMode()
164
    {
165
        return $this->l10nMode;
166
    }
167
168
    /**
169
     * @param array $inputs The group inputs.
170
     * @return FormGroupInterface Chainable
171
     */
172
    public function setInputs(array $inputs)
173
    {
174
        $this->inputs = [];
175
        foreach ($inputs as $inputIdent => $input) {
176
            $this->addInput($inputIdent, $input);
177
        }
178
179
        return $this;
180
    }
181
182
    /**
183
     * @param string                   $inputIdent The input identifier.
184
     * @param array|FormInputInterface $input      The input object or structure.
185
     * @throws InvalidArgumentException If the ident argument is not a string or if the input is not valid.
186
     * @return FormGroupInterface Chainable
187
     */
188
    public function addInput($inputIdent, $input)
189
    {
190
        if (!is_string($inputIdent)) {
191
            throw new InvalidArgumentException(
192
                'Group ident must be a string'
193
            );
194
        }
195
196
        if (($input instanceof FormInputInterface)) {
197
            $input->setForm($this->form);
0 ignored issues
show
Bug introduced by
The method setForm() does not exist on Charcoal\Ui\FormInput\FormInputInterface. Did you maybe mean setFormGroup()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
198
            $input->setFormGroup($this);
199
            $this->inputs[$inputIdent] = $input;
200
        } elseif (is_array($input)) {
201
            $g                         = $this->formInputBuilder->build($input);
202
            $this->inputs[$inputIdent] = $g;
203
        } else {
204
            throw new InvalidArgumentException(
205
                'Group must be a Form Group object or an array of form group options'
206
            );
207
        }
208
209
        return $this;
210
    }
211
212
    /**
213
     * Form Input generator.
214
     *
215
     * @param callable $inputCallback Optional. Input callback.
216
     * @return FormGroupInterface[]|Generator
217
     */
218
    public function inputs(callable $inputCallback = null)
219
    {
220
        $groups = $this->groups;
0 ignored issues
show
Bug introduced by
The property groups does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
221
        uasort($groups, [ $this, 'sortItemsByPriority' ]);
222
223
        $inputCallback = isset($inputCallback) ? $inputCallback : $this->inputCallback;
224
        foreach ($inputs as $input) {
0 ignored issues
show
Bug introduced by
The variable $inputs does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
225
            if (!$input->l10nMode()) {
226
                $input->setL10nMode($this->l10nMode());
227
            }
228
            if ($inputCallback) {
229
                $inputCallback($input);
230
            }
231
            $GLOBALS['widget_template'] = $input->template();
232
            yield $input->ident() => $input;
233
            $GLOBALS['widget_template'] = '';
234
        }
235
    }
236
237
    /**
238
     * Wether this group contains any inputs.
239
     *
240
     * @return boolean
241
     */
242
    public function hasInputs()
243
    {
244
        return (count($this->inputs) > 0);
245
    }
246
247
    /**
248
     * Get the number of inputs in this group.
249
     *
250
     * @return integer
251
     */
252
    public function numInputs()
253
    {
254
        return count($this->inputs);
255
    }
256
257
    /**
258
     * Set the identifier of the group.
259
     *
260
     * @param string $ident The group identifier.
261
     * @return self
262
     */
263
    public function setIdent($ident)
264
    {
265
        $this->ident = $ident;
266
267
        return $this;
268
    }
269
270
    /**
271
     * Retrieve the idenfitier of the group.
272
     *
273
     * @return string
274
     */
275
    public function ident()
276
    {
277
        return $this->ident;
278
    }
279
280
    /**
281
     * @param string|\string[] $classes Class or Classes for tab form group.
282
     * @return self
283
     */
284
    public function setTabCssClasses($classes)
285
    {
286
        if (is_string($classes)) {
287
            $this->tabCssClasses = $classes;
288
        }
289
290
        if (is_array($classes)) {
291
            $this->tabCssClasses = implode(' ', $classes);
292
        }
293
294
        return $this;
295
    }
296
297
    /**
298
     * @return string
299
     */
300
    public function tabCssClasses()
301
    {
302
        return $this->tabCssClasses;
303
    }
304
305
    /**
306
     * @return boolean
307
     */
308
    public function isHidden()
309
    {
310
        return $this->isHidden;
311
    }
312
313
    /**
314
     * @param  boolean $isHidden Hidden (TRUE) or shown (FALSE).
315
     * @return self
316
     */
317
    public function setIsHidden($isHidden)
318
    {
319
        $this->isHidden = $isHidden;
320
321
        return $this;
322
    }
323
324
    /**
325
     * @return array|null
326
     */
327
    public function conditionalLogic()
328
    {
329
        return $this->conditionalLogic;
330
    }
331
332
    /**
333
     * @return array|null
334
     */
335
    public function rawConditionalLogic()
336
    {
337
        return $this->rawConditionalLogic;
338
    }
339
340
    /**
341
     * @param array|null $conditionalLogic ConditionalLogic for FormGroupWidget.
342
     * @return self
343
     */
344
    public function setConditionalLogic($conditionalLogic)
345
    {
346
        $this->rawConditionalLogic = $conditionalLogic;
347
348
        if ($conditionalLogic && is_array($conditionalLogic)) {
349
            foreach ($conditionalLogic as &$condition) {
350
                $prop = $this->form()->formProperty($condition['property']);
0 ignored issues
show
Bug introduced by
The method formProperty() does not seem to exist on object<Charcoal\Ui\Form\FormInterface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
351
                $condition['input_id'] = $prop->input()->inputId();
352
            }
353
        }
354
355
        $this->conditionalLogic = [
356
            $this->widgetId() => $conditionalLogic
0 ignored issues
show
Bug introduced by
It seems like widgetId() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
357
        ];
358
359
        return $this;
360
    }
361
362
    /**
363
     * @return boolean
364
     */
365
    public function conditionalLogicUnmet()
366
    {
367
        return $this->conditionalLogicUnmet;
368
    }
369
370
    /**
371
     * @param boolean $conditionalLogicUnmet ConditionalLogicUnmet for FormGroupTrait.
372
     * @return self
373
     */
374
    public function setConditionalLogicUnmet($conditionalLogicUnmet)
375
    {
376
        $this->conditionalLogicUnmet = $conditionalLogicUnmet;
377
378
        return $this;
379
    }
380
}
381