Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
6 / 6
100.00% covered (success)
100.00%
1 / 1
CRAP
100.00% covered (success)
100.00%
1 / 1
AttributeCustomData
100.00% covered (success)
100.00%
6 / 6
100.00% covered (success)
100.00%
1 / 1
3
100.00% covered (success)
100.00%
1 / 1
 setName
100.00% covered (success)
100.00%
6 / 6
100.00% covered (success)
100.00%
1 / 1
3
1<?php
2
3/**
4 * @author: Doug Wilbourne (dougwilbourne@gmail.com)
5 */
6declare(strict_types=1);
7
8namespace pvc\html\attribute;
9
10use pvc\html\err\InvalidCustomDataNameException;
11
12/**
13 * Class AttributeCustomData
14 */
15class AttributeCustomData extends AttributeMultiValue
16{
17    /**
18     * setName
19     *
20     * @param  string  $name
21     *
22     * @throws InvalidCustomDataNameException
23     * Custom attributes are stored in the element with the prefix so
24     * that we can allow an element to have an 'href' attribute and a 'data-href' attribute.
25     */
26    public function setName(string $name): void
27    {
28        /**
29         * according to various online sources, the data attribute id must be at least one character long and must
30         * be prefixed with 'data-'. It should not contain any uppercase letters.  The isValidAttributeIdName method
31         * regex restricts it to lower case letters and numbers
32         */
33        if (!str_starts_with($name, 'data-')) {
34            throw new InvalidCustomDataNameException($name);
35        }
36
37        $suffix = substr($name, 5);
38        if (!$this->isValidAttributeIdName($suffix)) {
39            throw new InvalidCustomDataNameException($name);
40        }
41        $this->name = $name;
42    }
43}