Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
19 / 19 |
|
100.00% |
6 / 6 |
CRAP | |
100.00% |
1 / 1 |
| Button | |
100.00% |
19 / 19 |
|
100.00% |
6 / 6 |
10 | |
100.00% |
1 / 1 |
| __construct | |
100.00% |
12 / 12 |
|
100.00% |
1 / 1 |
5 | |||
| setValue | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
| resetValue | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
| getType | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| getValue | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| validate | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| 1 | <?php |
| 2 | declare(strict_types=1); |
| 3 | /** |
| 4 | * Pop PHP Framework (https://www.popphp.org/) |
| 5 | * |
| 6 | * @link https://github.com/popphp/popphp-framework |
| 7 | * @author Nick Sagona, III <nick@popphp.org> |
| 8 | * @copyright Copyright (c) 2009-2026 Nick Sagona, III |
| 9 | * @license https://www.popphp.org/license New BSD License |
| 10 | */ |
| 11 | |
| 12 | /** |
| 13 | * @namespace |
| 14 | */ |
| 15 | namespace Pop\Form\Element; |
| 16 | |
| 17 | /** |
| 18 | * Form button element class |
| 19 | * |
| 20 | * @category Pop |
| 21 | * @package Pop\Form |
| 22 | * @author Nick Sagona, III <nick@popphp.org> |
| 23 | * @copyright Copyright (c) 2009-2026 Nick Sagona, III |
| 24 | * @license https://www.popphp.org/license New BSD License |
| 25 | * @version 5.0.0 |
| 26 | */ |
| 27 | |
| 28 | class Button extends AbstractElement |
| 29 | { |
| 30 | |
| 31 | /** |
| 32 | * Constructor |
| 33 | * |
| 34 | * Instantiate the button form element. |
| 35 | * |
| 36 | * @param string $name |
| 37 | * @param ?string $value |
| 38 | * @param ?string $indent |
| 39 | */ |
| 40 | public function __construct(string $name, ?string $value = null, ?string $indent = null) |
| 41 | { |
| 42 | parent::__construct('button', $value); |
| 43 | |
| 44 | $this->setAttributes(['name' => $name, 'id' => $name]); |
| 45 | |
| 46 | if (strtolower($name) == 'submit') { |
| 47 | $this->setAttribute('type', 'submit'); |
| 48 | } else if (strtolower($name) == 'reset') { |
| 49 | $this->setAttribute('type', 'reset'); |
| 50 | } else{ |
| 51 | $this->setAttribute('type', 'button'); |
| 52 | } |
| 53 | |
| 54 | $this->setName($name); |
| 55 | |
| 56 | if ($value !== null) { |
| 57 | $this->setValue($value); |
| 58 | } |
| 59 | |
| 60 | if ($indent !== null) { |
| 61 | $this->setIndent($indent); |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | /** |
| 66 | * Set the value of the form button element object |
| 67 | * |
| 68 | * @param mixed $value |
| 69 | * @return Button |
| 70 | */ |
| 71 | public function setValue(mixed $value = null): Button |
| 72 | { |
| 73 | $this->setNodeValue($value); |
| 74 | return $this; |
| 75 | } |
| 76 | |
| 77 | /** |
| 78 | * Reset the value of the form element |
| 79 | * |
| 80 | * @return Button |
| 81 | */ |
| 82 | public function resetValue(): Button |
| 83 | { |
| 84 | $this->setNodeValue(''); |
| 85 | return $this; |
| 86 | } |
| 87 | |
| 88 | /** |
| 89 | * Get form element object type |
| 90 | * |
| 91 | * @return string |
| 92 | */ |
| 93 | public function getType(): string |
| 94 | { |
| 95 | return 'button'; |
| 96 | } |
| 97 | |
| 98 | /** |
| 99 | * Get the value of the form button element object |
| 100 | * |
| 101 | * @return ?string |
| 102 | */ |
| 103 | public function getValue(): ?string |
| 104 | { |
| 105 | return $this->getNodeValue(); |
| 106 | } |
| 107 | |
| 108 | /** |
| 109 | * Validate the form element object |
| 110 | * |
| 111 | * @param array $formValues |
| 112 | * @return bool |
| 113 | */ |
| 114 | public function validate(array $formValues = []): bool |
| 115 | { |
| 116 | return (count($this->errors) == 0); |
| 117 | } |
| 118 | |
| 119 | } |