Last active
October 1, 2017 13:46
-
-
Save Hadrien45/154f981ba3daf8149435aaebcb48d9c4 to your computer and use it in GitHub Desktop.
Pomm Unique Object
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//AppBundle\Validator\Constraints\UniqueObject | |
<?php | |
namespace AppBundle\Validator\Constraints; | |
use Symfony\Component\Validator\Constraint; | |
use Symfony\Component\Validator\Exception\UnexpectedTypeException; | |
use Symfony\Component\Validator\Exception\ConstraintDefinitionException; | |
/** | |
* @Annotation | |
*/ | |
class UniqueObject extends Constraint | |
{ | |
/** | |
* @var string Default message | |
*/ | |
public $message = 'A {{ model_class }} object already exists with fields : ({{ fields }})'; | |
/** | |
* @var array List of fields | |
*/ | |
public $fields = array(); | |
/** | |
* @var string FQN of Pomm Model | |
*/ | |
public $model = null; | |
/** | |
* @var string Used to set the path where the error will be attached, default is global. | |
*/ | |
public $errorPath; | |
public function validatedBy() | |
{ | |
return 'validator.pomm.unique_object'; | |
} | |
public function __construct($options = null) | |
{ | |
parent::__construct($options); | |
if (!is_array($this->fields) && !is_string($this->fields)) { | |
throw new UnexpectedTypeException($this->fields, 'array'); | |
} | |
if (0 === count($this->fields)) { | |
throw new ConstraintDefinitionException("At least one field must be specified."); | |
} | |
if (null !== $this->errorPath && !is_string($this->errorPath)) { | |
throw new UnexpectedTypeException($this->errorPath, 'string or null'); | |
} | |
} | |
/** | |
* {@inheritDoc} | |
*/ | |
public function getRequiredOptions() | |
{ | |
return array('fields'); | |
} | |
/** | |
* {@inheritDoc} | |
*/ | |
public function getTargets() | |
{ | |
return self::CLASS_CONSTRAINT; | |
} | |
} | |
// AppBundle\Validator\Constraints\UniqueObjectValidator | |
<?php | |
namespace AppBundle\Validator\Constraints; | |
use Symfony\Component\Validator\Constraint; | |
use Symfony\Component\Validator\ConstraintValidator; | |
use PommProject\Foundation\Pomm; | |
class UniqueObjectValidator extends ConstraintValidator | |
{ | |
private $pomm; | |
public function __construct(Pomm $pomm) | |
{ | |
$this->pomm = $pomm; | |
} | |
/** | |
* {@inheritdoc} | |
*/ | |
public function validate($object, Constraint $constraint) | |
{ | |
$fields = (array) $constraint->fields; | |
$modelClass = $constraint->model ? $constraint->model : get_class($object); | |
$query_string = ''; | |
$params = []; | |
foreach ($fields as $f){ | |
// Camelize | |
$fm = ucfirst(str_replace('_', '', ucwords($f, '_'))); | |
$method = 'get'.$fm; | |
$params[] = $object->$method(); | |
if(end($fields) !== $f){ | |
$query_string.= $f.' = $* AND '; | |
} else { | |
$query_string.= $f.' = $* '; | |
} | |
} | |
$bln = $this->pomm->getDefaultSession()->getModel($modelClass)->existWhere($query_string, $params); | |
if($bln){ | |
$this->context->buildViolation($constraint->message) | |
->atPath($constraint->errorPath) | |
->setParameter('{{ model_class }}', $modelClass) | |
->setParameter('{{ fields }}', implode(',', $fields)) | |
->addViolation(); | |
} | |
} | |
} | |
//services.yml | |
services: | |
validator.pomm.unique_object: | |
class: AppBundle\Validator\Constraints\UniqueObjectValidator | |
arguments: ["@pomm"] | |
tags: | |
- { name: validator.constraint_validator, alias: validator.pomm.unique_object} | |
// Example | |
new \AppBundle\Validator\Constraint\UniqueObject([ | |
"fields" => ["field1","field2",...], | |
"model" => "MyFully\Qualified\Name") //optionnal | |
]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment