Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
81 changes: 81 additions & 0 deletions sources/lib/Validator/Constraints/UniqueObject.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
<?php
/*
* This file is part of Pomm's SymfonyBidge package.
*
* (c) 2017 Grégoire HUBERT <[email protected]>
*
* 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;
}

}
72 changes: 72 additions & 0 deletions sources/lib/Validator/Constraints/UniqueObjectValidator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
<?php
/*
* This file is part of Pomm's SymfonyBidge package.
*
* (c) 2017 Grégoire HUBERT <[email protected]>
*
* 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 ';
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It’d be better to use the Where class to handle that.

} 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();
}
}

}