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