Completed
Push — master ( 6a996d...b41795 )
by Mathieu
13:00 queued 10:14
created

FormGroupTrait::setPriority()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 6

Duplication

Lines 12
Ratio 100 %

Importance

Changes 0
Metric Value
dl 12
loc 12
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 6
nc 2
nop 1

1 Method

Rating   Name   Duplication   Size   Complexity  
A FormGroupTrait::tabCssClasses() 0 4 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 required Acl permissions fetch from form group.
63
     *
64
     * @var string[] $requiredAclPermissions
65
     */
66
    private $requiredAclPermissions = [];
67
68
    /**
69
     * Class or Classes for tab form group.
70
     *
71
     * @var string|string[]
72
     */
73
    private $tabCssClasses;
74
75
    /**
76
     * @param FormInputBuilder $builder The builder, to create customized form input objects.
77
     * @return FormGroupInterface Chainable
78
     */
79
    protected function setFormInputBuilder(FormInputBuilder $builder)
80
    {
81
        $this->formInputBuilder = $builder;
82
83
        return $this;
84
    }
85
86
    /**
87
     * @param callable $cb The input callback.
88
     * @return FormGroupInterface Chainable
89
     */
90
    public function setInputCallback(callable $cb)
91
    {
92
        $this->inputCallback = $cb;
93
94
        return $this;
95
    }
96
97
    /**
98
     * @param FormInterface $form The parent form object.
99
     * @return FormGroupInterface Chainable
100
     */
101
    public function setForm(FormInterface $form)
102
    {
103
        $this->form = $form;
104
105
        return $this;
106
    }
107
108
    /**
109
     * @return FormInterface
110
     */
111
    public function form()
112
    {
113
        return $this->form;
114
    }
115
116
    /**
117
     * @param string $mode The l10n mode.
118
     * @return FormGroupInterface Chainable
119
     */
120
    public function setL10nMode($mode)
121
    {
122
        $this->l10nMode = $mode;
123
124
        return $this;
125
    }
126
127
    /**
128
     * @return string
129
     */
130
    public function l10nMode()
131
    {
132
        return $this->l10nMode;
133
    }
134
135
    /**
136
     * @param array $inputs The group inputs.
137
     * @return FormGroupInterface Chainable
138
     */
139
    public function setInputs(array $inputs)
140
    {
141
        $this->inputs = [];
142
        foreach ($inputs as $inputIdent => $input) {
143
            $this->addInput($inputIdent, $input);
144
        }
145
146
        return $this;
147
    }
148
149
    /**
150
     * @param string                   $inputIdent The input identifier.
151
     * @param array|FormInputInterface $input      The input object or structure.
152
     * @throws InvalidArgumentException If the ident argument is not a string or if the input is not valid.
153
     * @return FormGroupInterface Chainable
154
     */
155
    public function addInput($inputIdent, $input)
156
    {
157
        if (!is_string($inputIdent)) {
158
            throw new InvalidArgumentException(
159
                'Group ident must be a string'
160
            );
161
        }
162
163
        if (($input instanceof FormInputInterface)) {
164
            $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...
165
            $input->setFormGroup($this);
166
            $this->inputs[$inputIdent] = $input;
167
        } elseif (is_array($input)) {
168
            $g                         = $this->formInputBuilder->build($input);
169
            $this->inputs[$inputIdent] = $g;
170
        } else {
171
            throw new InvalidArgumentException(
172
                'Group must be a Form Group object or an array of form group options'
173
            );
174
        }
175
176
        return $this;
177
    }
178
179
    /**
180
     * Form Input generator.
181
     *
182
     * @param callable $inputCallback Optional. Input callback.
183
     * @return FormGroupInterface[]|Generator
184
     */
185
    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...
186
    {
187
        $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...
188
        uasort($groups, ['self', 'sortItemsByPriority']);
189
190
        $inputCallback = isset($inputCallback) ? $inputCallback : $this->inputCallback;
191
        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...
192
            if (!$input->l10nMode()) {
193
                $input->setL10nMode($this->l10nMode());
194
            }
195
            if ($inputCallback) {
196
                $inputCallback($input);
197
            }
198
            $GLOBALS['widget_template'] = $input->template();
199
            yield $input->ident() => $input;
200
            $GLOBALS['widget_template'] = '';
201
        }
202
    }
203
204
    /**
205
     * Wether this group contains any inputs.
206
     *
207
     * @return boolean
208
     */
209
    public function hasInputs()
210
    {
211
        return (count($this->inputs) > 0);
212
    }
213
214
    /**
215
     * Get the number of inputs in this group.
216
     *
217
     * @return integer
218
     */
219
    public function numInputs()
220
    {
221
        return count($this->inputs);
222
    }
223
224
    /**
225
     * Set the identifier of the group.
226
     *
227
     * @param string $ident The group identifier.
228
     * @return self
229
     */
230
    public function setIdent($ident)
231
    {
232
        $this->ident = $ident;
233
234
        return $this;
235
    }
236
237
    /**
238
     * Retrieve the idenfitier of the group.
239
     *
240
     * @return string
241
     */
242
    public function ident()
243
    {
244
        return $this->ident;
245
    }
246
247
    /**
248
     * @param string|\string[] $classes Class or Classes for tab form group.
249
     * @return self
250
     */
251
    public function setTabCssClasses($classes)
252
    {
253
        if (is_string($classes)) {
254
            $this->tabCssClasses = $classes;
255
        }
256
257
        if (is_array($classes)) {
258
            $this->tabCssClasses = implode(' ', $classes);
259
        }
260
261
        return $this;
262
    }
263
264
    /**
265
     * @return string|\string[]
266
     */
267
    public function tabCssClasses()
268
    {
269
        return $this->tabCssClasses;
270
    }
271
}
272