Completed
Push — master ( f2dc24...821a00 )
by Sebastian
01:48 queued 11s
created

Html::checkbox()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 3
dl 0
loc 6
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Spatie\Html;
4
5
use DateTimeImmutable;
6
use Spatie\Html\Elements\A;
7
use Spatie\Html\Elements\I;
8
use Illuminate\Http\Request;
9
use Spatie\Html\Elements\Div;
10
use Spatie\Html\Elements\Img;
11
use Spatie\Html\Elements\File;
12
use Spatie\Html\Elements\Form;
13
use Spatie\Html\Elements\Span;
14
use Spatie\Html\Elements\Input;
15
use Spatie\Html\Elements\Label;
16
use Spatie\Html\Elements\Button;
17
use Spatie\Html\Elements\Legend;
18
use Spatie\Html\Elements\Option;
19
use Spatie\Html\Elements\Select;
20
use Spatie\Html\Elements\Element;
21
use Illuminate\Support\Collection;
22
use Illuminate\Support\HtmlString;
23
use Spatie\Html\Elements\Fieldset;
24
use Spatie\Html\Elements\Textarea;
25
use Illuminate\Support\Traits\Macroable;
26
use Illuminate\Contracts\Support\Htmlable;
27
28
class Html
29
{
30
    use Macroable;
31
32
    const HTML_DATE_FORMAT = 'Y-m-d';
33
    const HTML_TIME_FORMAT = 'H:i:s';
34
35
    /** @var \Illuminate\Http\Request */
36
    protected $request;
37
38
    /** @var \ArrayAccess|array */
39
    protected $model;
40
41
    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...
42
    {
43
        $this->request = $request;
44
    }
45
46
    /**
47
     * @param string|null $href
48
     * @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...
49
     *
50
     * @return \Spatie\Html\Elements\A
51
     */
52
    public function a($href = null, $contents = null)
53
    {
54
        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...
55
            ->attributeIf($href, 'href', $href)
56
            ->html($contents);
57
    }
58
59
    /**
60
     * @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...
61
     * @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...
62
     *
63
     * @return \Spatie\Html\Elements\I
64
     */
65
    public function i($contents = null)
66
    {
67
        return I::create()
68
            ->html($contents);
69
    }
70
71
    /**
72
     * @param string|null $type
73
     * @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...
74
     *
75
     * @return \Spatie\Html\Elements\Button
76
     */
77
    public function button($contents = null, $type = null, $name = null)
78
    {
79
        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...
80
            ->attributeIf($type, 'type', $type)
81
            ->attributeIf($name, 'name', $this->fieldName($name))
82
            ->html($contents);
83
    }
84
85
    /**
86
     * @param \Illuminate\Support\Collection|iterable|string $classes
87
     *
88
     * @return \Illuminate\Contracts\Support\Htmlable
89
     */
90
    public function class($classes): Htmlable
91
    {
92
        if ($classes instanceof Collection) {
93
            $classes = $classes->toArray();
94
        }
95
96
        $attributes = new Attributes();
97
        $attributes->addClass($classes);
98
99
        return new HtmlString(
100
            $attributes->render()
101
        );
102
    }
103
104
    /**
105
     * @param string|null $name
106
     * @param bool $checked
107
     * @param string|null $value
108
     *
109
     * @return \Spatie\Html\Elements\Input
110
     */
111
    public function checkbox($name = null, $checked = null, $value = '1')
112
    {
113
        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...
114
            ->attributeIf(! is_null($value), 'value', $value)
115
            ->attributeIf((bool) $this->old($name, $checked), 'checked');
116
    }
117
118
    /**
119
     * @param \Spatie\Html\HtmlElement|string|null $contents
120
     *
121
     * @return \Spatie\Html\Elements\Div
122
     */
123
    public function div($contents = null)
124
    {
125
        return Div::create()->children($contents);
126
    }
127
128
    /**
129
     * @param string|null $name
130
     * @param string|null $value
131
     *
132
     * @return \Spatie\Html\Elements\Input
133
     */
134
    public function email($name = null, $value = null)
135
    {
136
        return $this->input('email', $name, $value);
137
    }
138
139
    /**
140
     * @param string|null $name
141
     * @param string|null $value
142
     * @param bool $format
143
     *
144
     * @return \Spatie\Html\Elements\Input
145
     */
146 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...
147
    {
148
        $element = $this->input('date', $name, $value);
149
150
        if (! $format || empty($element->getAttribute('value'))) {
151
            return $element;
152
        }
153
154
        return $element->value($this->formatDateTime($element->getAttribute('value'), self::HTML_DATE_FORMAT));
155
    }
156
157
    /**
158
     * @param string|null $name
159
     * @param string|null $value
160
     * @param bool $format
161
     *
162
     * @return \Spatie\Html\Elements\Input
163
     */
164 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...
165
    {
166
        $element = $this->input('time', $name, $value);
167
168
        if (! $format || empty($element->getAttribute('value'))) {
169
            return $element;
170
        }
171
172
        return $element->value($this->formatDateTime($element->getAttribute('value'), self::HTML_TIME_FORMAT));
173
    }
174
175
    /**
176
     * @param string $tag
177
     *
178
     * @return \Spatie\Html\Elements\Element
179
     */
180
    public function element($tag)
181
    {
182
        return Element::withTag($tag);
183
    }
184
185
    /**
186
     * @param string|null $type
187
     * @param string|null $name
188
     * @param string|null $value
189
     *
190
     * @return \Spatie\Html\Elements\Input
191
     */
192
    public function input($type = null, $name = null, $value = null)
193
    {
194
        $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...
195
196
        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...
197
            ->attributeIf($type, 'type', $type)
198
            ->attributeIf($name, 'name', $this->fieldName($name))
199
            ->attributeIf($name, 'id', $this->fieldName($name))
200
            ->attributeIf($hasValue, 'value', $this->old($name, $value));
201
    }
202
203
    /**
204
     * @param \Spatie\Html\HtmlElement|string|null $legend
205
     *
206
     * @return \Spatie\Html\Elements\Fieldset
207
     */
208
    public function fieldset($legend = null)
209
    {
210
        return $legend ?
211
            Fieldset::create()->legend($legend) :
212
            Fieldset::create();
213
    }
214
215
    /**
216
     * @param string $method
217
     * @param string|null $action
218
     *
219
     * @return \Spatie\Html\Elements\Form
220
     */
221
    public function form($method = 'POST', $action = null)
222
    {
223
        $method = strtoupper($method);
224
        $form = Form::create();
225
226
        // If Laravel needs to spoof the form's method, we'll append a hidden
227
        // field containing the actual method
228
        if (in_array($method, ['DELETE', 'PATCH', 'PUT'])) {
229
            $form = $form->addChild($this->hidden('_method')->value($method));
230
        }
231
232
        // On any other method that get, the form needs a CSRF token
233
        if ($method !== 'GET') {
234
            $form = $form->addChild($this->token());
235
        }
236
237
        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...
238
            ->method($method === 'GET' ? 'GET' : 'POST')
239
            ->attributeIf($action, 'action', $action);
240
    }
241
242
    /**
243
     * @param string|null $name
244
     * @param string|null $value
245
     *
246
     * @return \Spatie\Html\Elements\Input
247
     */
248
    public function hidden($name = null, $value = null)
249
    {
250
        return $this->input('hidden', $name, $value);
251
    }
252
253
    /**
254
     * @param string|null $src
255
     * @param string|null $alt
256
     *
257
     * @return \Spatie\Html\Elements\Img
258
     */
259
    public function img($src = null, $alt = null)
260
    {
261
        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...
262
            ->attributeIf($src, 'src', $src)
263
            ->attributeIf($alt, 'alt', $alt);
264
    }
265
266
    /**
267
     * @param \Spatie\Html\HtmlElement|iterable|string|null $contents
268
     * @param string|null $for
269
     *
270
     * @return \Spatie\Html\Elements\Label
271
     */
272
    public function label($contents = null, $for = null)
273
    {
274
        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...
275
            ->attributeIf($for, 'for', $this->fieldName($for))
276
            ->children($contents);
277
    }
278
279
    /**
280
     * @param \Spatie\Html\HtmlElement|string|null $contents
281
     *
282
     * @return \Spatie\Html\Elements\Legend
283
     */
284
    public function legend($contents = null)
285
    {
286
        return Legend::create()->html($contents);
0 ignored issues
show
Bug introduced by
It seems like $contents defined by parameter $contents on line 284 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...
287
    }
288
289
    /**
290
     * @param string $email
291
     * @param string|null $text
292
     *
293
     * @return \Spatie\Html\Elements\A
294
     */
295
    public function mailto($email, $text = null)
296
    {
297
        return $this->a('mailto:'.$email, $text ?: $email);
298
    }
299
300
    /**
301
     * @param string|null $name
302
     * @param iterable $options
303
     * @param string|iterable|null $value
304
     *
305
     * @return \Spatie\Html\Elements\Select
306
     */
307 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...
308
    {
309
        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...
310
            ->attributeIf($name, 'name', $this->fieldName($name))
311
            ->attributeIf($name, 'id', $this->fieldName($name))
312
            ->options($options)
313
            ->value($name ? $this->old($name, $value) : $value)
314
            ->multiple();
315
    }
316
317
    /**
318
     * @param string|null $text
319
     * @param string|null $value
320
     * @param bool $selected
321
     *
322
     * @return \Spatie\Html\Elements\Option
323
     */
324
    public function option($text = null, $value = null, $selected = false)
325
    {
326
        return Option::create()
327
            ->text($text)
328
            ->value($value)
329
            ->selectedIf($selected);
330
    }
331
332
    /**
333
     * @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...
334
     *
335
     * @return \Spatie\Html\Elements\Input
336
     */
337
    public function password($name = null)
338
    {
339
        return $this->input('password', $name);
340
    }
341
342
    /**
343
     * @param string|null $name
344
     * @param bool $checked
345
     * @param string|null $value
346
     *
347
     * @return \Spatie\Html\Elements\Input
348
     */
349
    public function radio($name = null, $checked = null, $value = null)
350
    {
351
        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...
352
            ->attributeIf($name, 'id', $value === null ? $name : ($name.'_'.str_slug($value)))
0 ignored issues
show
Deprecated Code introduced by
The function str_slug() has been deprecated with message: Str::slug() should be used directly instead. Will be removed in Laravel 5.9.

This function has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed from the class and what other function to use instead.

Loading history...
353
            ->attributeIf(! is_null($value), 'value', $value)
354
            ->attributeIf((! is_null($value) && $this->old($name) == $value) || $checked, 'checked');
355
    }
356
357
    /**
358
     * @param string|null $name
359
     * @param iterable $options
360
     * @param string|iterable|null $value
361
     *
362
     * @return \Spatie\Html\Elements\Select
363
     */
364 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...
365
    {
366
        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...
367
            ->attributeIf($name, 'name', $this->fieldName($name))
368
            ->attributeIf($name, 'id', $this->fieldName($name))
369
            ->options($options)
370
            ->value($name ? $this->old($name, $value) : $value);
371
    }
372
373
    /**
374
     * @param \Spatie\Html\HtmlElement|string|null $contents
375
     *
376
     * @return \Spatie\Html\Elements\Span
377
     */
378
    public function span($contents = null)
379
    {
380
        return Span::create()->children($contents);
381
    }
382
383
    /**
384
     * @param string|null $text
385
     *
386
     * @return \Spatie\Html\Elements\Button
387
     */
388
    public function submit($text = null)
389
    {
390
        return $this->button($text, 'submit');
391
    }
392
393
    /**
394
     * @param string|null $text
395
     *
396
     * @return \Spatie\Html\Elements\Button
397
     */
398
    public function reset($text = null)
399
    {
400
        return $this->button($text, 'reset');
401
    }
402
403
    /**
404
     * @param string $number
405
     * @param string|null $text
406
     *
407
     * @return \Spatie\Html\Elements\A
408
     */
409
    public function tel($number, $text = null)
410
    {
411
        return $this->a('tel:'.$number, $text ?: $number);
412
    }
413
414
    /**
415
     * @param string|null $name
416
     * @param string|null $value
417
     *
418
     * @return \Spatie\Html\Elements\Input
419
     */
420
    public function text($name = null, $value = null)
421
    {
422
        return $this->input('text', $name, $value);
423
    }
424
425
    /**
426
     * @param string|null $name
427
     *
428
     * @return \Spatie\Html\Elements\File
429
     */
430
    public function file($name = null)
431
    {
432
        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...
433
            ->attributeIf($name, 'name', $this->fieldName($name))
434
            ->attributeIf($name, 'id', $this->fieldName($name));
435
    }
436
437
    /**
438
     * @param string|null $name
439
     * @param string|null $value
440
     *
441
     * @return \Spatie\Html\Elements\Textarea
442
     */
443
    public function textarea($name = null, $value = null)
444
    {
445
        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...
446
            ->attributeIf($name, 'name', $this->fieldName($name))
447
            ->attributeIf($name, 'id', $this->fieldName($name))
448
            ->value($this->old($name, $value));
449
    }
450
451
    /**
452
     * @return \Spatie\Html\Elements\Input
453
     */
454
    public function token()
455
    {
456
        return $this
457
            ->hidden()
458
            ->name('_token')
459
            ->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...
460
    }
461
462
    /**
463
     * @param \ArrayAccess|array $model
464
     *
465
     * @return $this
466
     */
467
    public function model($model)
468
    {
469
        $this->model = $model;
470
471
        return $this;
472
    }
473
474
    /**
475
     * @param \ArrayAccess|array $model
476
     * @param string|null $method
477
     * @param string|null $action
478
     *
479
     * @return \Spatie\Html\Elements\Form
480
     */
481
    public function modelForm($model, $method = 'POST', $action = null): Form
482
    {
483
        $this->model($model);
484
485
        return $this->form($method, $action);
486
    }
487
488
    /**
489
     * @return $this
490
     */
491
    public function endModel()
492
    {
493
        $this->model = null;
494
495
        return $this;
496
    }
497
498
    /**
499
     * @return \Illuminate\Contracts\Support\Htmlable
500
     */
501
    public function closeModelForm(): Htmlable
502
    {
503
        $this->endModel();
504
505
        return $this->form()->close();
506
    }
507
508
    /**
509
     * @param string $name
510
     * @param mixed $value
511
     *
512
     * @return mixed
513
     */
514
    protected function old($name, $value = null)
515
    {
516
        if (empty($name)) {
517
            return;
518
        }
519
520
        // Convert array format (sth[1]) to dot notation (sth.1)
521
        $name = preg_replace('/\[(.+)\]/U', '.$1', $name);
522
523
        // If there's no default value provided, the html builder currently
524
        // has a model assigned and there aren't old input items,
525
        // try to retrieve a value from the model.
526
        if (is_null($value) && $this->model && empty($this->request->old())) {
527
            $value = data_get($this->model, $name) ?? '';
528
        }
529
530
        return $this->request->old($name, $value);
531
    }
532
533
    /**
534
     * Retrieve the value from the current session or assigned model. This is
535
     * a public alias for `old`.
536
     *
537
     * @param string $name
538
     * @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...
539
     *
540
     * @return mixed
541
     */
542
    public function value($name, $default = null)
543
    {
544
        return $this->old($name, $default);
545
    }
546
547
    /**
548
     * @param string $name
549
     *
550
     * @return string
551
     */
552
    protected function fieldName($name)
553
    {
554
        return $name;
555
    }
556
557
    protected function ensureModelIsAvailable()
558
    {
559
        if (empty($this->model)) {
560
            throw new Exception('Method requires a model to be set on the html builder');
561
        }
562
    }
563
564
    /**
565
     * @param string $value
566
     * @param string $format DateTime formatting string supported by date_format()
567
     * @return string
568
     */
569
    protected function formatDateTime($value, $format)
570
    {
571
        if (empty($value)) {
572
            return $value;
573
        }
574
575
        try {
576
            $date = new DateTimeImmutable($value);
577
578
            return $date->format($format);
579
        } catch (\Exception $e) {
580
            return $value;
581
        }
582
    }
583
}
584