From a0b93d12ac539bb8f4034a4c8953d8777680060f Mon Sep 17 00:00:00 2001 From: Hadrien45 Date: Thu, 26 Jan 2017 17:16:13 +0100 Subject: [PATCH 1/2] Add Pomm Unique Object Validator --- composer.json | 3 +- .../Validator/Constraints/UniqueObject.php | 81 +++++++++++++++++++ .../Constraints/UniqueObjectValidator.php | 72 +++++++++++++++++ 3 files changed, 155 insertions(+), 1 deletion(-) create mode 100644 sources/lib/Validator/Constraints/UniqueObject.php create mode 100644 sources/lib/Validator/Constraints/UniqueObjectValidator.php diff --git a/composer.json b/composer.json index b0a55ac..7c1bb98 100644 --- a/composer.json +++ b/composer.json @@ -11,7 +11,8 @@ "symfony/http-foundation": "~2.5|~3.0", "symfony/http-kernel": "~2.5|~3.0", "symfony/serializer": "~2.5|~3.0", - "symfony/property-info": "~2.8|~3.0" + "symfony/property-info": "~2.8|~3.0", + "symfony/validator": "~2.8|~3.0" }, "suggest": { "pomm-project/model-manager": "~2.0", diff --git a/sources/lib/Validator/Constraints/UniqueObject.php b/sources/lib/Validator/Constraints/UniqueObject.php new file mode 100644 index 0000000..804940b --- /dev/null +++ b/sources/lib/Validator/Constraints/UniqueObject.php @@ -0,0 +1,81 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ +namespace PommProject\SymfonyBridge\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 }} 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 __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'); + } + + if(is_null($this->model) || trim($this->model) === ''){ + throw new ConstraintDefinitionException("Pomm model must be specified."); + } + + } + + /** + * {@inheritDoc} + */ + public function getRequiredOptions() + { + return array('fields','model'); + } + + /** + * {@inheritDoc} + */ + public function getTargets() + { + return self::CLASS_CONSTRAINT; + } + +} \ No newline at end of file diff --git a/sources/lib/Validator/Constraints/UniqueObjectValidator.php b/sources/lib/Validator/Constraints/UniqueObjectValidator.php new file mode 100644 index 0000000..0bc8997 --- /dev/null +++ b/sources/lib/Validator/Constraints/UniqueObjectValidator.php @@ -0,0 +1,72 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ +namespace PommProject\SymfonyBridge\Validator\Constraints; + +use Symfony\Component\Validator\Constraint; +use Symfony\Component\Validator\ConstraintValidator; +use PommProject\Foundation\Pomm; + +class UniqueObjectValidator extends ConstraintValidator +{ + /** + * @var \PommProject\Foundation\Pomm + */ + private $pomm; + + public function __construct(Pomm $pomm) + { + $this->pomm = $pomm; + } + + /** + * Camelize field and return object method name + * + * @param string $field + * @return string + */ + private function getMethodName($field) + { + $fm = ucfirst(str_replace('_', '', ucwords($field, '_'))); + + return 'get'.$fm; + } + + /** + * {@inheritdoc} + */ + public function validate($object, Constraint $constraint) + { + $fields = (array) $constraint->fields; + $model = $constraint->model; + $query_string = ''; + $params = []; + + foreach ($fields as $f){ + $method = $this->getMethodName($f); + $params[] = $object->$method(); + + if(end($fields) !== $f){ + $query_string.= $f.' = $* AND '; + } else { + $query_string.= $f.' = $* '; + } + } + $hasObject = $this->pomm->getDefaultSession()->getModel($model)->existWhere($query_string, $params); + + if($hasObject){ + $this->context->buildViolation($constraint->message) + ->atPath($constraint->errorPath) + ->setParameter('{{ model }}', $model) + ->setParameter('{{ fields }}', implode(',', $fields)) + ->addViolation(); + } + } + +} \ No newline at end of file From 43b26291da485fd75265c7e4e0a9eceb97a812e4 Mon Sep 17 00:00:00 2001 From: Hadrien45 Date: Tue, 28 Feb 2017 20:56:59 +0100 Subject: [PATCH 2/2] Condition builder --- .../Constraints/UniqueObjectValidator.php | 15 +++++---------- 1 file changed, 5 insertions(+), 10 deletions(-) diff --git a/sources/lib/Validator/Constraints/UniqueObjectValidator.php b/sources/lib/Validator/Constraints/UniqueObjectValidator.php index 0bc8997..7eb3de3 100644 --- a/sources/lib/Validator/Constraints/UniqueObjectValidator.php +++ b/sources/lib/Validator/Constraints/UniqueObjectValidator.php @@ -12,6 +12,7 @@ use Symfony\Component\Validator\Constraint; use Symfony\Component\Validator\ConstraintValidator; use PommProject\Foundation\Pomm; +use PommProject\Foundation\Where; class UniqueObjectValidator extends ConstraintValidator { @@ -45,20 +46,14 @@ public function validate($object, Constraint $constraint) { $fields = (array) $constraint->fields; $model = $constraint->model; - $query_string = ''; - $params = []; + $where = new Where(); foreach ($fields as $f){ $method = $this->getMethodName($f); - $params[] = $object->$method(); - - if(end($fields) !== $f){ - $query_string.= $f.' = $* AND '; - } else { - $query_string.= $f.' = $* '; - } + $where->andWhere($f.' = $*',[$object->$method()]); } - $hasObject = $this->pomm->getDefaultSession()->getModel($model)->existWhere($query_string, $params); + + $hasObject = $this->pomm->getDefaultSession()->getModel($model)->existWhere($where); if($hasObject){ $this->context->buildViolation($constraint->message)