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     * @return Textarea
55     */
56    public function setRequired(bool $required): Textarea
57    {
58        if ($required) {
59            $this->setAttribute('required', 'required');
60        } else {
61            $this->removeAttribute('required');
62        }
63        return parent::setRequired($required);
64    }
65
66    /**
67     * Set whether the form element is disabled
68     *
69     * @param  bool $disabled
70     * @return Textarea
71     */
72    public function setDisabled(bool $disabled): Textarea
73    {
74        if ($disabled) {
75            $this->setAttribute('disabled', 'disabled');
76        } else {
77            $this->removeAttribute('disabled');
78        }
79        return parent::setDisabled($disabled);
80    }
81
82    /**
83     * Set whether the form element is readonly
84     *
85     * @param  bool $readonly
86     * @return Textarea
87     */
88    public function setReadonly(bool $readonly): Textarea
89    {
90        if ($readonly) {
91            $this->setAttribute('readonly', 'readonly');
92        } else {
93            $this->removeAttribute('readonly');
94        }
95        return parent::setReadonly($readonly);
96    }
97
98    /**
99     * Set the value of the form textarea element object
100     *
101     * @param  mixed $value
102     * @return Textarea
103     */
104    public function setValue(mixed $value): Textarea
105    {
106        $this->setNodeValue($value);
107        return $this;
108    }
109
110    /**
111     * Reset the value of the form element
112     *
113     * @return Textarea
114     */
115    public function resetValue(): Textarea
116    {
117        $this->setNodeValue('');
118        return $this;
119    }
120
121    /**
122     * Get form element object type
123     *
124     * @return string
125     */
126    public function getType(): string
127    {
128        return 'textarea';
129    }
130
131    /**
132     * Get the value of the form textarea element object
133     *
134     * @return ?string
135     */
136    public function getValue(): ?string
137    {
138        return $this->getNodeValue();
139    }
140
141    /**
142     * Validate the form element object
143     *
144     * @param  array $formValues
145     * @return bool
146     */
147    public function validate(array $formValues = []): bool
148    {
149        $value = $this->getValue();
150
151        // Check if the element is required
152        if (($this->required) && empty($value)) {
153            $this->errors[] = 'This field is required.';
154        }
155
156        $this->validateValue($value, $formValues);
157
158        return (count($this->errors) == 0);
159    }
160
161}