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

View on GitHub
src/PropertyProcessor/ComposedValueProcessorFactory.php

Summary

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

declare(strict_types=1);

namespace PHPModelGenerator\PropertyProcessor;

use PHPModelGenerator\Exception\SchemaException;
use PHPModelGenerator\Model\Schema;
use PHPModelGenerator\PropertyProcessor\ComposedValue\AbstractComposedValueProcessor;
use PHPModelGenerator\SchemaProcessor\SchemaProcessor;

/**
 * Class ComposedValueProcessorFactory
 *
 * @package PHPModelGenerator\PropertyProcessor
 */
class ComposedValueProcessorFactory implements ProcessorFactoryInterface
{
    /** @var bool */
    private $rootLevelComposition;

    /**
     * ComposedValueProcessorFactory constructor.
     *
     * @param bool $rootLevelComposition is the composed value on object root level (true) or on property level (false)?
     */
    public function __construct(bool $rootLevelComposition)
    {
        $this->rootLevelComposition = $rootLevelComposition;
    }

    /**
     * @inheritdoc
     *
     * @throws SchemaException
     */
    public function getProcessor(
        $type,
        PropertyMetaDataCollection $propertyMetaDataCollection,
        SchemaProcessor $schemaProcessor,
        Schema $schema,
    ): PropertyProcessorInterface {
        $processor = '\\PHPModelGenerator\\PropertyProcessor\\ComposedValue\\' . ucfirst($type) . 'Processor';

        $params = [$propertyMetaDataCollection, $schemaProcessor, $schema];

        if (is_a($processor, AbstractComposedValueProcessor::class, true)) {
            $params[] = $this->rootLevelComposition;
        }

        return new $processor(...$params);
    }
}