Add a unique object constraint validator
This commit is contained in:
parent
b4724a766a
commit
06ef181823
70
Validator/Constraints/UniqueObject.php
Normal file
70
Validator/Constraints/UniqueObject.php
Normal file
|
@ -0,0 +1,70 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This file is part of the PropelBundle package.
|
||||||
|
* For the full copyright and license information, please view the LICENSE
|
||||||
|
* file that was distributed with this source code.
|
||||||
|
*
|
||||||
|
* @license MIT License
|
||||||
|
*/
|
||||||
|
|
||||||
|
namespace Propel\PropelBundle\Validator\Constraints;
|
||||||
|
|
||||||
|
use Symfony\Component\Validator\Constraint;
|
||||||
|
use Symfony\Component\Validator\ConstraintValidator;
|
||||||
|
use Symfony\Component\Validator\Exception\UnexpectedTypeException;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Constraint for the Unique Object validator
|
||||||
|
*
|
||||||
|
* @author Maxime AILLOUD <maxime.ailloud@gmail.com>
|
||||||
|
*/
|
||||||
|
class UniqueObject extends Constraint
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* @var array
|
||||||
|
*/
|
||||||
|
public $fields = array();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return array
|
||||||
|
*/
|
||||||
|
public function getRequiredOptions()
|
||||||
|
{
|
||||||
|
return array('fields');
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The validator must be defined as a service with this name.
|
||||||
|
*
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
public function validatedBy()
|
||||||
|
{
|
||||||
|
return get_class($this).'Validator';
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* {@inheritDoc}
|
||||||
|
*/
|
||||||
|
public function getTargets()
|
||||||
|
{
|
||||||
|
return self::CLASS_CONSTRAINT;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
public function getDefaultOption()
|
||||||
|
{
|
||||||
|
return 'fields';
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
public function getMessage()
|
||||||
|
{
|
||||||
|
return 'A ' . $this->groups[1] . ' object already exists';
|
||||||
|
}
|
||||||
|
}
|
77
Validator/Constraints/UniqueObjectValidator.php
Normal file
77
Validator/Constraints/UniqueObjectValidator.php
Normal file
|
@ -0,0 +1,77 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This file is part of the PropelBundle package.
|
||||||
|
* For the full copyright and license information, please view the LICENSE
|
||||||
|
* file that was distributed with this source code.
|
||||||
|
*
|
||||||
|
* @license MIT License
|
||||||
|
*/
|
||||||
|
|
||||||
|
namespace Propel\PropelBundle\Validator\Constraints;
|
||||||
|
|
||||||
|
use Symfony\Component\Validator\Constraint;
|
||||||
|
use Symfony\Component\Validator\Exception\UnexpectedTypeException;
|
||||||
|
use Symfony\Component\Validator\Exception\ConstraintDefinitionException;
|
||||||
|
use Symfony\Component\Validator\ConstraintValidator;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Unique Object Validator checks if one or a set of fields contain unique values.
|
||||||
|
*
|
||||||
|
* @author Maxime AILLOUD <maxime.ailloud@gmail.com>
|
||||||
|
*/
|
||||||
|
class UniqueObjectValidator extends ConstraintValidator
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* @param object $object
|
||||||
|
* @param \Symfony\Component\Validator\Constraint $constraint
|
||||||
|
* @return Boolean
|
||||||
|
*/
|
||||||
|
public function isValid($object, Constraint $constraint)
|
||||||
|
{
|
||||||
|
if (!is_array($constraint->fields) && !is_string($constraint->fields)) {
|
||||||
|
throw new UnexpectedTypeException($constraint->fields, 'array');
|
||||||
|
}
|
||||||
|
|
||||||
|
$fields = (array)$constraint->fields;
|
||||||
|
|
||||||
|
if (count($fields) == 0) {
|
||||||
|
throw new ConstraintDefinitionException("At least one field must be specified.");
|
||||||
|
}
|
||||||
|
|
||||||
|
$class = get_class($object);
|
||||||
|
$peerClass = $class . 'Peer';
|
||||||
|
$queryClass = $class . 'Query';
|
||||||
|
$classFields = $peerClass::getFieldNames(\BasePeer::TYPE_FIELDNAME);
|
||||||
|
|
||||||
|
foreach ($fields as $fieldName) {
|
||||||
|
if(!array_search($fieldName, $classFields)) {
|
||||||
|
throw new ConstraintDefinitionException('The field "' . $fieldName .'" doesn\'t exist in the "' . $class . '" class.');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
$bddUsersQuery = $queryClass::create();
|
||||||
|
foreach ($fields as $fieldName) {
|
||||||
|
$bddUsersQuery->filterBy($peerClass::translateFieldName($fieldName, \BasePeer::TYPE_FIELDNAME, \BasePeer::TYPE_PHPNAME), $object->getByName($fieldName, \BasePeer::TYPE_FIELDNAME));
|
||||||
|
}
|
||||||
|
$bddUsers = $bddUsersQuery->find();
|
||||||
|
|
||||||
|
$countUser = count($bddUsers);
|
||||||
|
|
||||||
|
if ($countUser > 1 || ($countUser === 1 && $object !== $bddUsers[0])) {
|
||||||
|
$constraintMessage = $constraint->getMessage();
|
||||||
|
$constraintMessage .= ' with';
|
||||||
|
|
||||||
|
foreach ($fields as $fieldName) {
|
||||||
|
$constraintMessage .= ' ' . $peerClass::translateFieldName($fieldName, \BasePeer::TYPE_FIELDNAME, \BasePeer::TYPE_PHPNAME) . ' "' . $object->getByName($fieldName, \BasePeer::TYPE_FIELDNAME) . '" and';
|
||||||
|
}
|
||||||
|
|
||||||
|
$constraintMessage = substr($constraintMessage, 0, -4) . '.';
|
||||||
|
$this->setMessage($constraintMessage);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in a new issue