-
Notifications
You must be signed in to change notification settings - Fork 17
Add Pomm Unique Object Validator #29
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Hadrien45
wants to merge
2
commits into
pomm-project:master
Choose a base branch
from
Hadrien45:uniq
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
This file contains hidden or 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
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
72
sources/lib/Validator/Constraints/UniqueObjectValidator.php
This file contains hidden or 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
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 '; | ||
} 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(); | ||
} | ||
} | ||
|
||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.