Code Coverage
 
Classes and Traits
Functions and Methods
Lines
Total
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
8 / 8
CRAP
100.00% covered (success)
100.00%
40 / 40
DefaultCollectionManyValidator
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
8 / 8
13
100.00% covered (success)
100.00%
40 / 40
 validate
100.00% covered (success)
100.00%
1 / 1
1
100.00% covered (success)
100.00%
9 / 9
 getEntityName
100.00% covered (success)
100.00%
1 / 1
1
100.00% covered (success)
100.00%
1 / 1
 getCollectionManyName
100.00% covered (success)
100.00%
1 / 1
1
100.00% covered (success)
100.00%
1 / 1
 error
100.00% covered (success)
100.00%
1 / 1
1
100.00% covered (success)
100.00%
2 / 2
 validateCollectionName
100.00% covered (success)
100.00%
1 / 1
2
100.00% covered (success)
100.00%
6 / 6
 validateForeignEntity
100.00% covered (success)
100.00%
1 / 1
2
100.00% covered (success)
100.00%
6 / 6
 validateMappingEntity
100.00% covered (success)
100.00%
1 / 1
2
100.00% covered (success)
100.00%
6 / 6
 validateReferences
100.00% covered (success)
100.00%
1 / 1
3
100.00% covered (success)
100.00%
9 / 9
<?php
declare(strict_types = 1);
namespace Siesta\Validator;
use Siesta\Contract\CollectionManyValidator;
use Siesta\Model\CollectionMany;
use Siesta\Model\DataModel;
use Siesta\Model\Entity;
use Siesta\Model\ValidationLogger;
/**
 * @author Gregor Müller
 */
class DefaultCollectionManyValidator implements CollectionManyValidator
{
    const ERROR_INVALID_NAME = "Entity '%s' collectionMany with invalid name '%s' found.";
    const ERROR_INVALID_NAME_CODE = 1600;
    const ERROR_FOREIGN_TABLE = "Entiy '%s' collectionMany '%s' referes to unknonw table '%s";
    const ERROR_FOREIGN_TABLE_CODE = 1601;
    const ERROR_MAPPING_TABLE = "Entiy '%s' collectionMany '%s' referes to unknonw mapping table '%s";
    const ERROR_MAPPING_TABLE_CODE = 1602;
    const ERROR_FOREIGN_REFERENCE = "Entiy '%s' collectionMany '%s' mapping table '%s' has no reference to %s";
    const ERROR_FOREIGN_REFERENCE_CODE = 1603;
    const ERROR_MAPPING_REFERENCE = "Entiy '%s' collectionMany '%s' mapping table '%s' has no reference to %s";
    const ERROR_MAPPING_REFERENCE_CODE = 1604;
    /**
     * @var DataModel
     */
    protected $datamodel;
    /**
     * @var Entity
     */
    protected $entity;
    /**
     * @var CollectionMany
     */
    protected $collectionMany;
    /**
     * @var ValidationLogger
     */
    protected $logger;
    /**
     * @param DataModel $dataModel
     * @param Entity $entity
     * @param CollectionMany $collectionMany
     * @param ValidationLogger $logger
     */
    public function validate(DataModel $dataModel, Entity $entity, CollectionMany $collectionMany, ValidationLogger $logger)
    {
        $this->logger = $logger;
        $this->datamodel = $dataModel;
        $this->entity = $entity;
        $this->collectionMany = $collectionMany;
        $this->validateCollectionName();
        $this->validateForeignEntity();
        $this->validateMappingEntity();
        $this->validateReferences();
    }
    /**
     * @return string
     */
    protected function getEntityName()
    {
        return $this->entity->getClassShortName();
    }
    /**
     * @return string
     */
    protected function getCollectionManyName()
    {
        return $this->collectionMany->getName();
    }
    /**
     * @param string $text
     * @param int $code
     */
    protected function error(string $text, int $code)
    {
        $this->logger->error($text, $code);
    }
    /**
     *
     */
    protected function validateCollectionName()
    {
        $collectionName = $this->getCollectionManyName();
        if ($collectionName !== null) {
            return;
        }
        $error = sprintf(self::ERROR_INVALID_NAME, $this->getEntityName(), $collectionName);
        $this->error($error, self::ERROR_INVALID_NAME_CODE);
    }
    /**
     *
     */
    protected function validateForeignEntity()
    {
        $foreignEntity = $this->collectionMany->getForeignEntity();
        if ($foreignEntity !== null) {
            return;
        }
        $error = sprintf(self::ERROR_FOREIGN_TABLE, $this->getEntityName(), $this->getCollectionManyName(), $this->collectionMany->getForeignTable());
        $this->error($error, self::ERROR_FOREIGN_TABLE_CODE);
    }
    /**
     *
     */
    protected function validateMappingEntity()
    {
        $mappingEntity = $this->collectionMany->getMappingEntity();
        if ($mappingEntity !== null) {
            return;
        }
        $error = sprintf(self::ERROR_MAPPING_TABLE, $this->getEntityName(), $this->getCollectionManyName(), $this->collectionMany->getMappingTable());
        $this->error($error, self::ERROR_MAPPING_TABLE_CODE);
    }
    /**
     *
     */
    protected function validateReferences()
    {
        $foreignReference = $this->collectionMany->getForeignReference();
        if ($foreignReference === null) {
            $error = sprintf(self::ERROR_FOREIGN_REFERENCE, $this->getEntityName(), $this->getCollectionManyName(), $this->collectionMany->getMappingTable(), $this->collectionMany->getForeignTable());
            $this->error($error, self::ERROR_FOREIGN_REFERENCE_CODE);
        }
        $mappingReference = $this->collectionMany->getMappingReference();
        if ($mappingReference === null) {
            $error = sprintf(self::ERROR_MAPPING_REFERENCE, $this->getEntityName(), $this->getCollectionManyName(), $this->collectionMany->getMappingTable(), $this->getEntityName());
            $this->error($error, self::ERROR_MAPPING_REFERENCE_CODE);
        }
    }
}