Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
28 / 28
100.00% covered (success)
100.00%
9 / 9
CRAP
100.00% covered (success)
100.00%
1 / 1
Textarea
100.00% covered (success)
100.00%
28 / 28
100.00% covered (success)
100.00%
9 / 9
15
100.00% covered (success)
100.00%
1 / 1
 __construct
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 1
2
 setRequired
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
2
 setDisabled
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
2
 setReadonly
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
2
 setValue
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 resetValue
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 getType
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getValue
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 validate
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 1
3
1<?php
2/**
3 * Pop PHP Framework (http://www.popphp.org/)
4 *
5 * @link       https://github.com/popphp/popphp-framework
6 * @author     Nick Sagona, III <dev@nolainteractive.com>
7 * @copyright  Copyright (c) 2009-2024 NOLA Interactive, LLC. (http://www.nolainteractive.com)
8 * @license    http://www.popphp.org/license     New BSD License
9 */
10
11/**
12 * @namespace
13 */
14namespace Pop\Form\Element;
15
16/**
17 * Form textarea element class
18 *
19 * @category   Pop
20 * @package    Pop\Form
21 * @author     Nick Sagona, III <dev@nolainteractive.com>
22 * @copyright  Copyright (c) 2009-2024 NOLA Interactive, LLC. (http://www.nolainteractive.com)
23 * @license    http://www.popphp.org/license     New BSD License
24 * @version    4.0.0
25 */
26
27class Textarea extends AbstractElement
28{
29
30    /**
31     * Constructor
32     *
33     * Instantiate the textarea form element
34     *
35     * @param  string  $name
36     * @param  ?string $value
37     * @param  ?string $indent
38     */
39    public function __construct(string $name, ?string $value = null, ?string $indent = null)
40    {
41        parent::__construct('textarea', $value);
42
43        $this->setAttributes(['name' => $name, 'id' => $name]);
44        $this->setName($name);
45        if ($indent !== null) {
46            $this->setIndent($indent);
47        }
48    }
49
50    /**
51     * Set whether the form element is required
52     *
53     * @param  bool    $required
54     * @param  ?string $requiredMessage
55     * @return Textarea
56     */
57    public function setRequired(bool $required, ?string $requiredMessage = 'This field is required.'): Textarea
58    {
59        if ($required) {
60            $this->setAttribute('required', 'required');
61        } else {
62            $this->removeAttribute('required');
63        }
64        return parent::setRequired($required, $requiredMessage);
65    }
66
67    /**
68     * Set whether the form element is disabled
69     *
70     * @param  bool $disabled
71     * @return Textarea
72     */
73    public function setDisabled(bool $disabled): Textarea
74    {
75        if ($disabled) {
76            $this->setAttribute('disabled', 'disabled');
77        } else {
78            $this->removeAttribute('disabled');
79        }
80        return parent::setDisabled($disabled);
81    }
82
83    /**
84     * Set whether the form element is readonly
85     *
86     * @param  bool $readonly
87     * @return Textarea
88     */
89    public function setReadonly(bool $readonly): Textarea
90    {
91        if ($readonly) {
92            $this->setAttribute('readonly', 'readonly');
93        } else {
94            $this->removeAttribute('readonly');
95        }
96        return parent::setReadonly($readonly);
97    }
98
99    /**
100     * Set the value of the form textarea element object
101     *
102     * @param  mixed $value
103     * @return Textarea
104     */
105    public function setValue(mixed $value): Textarea
106    {
107        $this->setNodeValue($value);
108        return $this;
109    }
110
111    /**
112     * Reset the value of the form element
113     *
114     * @return Textarea
115     */
116    public function resetValue(): Textarea
117    {
118        $this->setNodeValue('');
119        return $this;
120    }
121
122    /**
123     * Get form element object type
124     *
125     * @return string
126     */
127    public function getType(): string
128    {
129        return 'textarea';
130    }
131
132    /**
133     * Get the value of the form textarea element object
134     *
135     * @return ?string
136     */
137    public function getValue(): ?string
138    {
139        return $this->getNodeValue();
140    }
141
142    /**
143     * Validate the form element object
144     *
145     * @param  array $formValues
146     * @return bool
147     */
148    public function validate(array $formValues = []): bool
149    {
150        $value = $this->getValue();
151
152        // Check if the element is required
153        if (($this->required) && empty($value)) {
154            $this->errors[] = $this->getRequiredMessage();
155        }
156
157        $this->validateValue($value, $formValues);
158
159        return (count($this->errors) == 0);
160    }
161
162}