Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 36
0.00% covered (danger)
0.00%
0 / 2
CRAP
0.00% covered (danger)
0.00%
0 / 1
ToggleSwitchRenderer
0.00% covered (danger)
0.00%
0 / 36
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 / 34
0.00% covered (danger)
0.00%
0 / 1
6
1<?php
2/**
3 * ToggleSwitchRenderer.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 `ToggleSwitchRenderer` 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\ToggleSwitch;
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 * ToggleSwitchRenderer is responsible for rendering the HTML markup
30 * for a ToggleSwitch 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 ToggleSwitchRenderer implements IRenderer {
44
45    use AttributeResolver;
46
47    /**
48     * The sanitizer instance used for content sanitization.
49     */
50    private Sanitizer $sanitizer;
51
52    /**
53     * The template renderer instance.
54     */
55    private ITemplateRenderer $templateRenderer;
56
57    /**
58     * Constructor to initialize the ToggleSwitchRenderer with a sanitizer and a template renderer.
59     *
60     * @since 0.1.0
61     * @param Sanitizer $sanitizer The sanitizer instance used for content sanitization.
62     * @param ITemplateRenderer $templateRenderer The template renderer instance.
63     */
64    public function __construct( Sanitizer $sanitizer, ITemplateRenderer $templateRenderer ) {
65        $this->sanitizer = $sanitizer;
66        $this->templateRenderer = $templateRenderer;
67    }
68
69    /**
70     * Renders the HTML for a ToggleSwitch component.
71     *
72     * @since 0.1.0
73     * @param ToggleSwitch $component The ToggleSwitch component to render.
74     * @return string The rendered HTML string for the component.
75     */
76    public function render( $component ): string {
77        if ( !$component instanceof ToggleSwitch ) {
78            throw new InvalidArgumentException( "Expected instance of ToggleSwitch, got " . get_class( $component ) );
79        }
80
81        $label = $component->getLabel();
82
83        $labelData = [
84            'id' => $this->sanitizer->sanitizeText( $label->getId() ),
85            'coreClass' => 'cdx-toggle-switch__label',
86            'labelText' => $this->sanitizer->sanitizeText( $label->getLabelText() ),
87            'optionalFlag' => $label->isOptional(),
88            'inputId' => $component->getInputId(),
89            'description' => $this->sanitizer->sanitizeText( $label->getDescription() ),
90            'descriptionId' => $this->sanitizer->sanitizeText( $label->getDescriptionId() ?? '' ),
91            'disabled' => $label->isDisabled(),
92            'iconClass' => $this->sanitizer->sanitizeText( $label->getIconClass() ?? '' ),
93            'attributes' => $this->resolve(
94                $this->sanitizer->sanitizeAttributes( $label->getAttributes() )
95            ),
96        ];
97
98        $toggleData = [
99            'id' => $this->sanitizer->sanitizeText( $component->getInputId() ),
100            'name' => $this->sanitizer->sanitizeText( $component->getName() ),
101            'value' => $this->sanitizer->sanitizeText( $component->getValue() ),
102            'inputId' => $component->getInputId(),
103            'isChecked' => $component->isChecked(),
104            'isDisabled' => $component->isDisabled(),
105            'ariaDescribedby' => $this->sanitizer->sanitizeText( $label->getDescriptionId() ?? '' ),
106            'inputAttributes' => $this->resolve(
107                $this->sanitizer->sanitizeAttributes( $component->getInputAttributes() )
108            ),
109            'wrapperAttributes' => $this->resolve(
110                $this->sanitizer->sanitizeAttributes( $component->getWrapperAttributes() )
111            ),
112            'label' => $labelData,
113        ];
114
115        return $this->templateRenderer->render( 'toggle-switch.mustache', $toggleData );
116    }
117}