Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
0.00% |
0 / 29 |
|
0.00% |
0 / 2 |
CRAP | |
0.00% |
0 / 1 |
FieldRenderer | |
0.00% |
0 / 29 |
|
0.00% |
0 / 2 |
12 | |
0.00% |
0 / 1 |
__construct | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
2 | |||
render | |
0.00% |
0 / 26 |
|
0.00% |
0 / 1 |
6 |
1 | <?php |
2 | /** |
3 | * FieldRenderer.php |
4 | * |
5 | * This file is part of the Codex PHP library, which provides a PHP-based interface for creating |
6 | * UI components consistent with the Codex design system. |
7 | * |
8 | * The `FieldRenderer` class leverages the `TemplateParser` and `Sanitizer` utilities to ensure the |
9 | * component object is rendered according to Codex design system standards. |
10 | * |
11 | * @category Renderer |
12 | * @package Codex\Renderer |
13 | * @since 0.1.0 |
14 | * @author Doğu Abaris <abaris@null.net> |
15 | * @license https://www.gnu.org/copyleft/gpl.html GPL-2.0-or-later |
16 | * @link https://doc.wikimedia.org/codex/main/ Codex Documentation |
17 | */ |
18 | |
19 | namespace Wikimedia\Codex\Renderer; |
20 | |
21 | use InvalidArgumentException; |
22 | use Wikimedia\Codex\Component\Field; |
23 | use Wikimedia\Codex\Contract\Renderer\IRenderer; |
24 | use Wikimedia\Codex\Parser\TemplateParser; |
25 | use Wikimedia\Codex\Traits\AttributeResolver; |
26 | use Wikimedia\Codex\Utility\Codex; |
27 | use Wikimedia\Codex\Utility\Sanitizer; |
28 | |
29 | /** |
30 | * FieldRenderer is responsible for rendering the HTML markup |
31 | * for a Field component using a Mustache template. |
32 | * |
33 | * This class uses the `TemplateParser` and `Sanitizer` utilities to manage |
34 | * the template rendering process, ensuring that the component object's HTML |
35 | * output adheres to the Codex design system's standards. |
36 | * |
37 | * @category Renderer |
38 | * @package Codex\Renderer |
39 | * @since 0.1.0 |
40 | * @author Doğu Abaris <abaris@null.net> |
41 | * @license https://www.gnu.org/copyleft/gpl.html GPL-2.0-or-later |
42 | * @link https://doc.wikimedia.org/codex/main/ Codex Documentation |
43 | */ |
44 | class FieldRenderer implements IRenderer { |
45 | /** |
46 | * Use the AttributeResolver trait |
47 | */ |
48 | use AttributeResolver; |
49 | |
50 | /** |
51 | * The sanitizer instance used for content sanitization. |
52 | */ |
53 | private Sanitizer $sanitizer; |
54 | |
55 | /** |
56 | * The template parser instance. |
57 | */ |
58 | private TemplateParser $templateParser; |
59 | |
60 | /** |
61 | * The codex instance. |
62 | */ |
63 | private Codex $codex; |
64 | |
65 | /** |
66 | * Constructor to initialize the FieldRenderer with a sanitizer and a template parser. |
67 | * |
68 | * @since 0.1.0 |
69 | * @param Sanitizer $sanitizer The sanitizer instance used for content sanitization. |
70 | * @param TemplateParser $templateParser The template parser instance. |
71 | */ |
72 | public function __construct( Sanitizer $sanitizer, TemplateParser $templateParser ) { |
73 | $this->sanitizer = $sanitizer; |
74 | $this->templateParser = $templateParser; |
75 | $this->codex = new Codex(); |
76 | } |
77 | |
78 | /** |
79 | * Renders the HTML for a field component. |
80 | * |
81 | * Uses the provided Field component to generate HTML markup adhering to the Codex design system. |
82 | * |
83 | * @since 0.1.0 |
84 | * @param Field $component The Field component to render. |
85 | * @return string The rendered HTML string for the component. |
86 | */ |
87 | public function render( $component ): string { |
88 | if ( !$component instanceof Field ) { |
89 | throw new InvalidArgumentException( "Expected instance of Field, got " . get_class( $component ) ); |
90 | } |
91 | |
92 | $label = $component->getLabel(); |
93 | |
94 | $labelData = [ |
95 | 'id' => $this->sanitizer->sanitizeText( $label->getId() ), |
96 | 'isLegend' => $component->isFieldset(), |
97 | 'inputId' => $this->sanitizer->sanitizeText( $label->getInputId() ), |
98 | 'labelText' => $this->sanitizer->sanitizeText( $label->getLabelText() ), |
99 | 'optionalFlag' => $label->isOptional(), |
100 | 'description' => $this->sanitizer->sanitizeText( $label->getDescription() ), |
101 | 'descriptionId' => $this->sanitizer->sanitizeText( $label->getDescriptionId() ), |
102 | 'icon' => $this->sanitizer->sanitizeText( $label->getIconClass() ), |
103 | 'isVisuallyHidden' => $label->isVisuallyHidden(), |
104 | 'isDisabled' => $label->isDisabled(), |
105 | 'attributes' => |
106 | $this->resolve( $this->sanitizer->sanitizeAttributes( $label->getAttributes() ) |
107 | ), |
108 | ]; |
109 | |
110 | $fieldData = [ |
111 | 'id' => $this->sanitizer->sanitizeText( $component->getId() ), |
112 | 'isFieldset' => $component->isFieldset(), |
113 | 'fields' => $component->getFields(), |
114 | 'attributes' => $this->resolve( $this->sanitizer->sanitizeAttributes( $component->getAttributes() ) ), |
115 | 'label' => $labelData, |
116 | ]; |
117 | |
118 | return $this->templateParser->processTemplate( 'field', $fieldData ); |
119 | } |
120 | } |