|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types = 1); |
| 4 | + |
| 5 | +namespace PHPModelGenerator\Model\Validator; |
| 6 | + |
| 7 | +use PHPMicroTemplate\Exception\FileSystemException; |
| 8 | +use PHPMicroTemplate\Exception\SyntaxErrorException; |
| 9 | +use PHPMicroTemplate\Exception\UndefinedSymbolException; |
| 10 | +use PHPModelGenerator\Exception\SchemaException; |
| 11 | +use PHPModelGenerator\Model\Schema; |
| 12 | +use PHPModelGenerator\PropertyProcessor\PropertyCollectionProcessor; |
| 13 | +use PHPModelGenerator\PropertyProcessor\PropertyFactory; |
| 14 | +use PHPModelGenerator\PropertyProcessor\PropertyProcessorFactory; |
| 15 | +use PHPModelGenerator\SchemaProcessor\SchemaProcessor; |
| 16 | +use PHPModelGenerator\Utils\RenderHelper; |
| 17 | + |
| 18 | +/** |
| 19 | + * Class AdditionalPropertiesValidator |
| 20 | + * |
| 21 | + * @package PHPModelGenerator\Model\Validator |
| 22 | + */ |
| 23 | +class AdditionalPropertiesValidator extends AbstractComposedPropertyValidator |
| 24 | +{ |
| 25 | + /** |
| 26 | + * AdditionalPropertiesValidator constructor. |
| 27 | + * |
| 28 | + * @param SchemaProcessor $schemaProcessor |
| 29 | + * @param Schema $schema |
| 30 | + * @param array $propertyStructure |
| 31 | + * |
| 32 | + * @throws FileSystemException |
| 33 | + * @throws SchemaException |
| 34 | + * @throws SyntaxErrorException |
| 35 | + * @throws UndefinedSymbolException |
| 36 | + */ |
| 37 | + public function __construct( |
| 38 | + SchemaProcessor $schemaProcessor, |
| 39 | + Schema $schema, |
| 40 | + array $propertyStructure |
| 41 | + ) { |
| 42 | + $propertyFactory = new PropertyFactory(new PropertyProcessorFactory()); |
| 43 | + |
| 44 | + $validationProperty = $propertyFactory->create( |
| 45 | + new PropertyCollectionProcessor(), |
| 46 | + $schemaProcessor, |
| 47 | + $schema, |
| 48 | + 'additional property', |
| 49 | + $propertyStructure['additionalProperties'] |
| 50 | + ); |
| 51 | + |
| 52 | + parent::__construct( |
| 53 | + $this->getRenderer()->renderTemplate( |
| 54 | + DIRECTORY_SEPARATOR . 'Exception' . DIRECTORY_SEPARATOR . 'InvalidPropertiesException.phptpl', |
| 55 | + ['error' => 'Provided JSON contains invalid additional properties.'] |
| 56 | + ), |
| 57 | + DIRECTORY_SEPARATOR . 'Validator' . DIRECTORY_SEPARATOR . 'AdditionalProperties.phptpl', |
| 58 | + [ |
| 59 | + 'validationProperty' => $validationProperty, |
| 60 | + 'additionalProperties' => preg_replace( |
| 61 | + '(\d+\s=>)', |
| 62 | + '', |
| 63 | + var_export(array_keys($propertyStructure['properties'] ?? []), true) |
| 64 | + ), |
| 65 | + 'generatorConfiguration' => $schemaProcessor->getGeneratorConfiguration(), |
| 66 | + 'viewHelper' => new RenderHelper($schemaProcessor->getGeneratorConfiguration()), |
| 67 | + ] |
| 68 | + ); |
| 69 | + } |
| 70 | + |
| 71 | + /** |
| 72 | + * Initialize all variables which are required to execute a property names validator |
| 73 | + * |
| 74 | + * @return string |
| 75 | + */ |
| 76 | + public function getValidatorSetUp(): string |
| 77 | + { |
| 78 | + return '$invalidProperties = [];'; |
| 79 | + } |
| 80 | +} |
0 commit comments