Completed
Pull Request — master (#143)
by
unknown
01:20
created

Html::number()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 5
dl 0
loc 7
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Spatie\Html;
4
5
use DateTimeImmutable;
6
use Illuminate\Support\Str;
7
use Spatie\Html\Elements\A;
8
use Spatie\Html\Elements\I;
9
use Illuminate\Http\Request;
10
use Spatie\Html\Elements\Div;
11
use Spatie\Html\Elements\Img;
12
use Spatie\Html\Elements\File;
13
use Spatie\Html\Elements\Form;
14
use Spatie\Html\Elements\Span;
15
use Spatie\Html\Elements\Input;
16
use Spatie\Html\Elements\Label;
17
use Spatie\Html\Elements\Button;
18
use Spatie\Html\Elements\Legend;
19
use Spatie\Html\Elements\Option;
20
use Spatie\Html\Elements\Select;
21
use Spatie\Html\Elements\Element;
22
use Illuminate\Support\Collection;
23
use Illuminate\Support\HtmlString;
24
use Spatie\Html\Elements\Fieldset;
25
use Spatie\Html\Elements\Textarea;
26
use Illuminate\Support\Traits\Macroable;
27
use Illuminate\Contracts\Support\Htmlable;
28
29
class Html
30
{
31
    use Macroable;
32
33
    const HTML_DATE_FORMAT = 'Y-m-d';
34
    const HTML_TIME_FORMAT = 'H:i:s';
35
36
    /** @var \Illuminate\Http\Request */
37
    protected $request;
38
39
    /** @var \ArrayAccess|array */
40
    protected $model;
41
42
    public function __construct(Request $request)
0 ignored issues
show
Bug introduced by
You have injected the Request via parameter $request. This is generally not recommended as there might be multiple instances during a request cycle (f.e. when using sub-requests). Instead, it is recommended to inject the RequestStack and retrieve the current request each time you need it via getCurrentRequest().
Loading history...
43
    {
44
        $this->request = $request;
45
    }
46
47
    /**
48
     * @param string|null $href
49
     * @param string|null $text
0 ignored issues
show
Bug introduced by
There is no parameter named $text. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
50
     *
51
     * @return \Spatie\Html\Elements\A
52
     */
53
    public function a($href = null, $contents = null)
54
    {
55
        return A::create()
0 ignored issues
show
Documentation Bug introduced by
The method attributeIf does not exist on object<Spatie\Html\Elements\A>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
56
            ->attributeIf($href, 'href', $href)
57
            ->html($contents);
58
    }
59
60
    /**
61
     * @param string|null $href
0 ignored issues
show
Bug introduced by
There is no parameter named $href. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
62
     * @param string|null $text
0 ignored issues
show
Bug introduced by
There is no parameter named $text. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
63
     *
64
     * @return \Spatie\Html\Elements\I
65
     */
66
    public function i($contents = null)
67
    {
68
        return I::create()
69
            ->html($contents);
70
    }
71
72
    /**
73
     * @param string|null $type
74
     * @param string|null $text
0 ignored issues
show
Bug introduced by
There is no parameter named $text. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
75
     *
76
     * @return \Spatie\Html\Elements\Button
77
     */
78
    public function button($contents = null, $type = null, $name = null)
79
    {
80
        return Button::create()
0 ignored issues
show
Documentation Bug introduced by
The method attributeIf does not exist on object<Spatie\Html\Elements\Button>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
81
            ->attributeIf($type, 'type', $type)
82
            ->attributeIf($name, 'name', $this->fieldName($name))
83
            ->html($contents);
84
    }
85
86
    /**
87
     * @param \Illuminate\Support\Collection|iterable|string $classes
88
     *
89
     * @return \Illuminate\Contracts\Support\Htmlable
90
     */
91
    public function class($classes): Htmlable
92
    {
93
        if ($classes instanceof Collection) {
94
            $classes = $classes->toArray();
95
        }
96
97
        $attributes = new Attributes();
98
        $attributes->addClass($classes);
99
100
        return new HtmlString(
101
            $attributes->render()
102
        );
103
    }
104
105
    /**
106
     * @param string|null $name
107
     * @param bool $checked
108
     * @param string|null $value
109
     *
110
     * @return \Spatie\Html\Elements\Input
111
     */
112
    public function checkbox($name = null, $checked = null, $value = '1')
113
    {
114
        return $this->input('checkbox', $name, $value)
0 ignored issues
show
Documentation Bug introduced by
The method attributeIf does not exist on object<Spatie\Html\Elements\Input>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
115
            ->attributeIf(!is_null($value), 'value', $value)
116
            ->attributeIf((bool) $this->old($name, $checked), 'checked');
117
    }
118
119
    /**
120
     * @param \Spatie\Html\HtmlElement|string|null $contents
121
     *
122
     * @return \Spatie\Html\Elements\Div
123
     */
124
    public function div($contents = null)
125
    {
126
        return Div::create()->children($contents);
127
    }
128
129
    /**
130
     * @param string|null $name
131
     * @param string|null $value
132
     *
133
     * @return \Spatie\Html\Elements\Input
134
     */
135
    public function email($name = null, $value = null)
136
    {
137
        return $this->input('email', $name, $value);
138
    }
139
140
    /**
141
     * @param string|null $name
142
     * @param string|null $value
143
     * @param bool $format
144
     *
145
     * @return \Spatie\Html\Elements\Input
146
     */
147 View Code Duplication
    public function date($name = '', $value = null, $format = true)
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...
148
    {
149
        $element = $this->input('date', $name, $value);
150
151
        if (!$format || empty($element->getAttribute('value'))) {
152
            return $element;
153
        }
154
155
        return $element->value($this->formatDateTime($element->getAttribute('value'), self::HTML_DATE_FORMAT));
156
    }
157
158
    /**
159
     * @param string|null $name
160
     * @param string|null $value
161
     * @param string|null $min
162
     * @param string|null $max
163
     * @param string|null $step
164
     *
165
     * @return \Spatie\Html\Elements\Input
166
     */
167
    public function range($name = '', $value = '', $min = null, $max = null, $step = null)
168
    {
169
        return $this->input('range', $name, $value)
0 ignored issues
show
Documentation Bug introduced by
The method attributeIfNotNull does not exist on object<Spatie\Html\Elements\Input>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
170
            ->attributeIfNotNull($min, 'min', $min)
171
            ->attributeIfNotNull($max, 'max', $max)
172
            ->attributeIfNotNull($step, 'step', $step);
173
    }
174
175
    /**
176
     * @param string|null $name
177
     * @param string|null $value
178
     * @param bool $format
179
     *
180
     * @return \Spatie\Html\Elements\Input
181
     */
182 View Code Duplication
    public function time($name = '', $value = null, $format = true)
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...
183
    {
184
        $element = $this->input('time', $name, $value);
185
186
        if (!$format || empty($element->getAttribute('value'))) {
187
            return $element;
188
        }
189
190
        return $element->value($this->formatDateTime($element->getAttribute('value'), self::HTML_TIME_FORMAT));
191
    }
192
193
    /**
194
     * @param string $tag
195
     *
196
     * @return \Spatie\Html\Elements\Element
197
     */
198
    public function element($tag)
199
    {
200
        return Element::withTag($tag);
201
    }
202
203
    /**
204
     * @param string|null $type
205
     * @param string|null $name
206
     * @param string|null $value
207
     *
208
     * @return \Spatie\Html\Elements\Input
209
     */
210
    public function input($type = null, $name = null, $value = null)
211
    {
212
        $hasValue = $name && ($type !== 'password' && !is_null($this->old($name, $value)) || !is_null($value));
0 ignored issues
show
Bug Best Practice introduced by
The expression $name of type string|null is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
213
214
        return Input::create()
0 ignored issues
show
Documentation Bug introduced by
The method attributeIf does not exist on object<Spatie\Html\Elements\Input>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
215
            ->attributeIf($type, 'type', $type)
216
            ->attributeIf($name, 'name', $this->fieldName($name))
217
            ->attributeIf($name, 'id', $this->fieldName($name))
218
            ->attributeIf($hasValue, 'value', $this->old($name, $value));
219
    }
220
221
    /**
222
     * @param \Spatie\Html\HtmlElement|string|null $legend
223
     *
224
     * @return \Spatie\Html\Elements\Fieldset
225
     */
226
    public function fieldset($legend = null)
227
    {
228
        return $legend ?
229
            Fieldset::create()->legend($legend) : Fieldset::create();
230
    }
231
232
    /**
233
     * @param string $method
234
     * @param string|null $action
235
     *
236
     * @return \Spatie\Html\Elements\Form
237
     */
238
    public function form($method = 'POST', $action = null)
239
    {
240
        $method = strtoupper($method);
241
        $form = Form::create();
242
243
        // If Laravel needs to spoof the form's method, we'll append a hidden
244
        // field containing the actual method
245
        if (in_array($method, ['DELETE', 'PATCH', 'PUT'])) {
246
            $form = $form->addChild($this->hidden('_method')->value($method));
247
        }
248
249
        // On any other method that get, the form needs a CSRF token
250
        if ($method !== 'GET') {
251
            $form = $form->addChild($this->token());
252
        }
253
254
        return $form
0 ignored issues
show
Documentation Bug introduced by
The method attributeIf does not exist on object<Spatie\Html\Elements\Form>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
255
            ->method($method === 'GET' ? 'GET' : 'POST')
256
            ->attributeIf($action, 'action', $action);
257
    }
258
259
    /**
260
     * @param string|null $name
261
     * @param string|null $value
262
     *
263
     * @return \Spatie\Html\Elements\Input
264
     */
265
    public function hidden($name = null, $value = null)
266
    {
267
        return $this->input('hidden', $name, $value);
268
    }
269
270
    /**
271
     * @param string|null $src
272
     * @param string|null $alt
273
     *
274
     * @return \Spatie\Html\Elements\Img
275
     */
276
    public function img($src = null, $alt = null)
277
    {
278
        return Img::create()
0 ignored issues
show
Documentation Bug introduced by
The method attributeIf does not exist on object<Spatie\Html\Elements\Img>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
279
            ->attributeIf($src, 'src', $src)
280
            ->attributeIf($alt, 'alt', $alt);
281
    }
282
283
    /**
284
     * @param \Spatie\Html\HtmlElement|iterable|string|null $contents
285
     * @param string|null $for
286
     *
287
     * @return \Spatie\Html\Elements\Label
288
     */
289
    public function label($contents = null, $for = null)
290
    {
291
        return Label::create()
0 ignored issues
show
Documentation Bug introduced by
The method attributeIf does not exist on object<Spatie\Html\Elements\Label>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
292
            ->attributeIf($for, 'for', $this->fieldName($for))
293
            ->children($contents);
294
    }
295
296
    /**
297
     * @param \Spatie\Html\HtmlElement|string|null $contents
298
     *
299
     * @return \Spatie\Html\Elements\Legend
300
     */
301
    public function legend($contents = null)
302
    {
303
        return Legend::create()->html($contents);
0 ignored issues
show
Bug introduced by
It seems like $contents defined by parameter $contents on line 301 can also be of type object<Spatie\Html\HtmlElement>; however, Spatie\Html\BaseElement::html() does only seem to accept string|null, maybe add an additional type check?

This check looks at variables that have been passed in as parameters and are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
304
    }
305
306
    /**
307
     * @param string $email
308
     * @param string|null $text
309
     *
310
     * @return \Spatie\Html\Elements\A
311
     */
312
    public function mailto($email, $text = null)
313
    {
314
        return $this->a('mailto:' . $email, $text ?: $email);
315
    }
316
317
    /**
318
     * @param string|null $name
319
     * @param iterable $options
320
     * @param string|iterable|null $value
321
     *
322
     * @return \Spatie\Html\Elements\Select
323
     */
324 View Code Duplication
    public function multiselect($name = null, $options = [], $value = 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...
325
    {
326
        return Select::create()
0 ignored issues
show
Documentation Bug introduced by
The method attributeIf does not exist on object<Spatie\Html\Elements\Select>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
327
            ->attributeIf($name, 'name', $this->fieldName($name))
328
            ->attributeIf($name, 'id', $this->fieldName($name))
329
            ->options($options)
330
            ->value($name ? $this->old($name, $value) : $value)
331
            ->multiple();
332
    }
333
334
    /**
335
     * @param string|null $name
336
     * @param string|null $value
337
     * @param string|null $step
338
     *
339
     * @return \Spatie\Html\Elements\Input
340
     */
341
    public function number($name = null, $value = null, $min = null, $max = null, $step = null)
342
    {
343
        return $this->input('number', $name, $value)
0 ignored issues
show
Documentation Bug introduced by
The method attributeIfNotNull does not exist on object<Spatie\Html\Elements\Input>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
344
                ->attributeIfNotNull($min, 'min', $min)
345
                ->attributeIfNotNull($max, 'max', $max)
346
                ->attributeIfNotNull($step, 'step', $step);
347
    }
348
349
    /**
350
     * @param string|null $text
351
     * @param string|null $value
352
     * @param bool $selected
353
     *
354
     * @return \Spatie\Html\Elements\Option
355
     */
356
    public function option($text = null, $value = null, $selected = false)
357
    {
358
        return Option::create()
359
            ->text($text)
360
            ->value($value)
361
            ->selectedIf($selected);
362
    }
363
364
    /**
365
     * @param string|null $value
0 ignored issues
show
Bug introduced by
There is no parameter named $value. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
366
     *
367
     * @return \Spatie\Html\Elements\Input
368
     */
369
    public function password($name = null)
370
    {
371
        return $this->input('password', $name);
372
    }
373
374
    /**
375
     * @param string|null $name
376
     * @param bool $checked
377
     * @param string|null $value
378
     *
379
     * @return \Spatie\Html\Elements\Input
380
     */
381
    public function radio($name = null, $checked = null, $value = null)
382
    {
383
        return $this->input('radio', $name, $value)
0 ignored issues
show
Documentation Bug introduced by
The method attributeIf does not exist on object<Spatie\Html\Elements\Input>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
384
            ->attributeIf($name, 'id', $value === null ? $name : ($name . '_' . Str::slug($value)))
385
            ->attributeIf(!is_null($value), 'value', $value)
386
            ->attributeIf((!is_null($value) && $this->old($name) == $value) || $checked, 'checked');
387
    }
388
389
    /**
390
     * @param string|null $name
391
     * @param iterable $options
392
     * @param string|iterable|null $value
393
     *
394
     * @return \Spatie\Html\Elements\Select
395
     */
396 View Code Duplication
    public function select($name = null, $options = [], $value = 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...
397
    {
398
        return Select::create()
0 ignored issues
show
Documentation Bug introduced by
The method attributeIf does not exist on object<Spatie\Html\Elements\Select>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
399
            ->attributeIf($name, 'name', $this->fieldName($name))
400
            ->attributeIf($name, 'id', $this->fieldName($name))
401
            ->options($options)
402
            ->value($name ? $this->old($name, $value) : $value);
403
    }
404
405
    /**
406
     * @param \Spatie\Html\HtmlElement|string|null $contents
407
     *
408
     * @return \Spatie\Html\Elements\Span
409
     */
410
    public function span($contents = null)
411
    {
412
        return Span::create()->children($contents);
413
    }
414
415
    /**
416
     * @param string|null $text
417
     *
418
     * @return \Spatie\Html\Elements\Button
419
     */
420
    public function submit($text = null)
421
    {
422
        return $this->button($text, 'submit');
423
    }
424
425
    /**
426
     * @param string|null $text
427
     *
428
     * @return \Spatie\Html\Elements\Button
429
     */
430
    public function reset($text = null)
431
    {
432
        return $this->button($text, 'reset');
433
    }
434
435
    /**
436
     * @param string $number
437
     * @param string|null $text
438
     *
439
     * @return \Spatie\Html\Elements\A
440
     */
441
    public function tel($number, $text = null)
442
    {
443
        return $this->a('tel:' . $number, $text ?: $number);
444
    }
445
446
    /**
447
     * @param string|null $name
448
     * @param string|null $value
449
     *
450
     * @return \Spatie\Html\Elements\Input
451
     */
452
    public function text($name = null, $value = null)
453
    {
454
        return $this->input('text', $name, $value);
455
    }
456
457
    /**
458
     * @param string|null $name
459
     *
460
     * @return \Spatie\Html\Elements\File
461
     */
462
    public function file($name = null)
463
    {
464
        return File::create()
0 ignored issues
show
Documentation Bug introduced by
The method attributeIf does not exist on object<Spatie\Html\Elements\File>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
465
            ->attributeIf($name, 'name', $this->fieldName($name))
466
            ->attributeIf($name, 'id', $this->fieldName($name));
467
    }
468
469
    /**
470
     * @param string|null $name
471
     * @param string|null $value
472
     *
473
     * @return \Spatie\Html\Elements\Textarea
474
     */
475
    public function textarea($name = null, $value = null)
476
    {
477
        return Textarea::create()
0 ignored issues
show
Documentation Bug introduced by
The method attributeIf does not exist on object<Spatie\Html\Elements\Textarea>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
478
            ->attributeIf($name, 'name', $this->fieldName($name))
479
            ->attributeIf($name, 'id', $this->fieldName($name))
480
            ->value($this->old($name, $value));
481
    }
482
483
    /**
484
     * @return \Spatie\Html\Elements\Input
485
     */
486
    public function token()
487
    {
488
        return $this
489
            ->hidden()
490
            ->name('_token')
491
            ->value($this->request->session()->token());
0 ignored issues
show
Bug introduced by
The method token() does not seem to exist on object<Symfony\Component...ssion\SessionInterface>.

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...
492
    }
493
494
    /**
495
     * @param \ArrayAccess|array $model
496
     *
497
     * @return $this
498
     */
499
    public function model($model)
500
    {
501
        $this->model = $model;
502
503
        return $this;
504
    }
505
506
    /**
507
     * @param \ArrayAccess|array $model
508
     * @param string|null $method
509
     * @param string|null $action
510
     *
511
     * @return \Spatie\Html\Elements\Form
512
     */
513
    public function modelForm($model, $method = 'POST', $action = null): Form
514
    {
515
        $this->model($model);
516
517
        return $this->form($method, $action);
518
    }
519
520
    /**
521
     * @return $this
522
     */
523
    public function endModel()
524
    {
525
        $this->model = null;
526
527
        return $this;
528
    }
529
530
    /**
531
     * @return \Illuminate\Contracts\Support\Htmlable
532
     */
533
    public function closeModelForm(): Htmlable
534
    {
535
        $this->endModel();
536
537
        return $this->form()->close();
538
    }
539
540
    /**
541
     * @param string $name
542
     * @param mixed $value
543
     *
544
     * @return mixed
545
     */
546
    protected function old($name, $value = null)
547
    {
548
        if (empty($name)) {
549
            return;
550
        }
551
552
        // Convert array format (sth[1]) to dot notation (sth.1)
553
        $name = preg_replace('/\[(.+)\]/U', '.$1', $name);
554
555
        // If there's no default value provided, the html builder currently
556
        // has a model assigned and there aren't old input items,
557
        // try to retrieve a value from the model.
558
        if (is_null($value) && $this->model && empty($this->request->old())) {
559
            $value = data_get($this->model, $name) ?? '';
560
        }
561
562
        return $this->request->old($name, $value);
563
    }
564
565
    /**
566
     * Retrieve the value from the current session or assigned model. This is
567
     * a public alias for `old`.
568
     *
569
     * @param string $name
570
     * @param mixed $value
0 ignored issues
show
Bug introduced by
There is no parameter named $value. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
571
     *
572
     * @return mixed
573
     */
574
    public function value($name, $default = null)
575
    {
576
        return $this->old($name, $default);
577
    }
578
579
    /**
580
     * @param string $name
581
     *
582
     * @return string
583
     */
584
    protected function fieldName($name)
585
    {
586
        return $name;
587
    }
588
589
    protected function ensureModelIsAvailable()
590
    {
591
        if (empty($this->model)) {
592
            throw new Exception('Method requires a model to be set on the html builder');
593
        }
594
    }
595
596
    /**
597
     * @param string $value
598
     * @param string $format DateTime formatting string supported by date_format()
599
     * @return string
600
     */
601
    protected function formatDateTime($value, $format)
602
    {
603
        if (empty($value)) {
604
            return $value;
605
        }
606
607
        try {
608
            $date = new DateTimeImmutable($value);
609
610
            return $date->format($format);
611
        } catch (\Exception $e) {
612
            return $value;
613
        }
614
    }
615
}
616