FormValidatedInput   A
last analyzed

Complexity

Total Complexity 15

Size/Duplication

Total Lines 117
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 48
c 0
b 0
f 0
dl 0
loc 117
rs 10
wmc 15

5 Methods

Rating   Name   Duplication   Size   Complexity  
A render() 0 6 2
A getType() 0 3 1
A __construct() 0 24 1
A getHtmlTypes() 0 3 1
B setType() 0 28 10
1
<?php declare(strict_types=1);
2
3
namespace XoopsModules\Tag;
4
5
/*
6
 You may not change or alter any portion of this comment or credits
7
 of supporting developers from this source code or any supporting source code
8
 which is considered copyrighted (c) material of the original comment or credit authors.
9
10
 This program is distributed in the hope that it will be useful,
11
 but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13
*/
14
15
/**
16
 * Tag form element for form input
17
 *
18
 * @copyright       {@link https://sourceforge.net/projects/xoops/ The XOOPS Project}
19
 * @license         {@link https://www.gnu.org/licenses/old-licenses/gpl-2.0.html GNU GPL 2}
20
 * @author          Kazumi Ono (AKA onokazu) https://www.myweb.ne.jp/, https://jp.xoops.org/
21
 * @author          ZySpec <[email protected]>
22
 * @since           2.33
23
 */
24
25
/**
26
 * A simple HTML5 type validated input field
27
 */
28
class FormValidatedInput extends \XoopsFormText
29
{
30
    /**
31
     * Initial type
32
     *
33
     * @var string
34
     */
35
    private $_type;
36
    /**
37
     * Valid HTML Type array
38
     *
39
     * @var array
40
     */
41
    private $_htmlTypes;
42
43
    /**
44
     * Constructor
45
     *
46
     * @param string $caption   Caption
47
     * @param string $name      "name" attribute
48
     * @param int    $size      Size
49
     * @param int    $maxlength Maximum length of text
50
     * @param string $value     Initial text
51
     */
52
    public function __construct($caption, $name, $size, $maxlength, $value = '', string $type = 'text')
53
    {
54
        $this->_htmlTypes = [
55
            'color',
56
            'date',
57
            'datetime',
58
            'datetime-local',
59
            'email',
60
            'month',
61
            'number',
62
            'range',
63
            'search',
64
            'tel',
65
            'text',
66
            'time',
67
            'url',
68
            'week',
69
        ];
70
        $this->setCaption($caption);
71
        $this->setName($name);
72
        $this->_size      = (int)$size;
73
        $this->_maxlength = (int)$maxlength;
74
        $this->setValue($value);
75
        $this->setType($type);
76
    }
77
78
    /**
79
     * Get type information value
80
     *
81
     * @return string containing type
82
     */
83
    public function getType(): string
84
    {
85
        return $this->_type;
86
    }
87
88
    /**
89
     * Get HTML types supported
90
     *
91
     * @return array containing HTML type(s) supported
92
     */
93
    public function getHtmlTypes(): array
94
    {
95
        return $this->_htmlTypes;
96
    }
97
98
    /**
99
     * Set initial text value
100
     *
101
     * @param string|array $value is string, set value; value is array then keys are ('type', 'min', 'max')
102
     */
103
    public function setType($value = ''): void
104
    {
105
        if ('' !== $value) {
106
            if (\is_array($value)) {
107
                $value       = isset($value['type']) ? \mb_strtolower($value['type']) : 'text';
108
                $this->_type = \in_array($value, $this->_htmlTypes, true) ? $value : 'text';
109
                if (\in_array(
110
                    $value['type'],
111
                    [
112
                        'number',
113
                        'date',
114
                        'range',
115
                    ],
116
                    true
117
                )) {
118
                    if (isset($value['min'])) {
119
                        $this->setExtra('min=' . $value['min']);
120
                    }
121
                    if (isset($value['max'])) {
122
                        $this->setExtra('max=' . $value['max']);
123
                    }
124
                }
125
            } else {
126
                $value       = isset($value) ? \mb_strtolower($value) : 'text';
127
                $this->_type = \in_array($value, $this->_htmlTypes, true) ? $value : 'text';
128
            }
129
        } else {
130
            $this->_type = 'text';
131
        }
132
    }
133
134
    /**
135
     * Prepare HTML for output
136
     *
137
     * @return string HTML <input>
138
     */
139
    public function render(): string
140
    {
141
        $myClasses = $this->getClass();
142
        $classes   = $myClasses ? " class='{$myClasses}'" : '';
143
144
        return "<input type='" . $this->_type . "' name='" . $this->getName() . "' title='" . $this->getTitle() . "' id='" . $this->getName() . "' size='" . $this->getSize() . "' maxlength='" . $this->getMaxlength() . "' value='" . $this->getValue() . "'" . $classes . $this->getExtra() . '>';
145
    }
146
}
147