for testing and deploying your application
for finding and fixing issues
for empowering human code reviews
<?php
/*
* This file is part of the CRUDlex package.
*
* (c) Philip Lehmann-Böhm <[email protected]>
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace CRUDlex;
use \Valdi\Validator\ValidatorInterface;
/**
* A validator to check for an unique field.
class UniqueValidator implements ValidatorInterface {
protected function isValidUniqueMany($value, $data, $entity, $field) {
$value
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.
$data
$entity
$field
return true;
}
* {@inheritdoc}
public function isValid($value, array $parameters) {
if (in_array($value, [null, ''])) {
$data = $parameters[0];
$entity = $parameters[1];
$field = $parameters[2];
$type = $data->getDefinition()->getType($field);
if ($type === 'many') {
return $this->isValidUniqueMany($value, $data, $entity, $field);
$params = [$field => $value];
$paramsOperators = [$field => '='];
if ($entity->get('id') !== null) {
$params['id'] = $entity->get('id');
$paramsOperators['id'] = '!=';
$amount = intval($data->countBy($data->getDefinition()->getTable(), $params, $paramsOperators, true));
return $amount == 0;
public function getInvalidDetails() {
return 'unique';
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.