|
| 1 | +<?php |
| 2 | + |
| 3 | +/** |
| 4 | + * @copyright Copyright (C) Ibexa AS. All rights reserved. |
| 5 | + * @license For full copyright and license information view LICENSE file distributed with this source code. |
| 6 | + */ |
| 7 | +declare(strict_types=1); |
| 8 | + |
| 9 | +namespace Ibexa\FieldTypeRichText\RichText\Validator; |
| 10 | + |
| 11 | +use DOMDocument; |
| 12 | +use DOMElement; |
| 13 | +use DOMNodeList; |
| 14 | +use DOMXPath; |
| 15 | +use Ibexa\Contracts\FieldTypeRichText\RichText\ValidatorInterface; |
| 16 | + |
| 17 | +/** |
| 18 | + * Validator for Custom Tags and Styles input. |
| 19 | + * |
| 20 | + * The Validator checks if the given XML reflects proper Custom Tags or Styles configuration, |
| 21 | + * mostly the existence of a specific Custom Tag or Style and their required attributes. |
| 22 | + */ |
| 23 | +class CustomTemplateValidator implements ValidatorInterface |
| 24 | +{ |
| 25 | + /** |
| 26 | + * Custom Tags global configuration (ibexa.richtext.custom_tags Semantic Config). |
| 27 | + * |
| 28 | + * @var array<string,array{template:string, is_inline:bool, icon:string, attributes:array<string, array{type: string, required: bool, default_value: mixed}>}> |
| 29 | + */ |
| 30 | + private array $customTagsConfiguration; |
| 31 | + |
| 32 | + /** |
| 33 | + * Custom Styles global configuration (ibexa.richtext.custom_styles Semantic Config). |
| 34 | + * |
| 35 | + * @var array<string,array{template:string,inline:bool}> |
| 36 | + */ |
| 37 | + private array $customStylesConfiguration; |
| 38 | + |
| 39 | + /** |
| 40 | + * CustomTemplateValidator constructor. |
| 41 | + * |
| 42 | + * @param array<string,array{template:string, is_inline:bool, icon:string, attributes:array<string, array{type: string, required: bool, default_value: mixed}>}> $customTagsConfiguration |
| 43 | + * @param array<string,array{template:string, inline:bool}> $customStylesConfiguration |
| 44 | + */ |
| 45 | + public function __construct( |
| 46 | + array $customTagsConfiguration, |
| 47 | + array $customStylesConfiguration |
| 48 | + ) { |
| 49 | + $this->customTagsConfiguration = $customTagsConfiguration; |
| 50 | + $this->customStylesConfiguration = $customStylesConfiguration; |
| 51 | + } |
| 52 | + |
| 53 | + /** |
| 54 | + * Validate Custom Tags found in the document. |
| 55 | + * |
| 56 | + * @return string[] an array of error messages |
| 57 | + */ |
| 58 | + public function validateDocument(DOMDocument $xmlDocument): array |
| 59 | + { |
| 60 | + $configuredTemplateNames = array_merge(array_keys($this->customTagsConfiguration), array_keys($this->customStylesConfiguration)); |
| 61 | + $errors = []; |
| 62 | + |
| 63 | + $xpath = new DOMXPath($xmlDocument); |
| 64 | + $xpath->registerNamespace('docbook', 'http://docbook.org/ns/docbook'); |
| 65 | + |
| 66 | + $eztemplateElements = $xpath->query('//docbook:eztemplate'); |
| 67 | + if ($eztemplateElements instanceof DOMNodeList) { |
| 68 | + foreach ($eztemplateElements as $tagElement) { |
| 69 | + if (!$tagElement instanceof DOMElement) { |
| 70 | + continue; |
| 71 | + } |
| 72 | + $tagName = $tagElement->getAttribute('name'); |
| 73 | + if (empty($tagName)) { |
| 74 | + $errors[] = 'Missing RichText Custom Tag name'; |
| 75 | + continue; |
| 76 | + } |
| 77 | + |
| 78 | + if (!in_array($tagName, $configuredTemplateNames, true)) { |
| 79 | + @trigger_error( |
| 80 | + "Configuration for RichText Custom Tag or Custom Style '{$tagName}' not found. " . |
| 81 | + 'Custom Tags and Custom Style configuration is required since 7.1, its lack will result in validation error in 8.x', |
| 82 | + E_USER_DEPRECATED |
| 83 | + ); |
| 84 | + continue; |
| 85 | + } |
| 86 | + |
| 87 | + // Custom Styles does not have any attributes, so we can skip validation for them |
| 88 | + if (isset($this->customStylesConfiguration[$tagName])) { |
| 89 | + continue; |
| 90 | + } |
| 91 | + |
| 92 | + $nonEmptyAttributes = []; |
| 93 | + $tagAttributes = $this->customTagsConfiguration[$tagName]['attributes']; |
| 94 | + |
| 95 | + // iterate over all attributes defined in XML document to check if their names match configuration |
| 96 | + $configElements = $xpath->query('./docbook:ezconfig/docbook:ezvalue', $tagElement); |
| 97 | + if ($configElements instanceof DOMNodeList) { |
| 98 | + foreach ($configElements as $configElement) { |
| 99 | + if (!$configElement instanceof DOMElement) { |
| 100 | + continue; |
| 101 | + } |
| 102 | + $attributeName = $configElement->getAttribute('key'); |
| 103 | + if (empty($attributeName)) { |
| 104 | + $errors[] = "Missing attribute name for RichText Custom Tag '{$tagName}'"; |
| 105 | + continue; |
| 106 | + } |
| 107 | + if (!isset($tagAttributes[$attributeName])) { |
| 108 | + $errors[] = "Unknown attribute '{$attributeName}' of RichText Custom Tag '{$tagName}'"; |
| 109 | + } |
| 110 | + |
| 111 | + // collect information about non-empty attributes |
| 112 | + if (!empty($configElement->textContent)) { |
| 113 | + $nonEmptyAttributes[] = $attributeName; |
| 114 | + } |
| 115 | + } |
| 116 | + } |
| 117 | + |
| 118 | + // check if all required attributes are present |
| 119 | + foreach ($tagAttributes as $attributeName => $attributeSettings) { |
| 120 | + if (empty($attributeSettings['required'])) { |
| 121 | + continue; |
| 122 | + } |
| 123 | + |
| 124 | + if (!in_array($attributeName, $nonEmptyAttributes)) { |
| 125 | + $errors[] = "The attribute '{$attributeName}' of RichText Custom Tag '{$tagName}' cannot be empty"; |
| 126 | + } |
| 127 | + } |
| 128 | + } |
| 129 | + } |
| 130 | + |
| 131 | + return $errors; |
| 132 | + } |
| 133 | +} |
| 134 | + |
| 135 | +class_alias(CustomTemplateValidator::class, 'EzSystems\EzPlatformRichText\eZ\RichText\Validator\CustomTagsValidator'); |
0 commit comments