Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 31
0.00% covered (danger)
0.00%
0 / 2
CRAP
0.00% covered (danger)
0.00%
0 / 1
ButtonRenderer
0.00% covered (danger)
0.00%
0 / 31
0.00% covered (danger)
0.00%
0 / 2
30
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 / 29
0.00% covered (danger)
0.00%
0 / 1
20
1<?php
2declare( strict_types = 1 );
3
4/**
5 * ButtonRenderer.php
6 *
7 * This file is part of the Codex PHP library, which provides a PHP-based interface for creating
8 * UI components consistent with the Codex design system.
9 *
10 * The `ButtonRenderer` class leverages the `TemplateParser` and `Sanitizer` utilities to ensure the
11 * component object is rendered according to Codex design system standards.
12 *
13 * @category Renderer
14 * @package  Codex\Renderer
15 * @since    0.1.0
16 * @author   Doğu Abaris <abaris@null.net>
17 * @license  https://www.gnu.org/copyleft/gpl.html GPL-2.0-or-later
18 * @link     https://doc.wikimedia.org/codex/main/ Codex Documentation
19 */
20
21namespace Wikimedia\Codex\Renderer;
22
23use InvalidArgumentException;
24use Wikimedia\Codex\Component\Button;
25use Wikimedia\Codex\Contract\Component;
26use Wikimedia\Codex\Contract\Renderer;
27use Wikimedia\Codex\Parser\TemplateParser;
28use Wikimedia\Codex\Utility\Sanitizer;
29
30/**
31 * ButtonRenderer is responsible for rendering the HTML markup
32 * for a Button component using a Mustache template.
33 *
34 * This class uses the `TemplateParser` and `Sanitizer` utilities to manage
35 * the template rendering process, ensuring that the component object's HTML
36 * output adheres to the Codex design system's standards.
37 *
38 * @category Renderer
39 * @package  Codex\Renderer
40 * @since    0.1.0
41 * @author   Doğu Abaris <abaris@null.net>
42 * @license  https://www.gnu.org/copyleft/gpl.html GPL-2.0-or-later
43 * @link     https://doc.wikimedia.org/codex/main/ Codex Documentation
44 */
45class ButtonRenderer extends Renderer {
46
47    /**
48     * The template parser instance.
49     */
50    private TemplateParser $templateParser;
51
52    /**
53     * Constructor to initialize the ButtonRenderer with a sanitizer and a template parser.
54     *
55     * @since 0.1.0
56     * @param Sanitizer $sanitizer The sanitizer instance used for content sanitization.
57     * @param TemplateParser $templateParser The template parser instance used for rendering templates.
58     */
59    public function __construct( Sanitizer $sanitizer, TemplateParser $templateParser ) {
60        parent::__construct( $sanitizer );
61        $this->templateParser = $templateParser;
62    }
63
64    /**
65     * Renders the HTML for a button component.
66     *
67     * Uses the provided Button component to generate HTML markup adhering to the Codex design system.
68     *
69     * @since 0.1.0
70     * @param Component $component The Button object to render.
71     * @return string The rendered HTML string for the component.
72     */
73    public function render( Component $component ): string {
74        if ( !$component instanceof Button ) {
75            throw new InvalidArgumentException(
76                "Expected instance of Button, got " . get_class( $component )
77            );
78        }
79        $isLink = $component->getHref() !== null;
80        $buttonData = [
81            'id' => $component->getId(),
82            'label-html' => $this->sanitizer->sanitizeText( $component->getLabel() ),
83            'action' => $component->getAction(),
84            'defaultAction' => $component->getAction() === 'default',
85            'weight' => $component->getWeight(),
86            'type' => $component->getType(),
87            'defaultWeight' => $component->getWeight() === 'normal',
88            'size' => $component->getSize(),
89            'defaultSize' => $component->getSize() === 'medium',
90            'iconClass' => $component->getIconClass(),
91            'isDisabled' => $component->isDisabled(),
92            'iconOnly' => $component->isIconOnly(),
93            'isLink' => $isLink,
94            'href' => $component->getHref(),
95            'fakeButtonClass' => $isLink ? (
96                $component->isDisabled()
97                    ? 'cdx-button--fake-button--disabled'
98                    : 'cdx-button--fake-button--enabled'
99            ) : '',
100            'extraClasses' => $this->getExtraClasses( $component->getAttributes() ),
101            'attributes' => $this->getOtherAttributes( $component->getAttributes() ),
102        ];
103
104        return $this->templateParser->processTemplate( 'button', $buttonData );
105    }
106}