Completed
Push — master ( f53814...1d1cb6 )
by
unknown
06:30
created

FormGroupTrait::inputs()   B

Complexity

Conditions 5
Paths 10

Size

Total Lines 18
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 18
rs 8.8571
c 0
b 0
f 0
cc 5
eloc 12
nc 10
nop 1
1
<?php
2
3
namespace Charcoal\Ui\FormGroup;
4
5
use InvalidArgumentException;
6
7
// Intra-module (`charcoal-ui`) dependencies
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
     * Class or Classes for tab form group.
71
     *
72
     * @var string|string[]
73
     */
74
    private $tabCssClasses;
75
76
    /**
77
     * @var boolean
78
     */
79
    private $isHidden;
80
81
    /**
82
     * Comparison function used by {@see uasort()}.
83
     *
84
     * @param  PrioritizableInterface $a Sortable entity A.
85
     * @param  PrioritizableInterface $b Sortable entity B.
86
     * @return integer Sorting value: -1 or 1.
87
     */
88
    abstract protected function sortItemsByPriority(
89
        PrioritizableInterface $a,
90
        PrioritizableInterface $b
91
    );
92
93
    /**
94
     * @param FormInputBuilder $builder The builder, to create customized form input objects.
95
     * @return FormGroupInterface Chainable
96
     */
97
    protected function setFormInputBuilder(FormInputBuilder $builder)
98
    {
99
        $this->formInputBuilder = $builder;
100
101
        return $this;
102
    }
103
104
    /**
105
     * @param callable $cb The input callback.
106
     * @return FormGroupInterface Chainable
107
     */
108
    public function setInputCallback(callable $cb)
109
    {
110
        $this->inputCallback = $cb;
111
112
        return $this;
113
    }
114
115
    /**
116
     * @param FormInterface $form The parent form object.
117
     * @return FormGroupInterface Chainable
118
     */
119
    public function setForm(FormInterface $form)
120
    {
121
        $this->form = $form;
122
123
        return $this;
124
    }
125
126
    /**
127
     * @return FormInterface
128
     */
129
    public function form()
130
    {
131
        return $this->form;
132
    }
133
134
    /**
135
     * @param string $mode The l10n mode.
136
     * @return FormGroupInterface Chainable
137
     */
138
    public function setL10nMode($mode)
139
    {
140
        $this->l10nMode = $mode;
141
142
        return $this;
143
    }
144
145
    /**
146
     * @return string
147
     */
148
    public function l10nMode()
149
    {
150
        return $this->l10nMode;
151
    }
152
153
    /**
154
     * @param array $inputs The group inputs.
155
     * @return FormGroupInterface Chainable
156
     */
157
    public function setInputs(array $inputs)
158
    {
159
        $this->inputs = [];
160
        foreach ($inputs as $inputIdent => $input) {
161
            $this->addInput($inputIdent, $input);
162
        }
163
164
        return $this;
165
    }
166
167
    /**
168
     * @param string                   $inputIdent The input identifier.
169
     * @param array|FormInputInterface $input      The input object or structure.
170
     * @throws InvalidArgumentException If the ident argument is not a string or if the input is not valid.
171
     * @return FormGroupInterface Chainable
172
     */
173
    public function addInput($inputIdent, $input)
174
    {
175
        if (!is_string($inputIdent)) {
176
            throw new InvalidArgumentException(
177
                'Group ident must be a string'
178
            );
179
        }
180
181
        if (($input instanceof FormInputInterface)) {
182
            $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...
183
            $input->setFormGroup($this);
184
            $this->inputs[$inputIdent] = $input;
185
        } elseif (is_array($input)) {
186
            $g                         = $this->formInputBuilder->build($input);
187
            $this->inputs[$inputIdent] = $g;
188
        } else {
189
            throw new InvalidArgumentException(
190
                'Group must be a Form Group object or an array of form group options'
191
            );
192
        }
193
194
        return $this;
195
    }
196
197
    /**
198
     * Form Input generator.
199
     *
200
     * @param callable $inputCallback Optional. Input callback.
201
     * @return FormGroupInterface[]|Generator
202
     */
203
    public function inputs(callable $inputCallback = null)
0 ignored issues
show
Coding Style introduced by
inputs uses the super-global variable $GLOBALS which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
204
    {
205
        $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...
206
        uasort($groups, [ $this, 'sortItemsByPriority' ]);
207
208
        $inputCallback = isset($inputCallback) ? $inputCallback : $this->inputCallback;
209
        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...
210
            if (!$input->l10nMode()) {
211
                $input->setL10nMode($this->l10nMode());
212
            }
213
            if ($inputCallback) {
214
                $inputCallback($input);
215
            }
216
            $GLOBALS['widget_template'] = $input->template();
217
            yield $input->ident() => $input;
218
            $GLOBALS['widget_template'] = '';
219
        }
220
    }
221
222
    /**
223
     * Wether this group contains any inputs.
224
     *
225
     * @return boolean
226
     */
227
    public function hasInputs()
228
    {
229
        return (count($this->inputs) > 0);
230
    }
231
232
    /**
233
     * Get the number of inputs in this group.
234
     *
235
     * @return integer
236
     */
237
    public function numInputs()
238
    {
239
        return count($this->inputs);
240
    }
241
242
    /**
243
     * Set the identifier of the group.
244
     *
245
     * @param string $ident The group identifier.
246
     * @return self
247
     */
248
    public function setIdent($ident)
249
    {
250
        $this->ident = $ident;
251
252
        return $this;
253
    }
254
255
    /**
256
     * Retrieve the idenfitier of the group.
257
     *
258
     * @return string
259
     */
260
    public function ident()
261
    {
262
        return $this->ident;
263
    }
264
265
    /**
266
     * @param string|\string[] $classes Class or Classes for tab form group.
267
     * @return self
268
     */
269
    public function setTabCssClasses($classes)
270
    {
271
        if (is_string($classes)) {
272
            $this->tabCssClasses = $classes;
273
        }
274
275
        if (is_array($classes)) {
276
            $this->tabCssClasses = implode(' ', $classes);
277
        }
278
279
        return $this;
280
    }
281
282
    /**
283
     * @return string|\string[]
284
     */
285
    public function tabCssClasses()
286
    {
287
        return $this->tabCssClasses;
288
    }
289
290
    /**
291
     * @return boolean
292
     */
293
    public function isHidden()
294
    {
295
        return $this->isHidden;
296
    }
297
298
    /**
299
     * @param boolean $isHidden
300
     * @return self
301
     */
302
    public function setIsHidden($isHidden)
303
    {
304
        $this->isHidden = $isHidden;
305
306
        return $this;
307
    }
308
}
309