Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
19 / 19
100.00% covered (success)
100.00%
6 / 6
CRAP
100.00% covered (success)
100.00%
1 / 1
Button
100.00% covered (success)
100.00%
19 / 19
100.00% covered (success)
100.00%
6 / 6
10
100.00% covered (success)
100.00%
1 / 1
 __construct
100.00% covered (success)
100.00%
12 / 12
100.00% covered (success)
100.00%
1 / 1
5
 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%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
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 button 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 Button extends AbstractElement
28{
29
30    /**
31     * Constructor
32     *
33     * Instantiate the button 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('button', $value);
42
43        $this->setAttributes(['name' => $name, 'id' => $name]);
44
45        if (strtolower($name) == 'submit') {
46            $this->setAttribute('type', 'submit');
47        } else if (strtolower($name) == 'reset') {
48            $this->setAttribute('type', 'reset');
49        } else{
50            $this->setAttribute('type', 'button');
51        }
52
53        $this->setName($name);
54
55        if ($value !== null) {
56            $this->setValue($value);
57        }
58
59        if ($indent !== null) {
60            $this->setIndent($indent);
61        }
62    }
63
64    /**
65     * Set the value of the form button element object
66     *
67     * @param  mixed $value
68     * @return Button
69     */
70    public function setValue(mixed $value): Button
71    {
72        $this->setNodeValue($value);
73        return $this;
74    }
75
76    /**
77     * Reset the value of the form element
78     *
79     * @return Button
80     */
81    public function resetValue(): Button
82    {
83        $this->setNodeValue('');
84        return $this;
85    }
86
87    /**
88     * Get form element object type
89     *
90     * @return string
91     */
92    public function getType(): string
93    {
94        return 'button';
95    }
96
97    /**
98     * Get the value of the form button element object
99     *
100     * @return ?string
101     */
102    public function getValue(): ?string
103    {
104        return $this->getNodeValue();
105    }
106
107    /**
108     * Validate the form element object
109     *
110     * @param  array $formValues
111     * @return bool
112     */
113    public function validate(array $formValues = []): bool
114    {
115        return (count($this->errors) == 0);
116    }
117
118}