Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
100.00% |
28 / 28 |
|
100.00% |
6 / 6 |
CRAP | |
100.00% |
1 / 1 |
HtmlFactory | |
100.00% |
28 / 28 |
|
100.00% |
6 / 6 |
9 | |
100.00% |
1 / 1 |
__construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
makeElement | |
100.00% |
6 / 6 |
|
100.00% |
1 / 1 |
2 | |||
makeAttribute | |
100.00% |
6 / 6 |
|
100.00% |
1 / 1 |
2 | |||
makeEvent | |
100.00% |
6 / 6 |
|
100.00% |
1 / 1 |
2 | |||
makeCustomData | |
100.00% |
6 / 6 |
|
100.00% |
1 / 1 |
1 | |||
makeTextNode | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
1 |
1 | <?php |
2 | |
3 | namespace pvc\html\factory; |
4 | |
5 | use Psr\Container\ContainerExceptionInterface; |
6 | use Psr\Container\ContainerInterface; |
7 | use Psr\Container\NotFoundExceptionInterface; |
8 | use pvc\html\attribute\AttributeCustomData; |
9 | use pvc\html\err\InvalidAttributeException; |
10 | use pvc\html\err\InvalidEventNameException; |
11 | use pvc\html\err\InvalidTagNameException; |
12 | use pvc\html\text_node\TextElement; |
13 | use pvc\htmlbuilder\definitions\types\DefinitionType; |
14 | use pvc\interfaces\html\attribute\AttributeInterface; |
15 | use pvc\interfaces\html\attribute\AttributeWithValueInterface; |
16 | use pvc\interfaces\html\attribute\DataType; |
17 | use pvc\interfaces\html\attribute\EventInterface; |
18 | use pvc\interfaces\html\element\ElementInterface; |
19 | use pvc\interfaces\validator\ValTesterInterface; |
20 | |
21 | /** |
22 | * @phpstan-import-type ValueType from AttributeWithValueInterface |
23 | */ |
24 | class HtmlFactory |
25 | { |
26 | |
27 | public function __construct(protected ContainerInterface $container) {} |
28 | |
29 | /** |
30 | * makeElement |
31 | * @param string $elementName |
32 | * @return ElementInterface |
33 | * @throws InvalidTagNameException |
34 | * @throws ContainerExceptionInterface |
35 | * @throws NotFoundExceptionInterface |
36 | */ |
37 | public function makeElement(string $elementName): ElementInterface |
38 | { |
39 | $defId = NameResolver::getDefIdFromName($elementName, DefinitionType::Element); |
40 | if (!$this->container->has($defId)) { |
41 | throw new InvalidTagNameException($elementName); |
42 | } |
43 | $element = $this->container->get($defId); |
44 | assert($element instanceof ElementInterface); |
45 | return $element; |
46 | } |
47 | |
48 | /** |
49 | * @param string $attributeName |
50 | * |
51 | * @return AttributeInterface |
52 | * @throws ContainerExceptionInterface |
53 | * @throws InvalidAttributeException |
54 | * @throws NotFoundExceptionInterface |
55 | */ |
56 | public function makeAttribute(string $attributeName): AttributeInterface |
57 | { |
58 | $defId = NameResolver::getDefIdFromName($attributeName, DefinitionType::Attribute); |
59 | if (!$this->container->has($defId)) { |
60 | throw new InvalidAttributeException($attributeName); |
61 | } |
62 | $attribute = $this->container->get($defId); |
63 | assert($attribute instanceof AttributeInterface); |
64 | return $attribute; |
65 | } |
66 | |
67 | |
68 | /** |
69 | * makeEvent |
70 | * @param string $eventName |
71 | * @return EventInterface |
72 | * @throws InvalidEventNameException |
73 | * @throws ContainerExceptionInterface |
74 | * @throws NotFoundExceptionInterface |
75 | */ |
76 | public function makeEvent(string $eventName): EventInterface |
77 | { |
78 | $defId = NameResolver::getDefIdFromName($eventName, DefinitionType::Event); |
79 | if (!$this->container->has($defId)) { |
80 | throw new InvalidEventNameException($eventName); |
81 | } |
82 | $event = $this->container->get($defId); |
83 | assert($event instanceof EventInterface); |
84 | return $event; |
85 | } |
86 | |
87 | /** |
88 | * @param string $name (should already be prefixed with 'data-') |
89 | * @param DataType $dataType |
90 | * @param bool $caseSensitive |
91 | * @param ValTesterInterface<ValueType>|null $valTester |
92 | * @return AttributeCustomData |
93 | */ |
94 | public function makeCustomData( |
95 | string $name, |
96 | DataType $dataType, |
97 | ?bool $caseSensitive = null, |
98 | ?ValTesterInterface $valTester = null, |
99 | ): AttributeCustomData { |
100 | |
101 | return new AttributeCustomData( |
102 | $name, |
103 | $dataType, |
104 | $caseSensitive, |
105 | $valTester, |
106 | ); |
107 | } |
108 | |
109 | /** |
110 | * @return TextElement |
111 | */ |
112 | public function makeTextNode(): TextElement |
113 | { |
114 | $node = $this->container->get(TextElement::class); |
115 | assert( $node instanceof TextElement); |
116 | return $node; |
117 | } |
118 | } |