summaryrefslogtreecommitdiffstats
path: root/vendor/ipl/html/src/Common/MultipleAttribute.php
blob: 00a68b2bcea6b5d36bf992616abc52bf453aeeed (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
<?php

namespace ipl\Html\Common;

use ipl\Html\Attributes;
use ipl\Html\Contract\FormElement;

/**
 * Trait for form elements that can have the `multiple` attribute
 *
 * **Example usage:**
 *
 * ```
 * namespace ipl\Html\FormElement;
 *
 * use ipl\Html\Common\MultipleAttribute;
 *
 * class SelectElement extends BaseFormElement
 * {
 *     protected function registerAttributeCallbacks(Attributes $attributes)
 *     {
 *         // ...
 *         $this->registerMultipleAttributeCallback($attributes);
 *     }
 * }
 * ```
 */
trait MultipleAttribute
{
    /** @var bool Whether the attribute `multiple` is set to `true` */
    protected $multiple = false;

    /**
     * Get whether the attribute `multiple` is set to `true`
     *
     * @return bool
     */
    public function isMultiple(): bool
    {
        return $this->multiple;
    }

    /**
     * Set the `multiple` attribute
     *
     * @param bool $multiple
     *
     * @return $this
     */
    public function setMultiple(bool $multiple): self
    {
        $this->multiple = $multiple;

        return $this;
    }

    /**
     * Register the callback for `multiple` Attribute
     *
     * @param Attributes $attributes
     */
    protected function registerMultipleAttributeCallback(Attributes $attributes): void
    {
        $attributes->registerAttributeCallback(
            'multiple',
            [$this, 'isMultiple'],
            [$this, 'setMultiple']
        );
    }
}