wol-soft/php-json-schema-model-generator

View on GitHub
src/Model/Validator/PropertyTemplateValidator.php

Summary

Maintainability
A
35 mins
Test Coverage
A
100%
<?php

declare(strict_types = 1);

namespace PHPModelGenerator\Model\Validator;

use PHPMicroTemplate\Exception\PHPMicroTemplateException;
use PHPMicroTemplate\Render;
use PHPModelGenerator\Exception\RenderException;
use PHPModelGenerator\Model\Property\PropertyInterface;
use PHPModelGenerator\Model\Schema;

/**
 * Class PropertyTemplateValidator
 *
 * @package PHPModelGenerator\Model\Validator
 */
class PropertyTemplateValidator extends AbstractPropertyValidator
{
    /** @var string */
    protected $template;
    /** @var array */
    protected $templateValues;
    /** @var Schema|null */
    protected $scope = null;

    /** @var Render */
    static private $renderer;

    /**
     * PropertyTemplateValidator constructor.
     *
     * @param PropertyInterface $property
     * @param string $template
     * @param array $templateValues
     * @param string $exceptionClass
     * @param array $exceptionParams
     */
    public function __construct(
        PropertyInterface $property,
        string $template,
        array $templateValues,
        string $exceptionClass,
        array $exceptionParams = [],
    ) {
        $this->template = $template;
        $this->templateValues = $templateValues;

        parent::__construct($property, $exceptionClass, $exceptionParams);
    }

    public function setScope(Schema $schema): void
    {
        $this->scope = $schema;

        if (isset($this->templateValues['schema'])) {
            $this->templateValues['schema'] = $schema;
        }

        $this->templateValues['isBaseValidator'] = in_array($this, $schema->getBaseValidators());
    }

    /**
     * Get the source code for the check to perform
     *
     * @return string
     *
     * @throws RenderException
     */
    public function getCheck(): string
    {
        try {
            return $this->getRenderer()->renderTemplate(
                $this->template,
                // make sure the current bound property is available in the template
                $this->templateValues + ['property' => $this->property],
            );
        } catch (PHPMicroTemplateException $exception) {
            throw new RenderException("Can't render property validation template {$this->template}", 0, $exception);
        }
    }

    /**
     * @return Render
     */
    protected function getRenderer(): Render
    {
        if (!self::$renderer) {
            self::$renderer = new Render(
                join(DIRECTORY_SEPARATOR, [__DIR__, '..', '..', 'Templates']) . DIRECTORY_SEPARATOR,
            );
        }

        return self::$renderer;
    }
}