Code Coverage |
||||||||||
Classes and Traits |
Functions and Methods |
Lines |
||||||||
Total | |
100.00% |
1 / 1 |
|
100.00% |
5 / 5 |
CRAP | |
100.00% |
28 / 28 |
DefaultDataModelValidator | |
100.00% |
1 / 1 |
|
100.00% |
5 / 5 |
10 | |
100.00% |
28 / 28 |
validate | |
100.00% |
1 / 1 |
1 | |
100.00% |
5 / 5 |
|||
error | |
100.00% |
1 / 1 |
1 | |
100.00% |
2 / 2 |
|||
validateTableNamesUnique | |
100.00% |
1 / 1 |
3 | |
100.00% |
8 / 8 |
|||
validateClassNamesUnique | |
100.00% |
1 / 1 |
3 | |
100.00% |
8 / 8 |
|||
checkDuplicate | |
100.00% |
1 / 1 |
2 | |
100.00% |
5 / 5 |
<?php | |
declare(strict_types = 1); | |
namespace Siesta\Validator; | |
use Siesta\Contract\DataModelValidator; | |
use Siesta\Model\DataModel; | |
use Siesta\Model\ValidationLogger; | |
use Siesta\Util\ArrayUtil; | |
/** | |
* @author Gregor Müller | |
*/ | |
class DefaultDataModelValidator implements DataModelValidator | |
{ | |
const ERROR_DUPLICATE_TABLE_NAME = "Table '%s' is used more than once."; | |
const ERROR_DUPLICATE_TABLE_NAME_CODE = 1000; | |
const ERROR_DUPLICATE_CLASS_NAME = "Class '%s' is used more than once."; | |
const ERROR_DUPLICATE_CLASS_NAME_CODE = 1001; | |
/** | |
* @var DataModel | |
*/ | |
protected $datamodel; | |
/** | |
* @var ValidationLogger | |
*/ | |
protected $logger; | |
/** | |
* @param DataModel $dataModel | |
* @param ValidationLogger $logger | |
*/ | |
public function validate(DataModel $dataModel, ValidationLogger $logger) | |
{ | |
$this->datamodel = $dataModel; | |
$this->logger = $logger; | |
$this->validateTableNamesUnique(); | |
$this->validateClassNamesUnique(); | |
} | |
/** | |
* @param string $text | |
* @param int $code | |
*/ | |
protected function error(string $text, int $code) | |
{ | |
$this->logger->error($text, $code); | |
} | |
/** | |
* | |
*/ | |
protected function validateTableNamesUnique() | |
{ | |
$nameList = []; | |
$duplicateNameList = []; | |
foreach ($this->datamodel->getEntityList() as $entity) { | |
$this->checkDuplicate($nameList, $duplicateNameList, $entity->getTableName()); | |
} | |
foreach ($duplicateNameList as $duplicateName) { | |
$error = sprintf(self::ERROR_DUPLICATE_TABLE_NAME, $duplicateName); | |
$this->error($error, self::ERROR_DUPLICATE_TABLE_NAME_CODE); | |
} | |
} | |
/** | |
* | |
*/ | |
protected function validateClassNamesUnique() | |
{ | |
$nameList = []; | |
$duplicateNameList = []; | |
foreach ($this->datamodel->getEntityList() as $entity) { | |
$this->checkDuplicate($nameList, $duplicateNameList, $entity->getClassName()); | |
} | |
foreach ($duplicateNameList as $duplicateName) { | |
$error = sprintf(self::ERROR_DUPLICATE_CLASS_NAME, $duplicateName); | |
$this->error($error, self::ERROR_DUPLICATE_CLASS_NAME_CODE); | |
} | |
} | |
/** | |
* @param array $nameList | |
* @param array $duplicateNameList | |
* @param $name | |
*/ | |
protected function checkDuplicate(array &$nameList, array &$duplicateNameList, $name) | |
{ | |
$existing = ArrayUtil::getFromArray($nameList, $name); | |
if ($existing) { | |
$duplicateNameList[] = $name; | |
} | |
$nameList[$name] = true; | |
} | |
} |