Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 37
0.00% covered (danger)
0.00%
0 / 2
CRAP
0.00% covered (danger)
0.00%
0 / 1
CheckboxRenderer
0.00% covered (danger)
0.00%
0 / 37
0.00% covered (danger)
0.00%
0 / 2
12
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
2
 render
0.00% covered (danger)
0.00%
0 / 35
0.00% covered (danger)
0.00%
0 / 1
6
1<?php
2/**
3 * CheckboxRenderer.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 `CheckboxRenderer` class leverages the `TemplateRenderer` 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
19namespace Wikimedia\Codex\Renderer;
20
21use InvalidArgumentException;
22use Wikimedia\Codex\Component\Checkbox;
23use Wikimedia\Codex\Contract\Renderer\IRenderer;
24use Wikimedia\Codex\Contract\Renderer\ITemplateRenderer;
25use Wikimedia\Codex\Traits\AttributeResolver;
26use Wikimedia\Codex\Utility\Sanitizer;
27
28/**
29 * CheckboxRenderer is responsible for rendering the HTML markup
30 * for a Checkbox component using a Mustache template.
31 *
32 * This class uses the `TemplateRenderer` and `Sanitizer` utilities to manage
33 * the template rendering process, ensuring that the component object's HTML
34 * output adheres to the Codex design system's standards.
35 *
36 * @category Renderer
37 * @package  Codex\Renderer
38 * @since    0.1.0
39 * @author   Doğu Abaris <abaris@null.net>
40 * @license  https://www.gnu.org/copyleft/gpl.html GPL-2.0-or-later
41 * @link     https://doc.wikimedia.org/codex/main/ Codex Documentation
42 */
43class CheckboxRenderer implements IRenderer {
44
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 renderer instance.
57     */
58    private ITemplateRenderer $templateRenderer;
59
60    /**
61     * Constructor to initialize the CheckboxRenderer with a sanitizer and a template renderer.
62     *
63     * @since 0.1.0
64     * @param Sanitizer $sanitizer The sanitizer instance used for content sanitization.
65     * @param ITemplateRenderer $templateRenderer The template renderer instance.
66     */
67    public function __construct( Sanitizer $sanitizer, ITemplateRenderer $templateRenderer ) {
68        $this->sanitizer = $sanitizer;
69        $this->templateRenderer = $templateRenderer;
70    }
71
72    /**
73     * Renders the HTML for a checkbox component.
74     *
75     * Uses the provided Checkbox component to generate HTML markup adhering to the Codex design system.
76     *
77     * @since 0.1.0
78     * @param Checkbox $component The Checkbox component to render.
79     * @return string The rendered HTML string for the component.
80     */
81    public function render( $component ): string {
82        if ( !$component instanceof Checkbox ) {
83            throw new InvalidArgumentException( "Expected instance of Checkbox, got " . get_class( $component ) );
84        }
85
86        $label = $component->getLabel();
87
88        $labelData = [
89            'id' => $this->sanitizer->sanitizeText( $label->getId() ),
90            'coreClass' => 'cdx-checkbox__label',
91            'labelText' => $this->sanitizer->sanitizeText( $label->getLabelText() ),
92            'optionalFlag' => $label->isOptional(),
93            'inputId' => $component->getInputId(),
94            'description' => $this->sanitizer->sanitizeText( $label->getDescription() ),
95            'descriptionId' => $this->sanitizer->sanitizeText( $label->getDescriptionId() ?? '' ),
96            'disabled' => $label->isDisabled(),
97            'iconClass' => $this->sanitizer->sanitizeText( $label->getIconClass() ?? '' ),
98            'attributes' => $this->resolve(
99                $this->sanitizer->sanitizeAttributes( $label->getAttributes() )
100            ),
101        ];
102
103        $checkboxData = [
104            'id' => $this->sanitizer->sanitizeText( $component->getInputId() ),
105            'name' => $this->sanitizer->sanitizeText( $component->getName() ),
106            'value' => $this->sanitizer->sanitizeText( $component->getValue() ),
107            'inputId' => $component->getInputId(),
108            'isChecked' => $component->isChecked(),
109            'isDisabled' => $component->isDisabled(),
110            'isInline' => $component->isInline(),
111            'ariaDescribedby' => $this->sanitizer->sanitizeText( $label->getDescriptionId() ?? '' ),
112            'inputAttributes' => $this->resolve(
113                $this->sanitizer->sanitizeAttributes( $component->getInputAttributes() )
114            ),
115            'wrapperAttributes' => $this->resolve(
116                $this->sanitizer->sanitizeAttributes( $component->getWrapperAttributes() )
117            ),
118            'label' => $labelData,
119        ];
120
121        return $this->templateRenderer->render( 'checkbox.mustache', $checkboxData );
122    }
123}