Passed
Push — master ( ebc049...219f0e )
by
unknown
08:33
created

FormGroupTrait::setTabCssClasses()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 12
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 6
nc 4
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
12
/**
13
 * Provides an implementation of {@see \Charcoal\Ui\FormGroup\FormGroupInterface}.
14
 */
15
trait FormGroupTrait
16
{
17
    /**
18
     * Store a reference to the parent form widget.
19
     *
20
     * @var FormInterface
21
     */
22
    protected $form;
23
24
    /**
25
     * The group's collection of fields.
26
     *
27
     * @var FormInputInterface[]
28
     */
29
    private $inputs;
30
31
    /**
32
     * The input callback; called on every input.
33
     *
34
     * Callable signature: `function(FormInputInterface $input)`
35
     *
36
     * @var callable
37
     */
38
    private $inputCallback;
39
40
    /**
41
     * Store the builder instance for the current class.
42
     *
43
     * @var FormInputBuilder
44
     */
45
    protected $formInputBuilder;
46
47
    /**
48
     * The L10N display mode.
49
     *
50
     * @var string
51
     */
52
    private $l10nMode;
53
54
    /**
55
     * The group's identifier.
56
     *
57
     * @var string
58
     */
59
    private $ident;
60
61
    /**
62
     * The group's priority.
63
     *
64
     * @var integer
65
     */
66
    private $priority;
67
68
    /**
69
     * The required Acl permissions fetch from form group.
70
     *
71
     * @var string[] $requiredAclPermissions
72
     */
73
    private $requiredAclPermissions = [];
74
75
    /**
76
     * Class or Classes for tab form group.
77
     *
78
     * @var string|string[]
79
     */
80
    private $tabCssClasses;
81
82
    /**
83
     * @param FormInputBuilder $builder The builder, to create customized form input objects.
84
     * @return FormGroupInterface Chainable
85
     */
86
    protected function setFormInputBuilder(FormInputBuilder $builder)
87
    {
88
        $this->formInputBuilder = $builder;
89
90
        return $this;
91
    }
92
93
    /**
94
     * @param callable $cb The input callback.
95
     * @return FormGroupInterface Chainable
96
     */
97
    public function setInputCallback(callable $cb)
98
    {
99
        $this->inputCallback = $cb;
100
101
        return $this;
102
    }
103
104
    /**
105
     * @param FormInterface $form The parent form object.
106
     * @return FormGroupInterface Chainable
107
     */
108
    public function setForm(FormInterface $form)
109
    {
110
        $this->form = $form;
111
112
        return $this;
113
    }
114
115
    /**
116
     * @return FormInterface
117
     */
118
    public function form()
119
    {
120
        return $this->form;
121
    }
122
123
    /**
124
     * @param string $mode The l10n mode.
125
     * @return FormGroupInterface Chainable
126
     */
127
    public function setL10nMode($mode)
128
    {
129
        $this->l10nMode = $mode;
130
131
        return $this;
132
    }
133
134
    /**
135
     * @return string
136
     */
137
    public function l10nMode()
138
    {
139
        return $this->l10nMode;
140
    }
141
142
    /**
143
     * @param array $inputs The group inputs.
144
     * @return FormGroupInterface Chainable
145
     */
146
    public function setInputs(array $inputs)
147
    {
148
        $this->inputs = [];
149
        foreach ($inputs as $inputIdent => $input) {
150
            $this->addInput($inputIdent, $input);
151
        }
152
153
        return $this;
154
    }
155
156
    /**
157
     * @param string                   $inputIdent The input identifier.
158
     * @param array|FormInputInterface $input      The input object or structure.
159
     * @throws InvalidArgumentException If the ident argument is not a string or if the input is not valid.
160
     * @return FormGroupInterface Chainable
161
     */
162
    public function addInput($inputIdent, $input)
163
    {
164
        if (!is_string($inputIdent)) {
165
            throw new InvalidArgumentException(
166
                'Group ident must be a string'
167
            );
168
        }
169
170
        if (($input instanceof FormInputInterface)) {
171
            $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...
172
            $input->setFormGroup($this);
173
            $this->inputs[$inputIdent] = $input;
174
        } elseif (is_array($input)) {
175
            $g                         = $this->formInputBuilder->build($input);
176
            $this->inputs[$inputIdent] = $g;
177
        } else {
178
            throw new InvalidArgumentException(
179
                'Group must be a Form Group object or an array of form group options'
180
            );
181
        }
182
183
        return $this;
184
    }
185
186
    /**
187
     * Form Input generator.
188
     *
189
     * @param callable $inputCallback Optional. Input callback.
190
     * @return FormGroupInterface[]|Generator
191
     */
192
    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...
193
    {
194
        $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...
195
        uasort($groups, ['self', 'sortInputsByPriority']);
196
197
        $inputCallback = isset($inputCallback) ? $inputCallback : $this->inputCallback;
198
        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...
199
            if (!$input->l10nMode()) {
200
                $input->setL10nMode($this->l10nMode());
201
            }
202
            if ($inputCallback) {
203
                $inputCallback($input);
204
            }
205
            $GLOBALS['widget_template'] = $input->template();
206
            yield $input->ident() => $input;
207
        }
208
    }
209
210
    /**
211
     * Wether this group contains any inputs.
212
     *
213
     * @return boolean
214
     */
215
    public function hasInputs()
216
    {
217
        return (count($this->inputs) > 0);
218
    }
219
220
    /**
221
     * Get the number of inputs in this group.
222
     *
223
     * @return integer
224
     */
225
    public function numInputs()
226
    {
227
        return count($this->inputs);
228
    }
229
230
    /**
231
     * Set the identifier of the group.
232
     *
233
     * @param string $ident The group identifier.
234
     * @return self
235
     */
236
    public function setIdent($ident)
237
    {
238
        $this->ident = $ident;
239
240
        return $this;
241
    }
242
243
    /**
244
     * Retrieve the idenfitier of the group.
245
     *
246
     * @return string
247
     */
248
    public function ident()
249
    {
250
        return $this->ident;
251
    }
252
253
    /**
254
     * Set the group's priority or sorting index.
255
     *
256
     * @param  integer $priority An index, for sorting.
257
     * @throws InvalidArgumentException If the priority is not an integer.
258
     * @return self
259
     */
260 View Code Duplication
    public function setPriority($priority)
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...
261
    {
262
        if (!is_numeric($priority)) {
263
            throw new InvalidArgumentException(
264
                'Priority must be an integer'
265
            );
266
        }
267
268
        $this->priority = intval($priority);
269
270
        return $this;
271
    }
272
273
    /**
274
     * Retrieve the group's priority or sorting index.
275
     *
276
     * @return integer
277
     */
278
    public function priority()
279
    {
280
        return $this->priority;
281
    }
282
283
    /**
284
     * @param string|\string[] $classes Class or Classes for tab form group.
285
     * @return self
286
     */
287
    public function setTabCssClasses($classes)
288
    {
289
        if (is_string($classes)) {
290
            $this->tabCssClasses = $classes;
291
        }
292
293
        if (is_array($classes)) {
294
            $this->tabCssClasses = implode(' ', $classes);
295
        }
296
297
        return $this;
298
    }
299
300
    /**
301
     * @return string|\string[]
302
     */
303
    public function tabCssClasses()
304
    {
305
        return $this->tabCssClasses;
306
    }
307
}
308