Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
7 / 7 |
|
100.00% |
4 / 4 |
CRAP | |
100.00% |
1 / 1 |
| Attribute | |
100.00% |
7 / 7 |
|
100.00% |
4 / 4 |
5 | |
100.00% |
1 / 1 |
| __construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| getName | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| setName | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
2 | |||
| isValidAttributeIdName | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
| render | n/a |
0 / 0 |
n/a |
0 / 0 |
0 | |||||
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * @author: Doug Wilbourne (dougwilbourne@gmail.com) |
| 5 | */ |
| 6 | |
| 7 | declare(strict_types=1); |
| 8 | |
| 9 | namespace pvc\html\attribute; |
| 10 | |
| 11 | use pvc\html\err\InvalidAttributeIdNameException; |
| 12 | use pvc\interfaces\html\attribute\AttributeInterface; |
| 13 | |
| 14 | /** |
| 15 | * Class Attribute |
| 16 | * |
| 17 | */ |
| 18 | abstract class Attribute implements AttributeInterface |
| 19 | { |
| 20 | |
| 21 | public function __construct( |
| 22 | protected(set) string $name, |
| 23 | ) |
| 24 | { |
| 25 | $this->setName($name); |
| 26 | } |
| 27 | |
| 28 | /** |
| 29 | * getName |
| 30 | * @return string |
| 31 | */ |
| 32 | public function getName(): string |
| 33 | { |
| 34 | return $this->name; |
| 35 | } |
| 36 | |
| 37 | /** |
| 38 | * setName |
| 39 | * @param string $name |
| 40 | * @throws InvalidAttributeIdNameException |
| 41 | * |
| 42 | */ |
| 43 | protected function setName(string $name): void |
| 44 | { |
| 45 | if (!$this->isValidAttributeIdName($name)) { |
| 46 | throw new InvalidAttributeIdNameException($name); |
| 47 | } |
| 48 | $this->name = $name; |
| 49 | } |
| 50 | |
| 51 | /** |
| 52 | * isValidAttributeName |
| 53 | * @param string $name |
| 54 | * @return bool |
| 55 | * |
| 56 | * This regex restricts the name to start with a lower case letter and then can be followed by lower case letters |
| 57 | * and numbers and hyphens. |
| 58 | * |
| 59 | * As a practical matter, pre-defined html attribute names are purely alphabetic with a couple that are |
| 60 | * hyphenated. And since the usual manner of |
| 61 | * creating an attribute is in a htmlBuilder / container, most times the attribute names come right from the |
| 62 | * html specification. However, you can create an attribute using an arbitrary name and at least in some browsers, |
| 63 | * you can get at the value using javascript even if the id is not prefixed with 'data-'. So this |
| 64 | * validation tries to find the middle ground between what the language spec says and how browsers |
| 65 | * actually work. |
| 66 | * |
| 67 | * Moreover, according to various online sources, the data attribute id (i.e. custom attribute names that |
| 68 | * are prefixed with 'data-') must be at least one character long, must be prefixed with 'data-', and |
| 69 | * should not contain any uppercase letters. This method is inherited and used by the AttributeCustomData |
| 70 | * class. |
| 71 | */ |
| 72 | protected function isValidAttributeIdName(string $name): bool |
| 73 | { |
| 74 | $pattern = '/^[a-z]+[a-z0-9\-]*$/'; |
| 75 | return (bool) preg_match($pattern, $name); |
| 76 | } |
| 77 | |
| 78 | abstract public function render(): string; |
| 79 | } |