Code Coverage
 
Classes and Traits
Functions and Methods
Lines
Total
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
7 / 7
CRAP
100.00% covered (success)
100.00%
30 / 30
DefaultCollectionValidator
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
7 / 7
10
100.00% covered (success)
100.00%
30 / 30
 validate
100.00% covered (success)
100.00%
1 / 1
1
100.00% covered (success)
100.00%
8 / 8
 getEntityName
100.00% covered (success)
100.00%
1 / 1
1
100.00% covered (success)
100.00%
1 / 1
 getCollectionName
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
 validateForeignReference
100.00% covered (success)
100.00%
1 / 1
2
100.00% covered (success)
100.00%
6 / 6
<?php
declare(strict_types = 1);
namespace Siesta\Validator;
use Siesta\Contract\CollectionValidator;
use Siesta\Model\Collection;
use Siesta\Model\DataModel;
use Siesta\Model\Entity;
use Siesta\Model\ValidationLogger;
/**
 * @author Gregor Müller
 */
class DefaultCollectionValidator implements CollectionValidator
{
    const ERROR_INVALID_NAME = "Entity '%s' collection with invalid name '%s' found.";
    const ERROR_INVALID_NAME_CODE = 1500;
    const ERROR_FOREIGN_TABLE = "Entiy '%s' collection '%s' referes to unknonw table '%s";
    const ERROR_FOREIGN_TABLE_CODE = 1501;
    const ERROR_FOREIGN_REFERENCE = "Entiy '%s' collection '%s' referes to unknonw reference '%s";
    const ERROR_FOREIGN_REFERENCE_CODE = 1502;
    /**
     * @var DataModel
     */
    protected $datamodel;
    /**
     * @var Entity
     */
    protected $entity;
    /**
     * @var Collection
     */
    protected $collection;
    /**
     * @var ValidationLogger
     */
    protected $logger;
    public function validate(DataModel $dataModel, Entity $entity, Collection $collection, ValidationLogger $logger)
    {
        $this->logger = $logger;
        $this->datamodel = $dataModel;
        $this->entity = $entity;
        $this->collection = $collection;
        $this->validateCollectionName();
        $this->validateForeignEntity();
        $this->validateForeignReference();
    }
    /**
     * @return string
     */
    protected function getEntityName()
    {
        return $this->entity->getClassShortName();
    }
    /**
     * @return string
     */
    protected function getCollectionName()
    {
        return $this->collection->getName();
    }
    /**
     * @param string $text
     * @param int $code
     */
    protected function error(string $text, int $code)
    {
        $this->logger->error($text, $code);
    }
    /**
     *
     */
    protected function validateCollectionName()
    {
        $collectionName = $this->getCollectionName();
        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->collection->getForeignEntity();
        if ($foreignEntity !== null) {
            return;
        }
        $error = sprintf(self::ERROR_FOREIGN_TABLE, $this->getEntityName(), $this->getCollectionName(), $this->collection->getForeignTable());
        $this->error($error, self::ERROR_FOREIGN_TABLE_CODE);
    }
    /**
     *
     */
    protected function validateForeignReference()
    {
        $foreignReference = $this->collection->getForeignReference();
        if ($foreignReference !== null) {
            return;
        }
        $error = sprintf(self::ERROR_FOREIGN_REFERENCE, $this->getEntityName(), $this->getCollectionName(), $this->collection->getForeignReferenceName());
        $this->error($error, self::ERROR_FOREIGN_REFERENCE_CODE);
    }
}