Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 27
0.00% covered (danger)
0.00%
0 / 2
CRAP
0.00% covered (danger)
0.00%
0 / 1
CardRenderer
0.00% covered (danger)
0.00%
0 / 27
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 / 25
0.00% covered (danger)
0.00%
0 / 1
20
1<?php
2/**
3 * CardRenderer.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 `CardRenderer` 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
19namespace Wikimedia\Codex\Renderer;
20
21use InvalidArgumentException;
22use Wikimedia\Codex\Component\Card;
23use Wikimedia\Codex\Contract\Renderer\IRenderer;
24use Wikimedia\Codex\Parser\TemplateParser;
25use Wikimedia\Codex\Traits\AttributeResolver;
26use Wikimedia\Codex\Utility\Sanitizer;
27
28/**
29 * CardRenderer is responsible for rendering the HTML markup
30 * for a Card component using a Mustache template.
31 *
32 * This class uses the `TemplateParser` 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 CardRenderer 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 parser instance.
57     */
58    private TemplateParser $templateParser;
59
60    /**
61     * Constructor to initialize the CardRenderer with a sanitizer and a template parser.
62     *
63     * @since 0.1.0
64     * @param Sanitizer $sanitizer The sanitizer instance used for content sanitization.
65     * @param TemplateParser $templateParser The template parser instance.
66     */
67    public function __construct( Sanitizer $sanitizer, TemplateParser $templateParser ) {
68        $this->sanitizer = $sanitizer;
69        $this->templateParser = $templateParser;
70    }
71
72    /**
73     * Renders the HTML for a card component.
74     *
75     * Uses the provided Card component to generate HTML markup adhering to the Codex design system.
76     *
77     * @since 0.1.0
78     * @param Card $component The Card object to render.
79     * @return string The rendered HTML string for the component.
80     */
81    public function render( $component ): string {
82        if ( !$component instanceof Card ) {
83            throw new InvalidArgumentException( "Expected instance of Card, got " . get_class( $component ) );
84        }
85
86        $thumbnail = $component->getThumbnail();
87        $thumbnailData = null;
88        if ( $thumbnail ) {
89            $thumbnailData = [
90                'id' => $this->sanitizer->sanitizeText( $thumbnail->getId() ),
91                'coreClass' => 'cdx-card__thumbnail',
92                'backgroundImage' => $this->sanitizer->sanitizeText( $thumbnail->getBackgroundImage() ),
93                'useDefaultPlaceholder' => (bool)$component->getThumbnail(),
94                'placeholderClass' => $this->sanitizer->sanitizeText( $thumbnail->getPlaceholderClass() ),
95                'attributes' => $this->resolve( $this->sanitizer->sanitizeAttributes( $thumbnail->getAttributes() ) ),
96            ];
97        }
98
99        $cardData = [
100            'id' => $this->sanitizer->sanitizeText( $component->getId() ),
101            'tag' => $component->getUrl() !== '' ? 'a' : 'span',
102            'title' => $this->sanitizer->sanitizeText( $component->getTitle() ),
103            'description' => $this->sanitizer->sanitizeText( $component->getDescription() ),
104            'supportingText' => $this->sanitizer->sanitizeText( $component->getSupportingText() ),
105            'url' => $this->sanitizer->sanitizeUrl( $component->getUrl() ),
106            'iconClass' => $this->sanitizer->sanitizeText( $component->getIconClass() ),
107            'thumbnail' => $thumbnailData,
108            'attributes' => $this->resolve( $this->sanitizer->sanitizeAttributes( $component->getAttributes() ) ),
109        ];
110
111        return $this->templateParser->processTemplate( 'card', $cardData );
112    }
113}