Completed
Push — master ( 5f79ee...8603e9 )
by Mathieu
08:29
created

FormGroupTrait::active()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
dl 0
loc 4
rs 10
c 1
b 0
f 1
cc 1
eloc 2
nc 1
nop 0
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
 *
14
 */
15
trait FormGroupTrait
16
{
17
    /**
18
     * @var FormInputInterface[] $inputs
19
     */
20
    private $inputs;
21
22
    /**
23
     * In-memory copy of the parent form widget.
24
     * @var FormInterface $form
25
     */
26
    protected $form;
27
28
    /**
29
     * @var FormInputBuilder $formInputBuilder
30
     */
31
    protected $formInputBuilder;
32
33
    /**
34
     * The input callback; called on every input.
35
     * Callable signature: `function(FormInputInterface $input)`
36
     *
37
     * @var callable $itemCallback
38
     */
39
    private $inputCallback = null;
40
41
    /**
42
     * @var boolean $active
43
     */
44
    private $active = true;
45
46
    /**
47
     * @var integer $priority
48
     */
49
    private $priority;
50
51
    /**
52
     * @var string $l10nMode
53
     */
54
    private $l10nMode;
55
56
    /**
57
     * @param FormInputBuilder $builder The builder, to create customized form input objects.
58
     * @return FormGroupInterface Chainable
59
     */
60
    protected function setFormInputBuilder(FormInputBuilder $builder)
61
    {
62
        $this->formInputBuilder = $builder;
63
        return $this;
64
    }
65
66
    /**
67
     * @param callable $cb The input callback.
68
     * @return FormGroupInterface Chainable
69
     */
70
    public function setInputCallback(callable $cb)
71
    {
72
        $this->inputCallback = $cb;
73
        return $this;
74
    }
75
76
    /**
77
     * @param FormInterface $form The parent form object.
78
     * @return FormGroupInterface Chainable
79
     */
80
    public function setForm(FormInterface $form)
81
    {
82
        $this->form = $form;
83
        return $this;
84
    }
85
86
    /**
87
     * Allow the form to public.
88
     * @return FormInterface
89
     */
90
    public function form()
91
    {
92
        return $this->form;
93
    }
94
95
    /**
96
     * @param boolean $active The active flag.
97
     * @return FormPropertyWidget Chainable
98
     */
99
    public function setActive($active)
100
    {
101
        $this->active = !!$active;
102
        return $this;
103
    }
104
105
    /**
106
     * @return boolean
107
     */
108
    public function active()
109
    {
110
        return $this->active;
111
    }
112
113
    /**
114
     * @param integer $priority The priority, for ordering purpose.
115
     * @throws InvalidArgumentException If the priority argument is not a number.
116
     * @return FormGroupInterface Chainable
117
     */
118
    public function setPriority($priority)
119
    {
120
        if (!is_numeric($priority)) {
121
            throw new InvalidArgumentException(
122
                'Priority must be an integer'
123
            );
124
        }
125
        $priority = (int)$priority;
126
        $this->priority = $priority;
127
        return $this;
128
    }
129
130
    /**
131
     * @return integer
132
     */
133
    public function priority()
134
    {
135
        return $this->priority;
136
    }
137
138
    /**
139
     * @param string $mode The l10n mode.
140
     * @return FormGroupInterface Chainable
141
     */
142
    public function setL10nMode($mode)
143
    {
144
        $this->l10nMode = $mode;
145
        return $this;
146
    }
147
148
    /**
149
     * @return string
150
     */
151
    public function l10nMode()
152
    {
153
        return $this->l10nMode;
154
    }
155
156
    /**
157
     * @param array $inputs The group inputs.
158
     * @return FormGroupInterface Chainable
159
     */
160
    public function setInputs(array $inputs)
161
    {
162
        $this->inputs = [];
163
        foreach ($inputs as $inputIdent => $input) {
164
            $this->addInput($inputIdent, $input);
165
        }
166
        return $this;
167
    }
168
169
    /**
170
     * @param string                   $inputIdent The input identifier.
171
     * @param array|FormInputInterface $input      The input object or structure.
172
     * @throws InvalidArgumentException If the ident argument is not a string or if the input is not valid.
173
     * @return FormInterface Chainable
174
     */
175
    public function addInput($inputIdent, $input)
176
    {
177
        if (!is_string($inputIdent)) {
178
            throw new InvalidArgumentException(
179
                'Group ident must be a string'
180
            );
181
        }
182
183
        if (($input instanceof FormInputInterface)) {
184
            $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...
185
            $input->setFormGroup($this);
186
            $this->inputs[$inputIdent] = $input;
187
        } elseif (is_array($input)) {
188
            $g = $this->formInputBuilder->build($input);
189
            $this->inputs[$inputIdent] = $g;
190
        } else {
191
            throw new InvalidArgumentException(
192
                'Group must be a Form Group object or an array of form group options'
193
            );
194
        }
195
196
        return $this;
197
    }
198
199
    /**
200
     * Form Input generator.
201
     *
202
     * @param callable $inputCallback Optional. Input callback.
203
     * @return FormGroupInterface[]
204
     */
205 View Code Duplication
    public function inputs(callable $inputCallback = null)
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...
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...
206
    {
207
        $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...
208
        uasort($groups, ['self', 'sortInputsByPriority']);
209
210
        $inputCallback = isset($inputCallback) ? $inputCallback : $this->inputCallback;
211
        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...
212
            if (!$input->l10nMode()) {
213
                $input->setL10nMode($this->l10nMode());
214
            }
215
            if ($inputCallback) {
216
                $inputCallback($input);
217
            }
218
            $GLOBALS['widget_template'] = $input->template();
219
            yield $input->ident() => $input;
220
        }
221
222
    }
223
224
    /**
225
     * Wether this group contains any inputs.
226
     *
227
     * @return boolean
228
     */
229
    public function hasInputs()
230
    {
231
        return (count($this->inputs) > 0);
232
    }
233
234
    /**
235
     * Get the number of inputs in this group.
236
     *
237
     * @return integer
238
     */
239
    public function numInputs()
240
    {
241
        return count($this->inputs);
242
    }
243
}
244