Skip to content

Commit

Permalink
Merge pull request #15 from chernecov/identifier-and-validation-impro…
Browse files Browse the repository at this point in the history
…vements

[CrudManager] Identifier and validation improvements
  • Loading branch information
chernecov committed Aug 26, 2015
2 parents 6602f14 + 5ee7ead commit 0ce6929
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 23 deletions.
31 changes: 13 additions & 18 deletions Services/CRUD/CrudManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\Validator\ConstraintViolation;
use Symfony\Component\Validator\ConstraintViolationList;
use Symfony\Component\Validator\ConstraintViolationListInterface;
use Symfony\Component\Validator\Validator\RecursiveValidator;

/**
Expand Down Expand Up @@ -308,8 +307,8 @@ private function validateCollectionUniqueness(
foreach ($collection as $collectionItem) {
$count = 0;
foreach ($collection as $collectionItemToCompare) {
if ($collectionItemToCompare->getId()) {
if ($collectionItemToCompare->getId() == $collectionItem->getId()) {
if ($collectionItemToCompare->getPrimaryKey()) {
if ($collectionItemToCompare->getPrimaryKey() == $collectionItem->getPrimaryKey()) {
$count++;
}
} else {
Expand Down Expand Up @@ -415,20 +414,16 @@ public function setData(CrudEntityInterface $entity, array $data = array())
*/
public function save(CrudEntityInterface $entity)
{
if ($entity instanceof ValidatableInterface) {
$validation = $this->validate($entity);
if ($validation instanceof ConstraintViolationListInterface &&
$entity->getViolations() instanceof ConstraintViolationListInterface
) {
$entity->getViolations()->addAll($validation);
} else if ($validation instanceof ConstraintViolationListInterface) {
$entity->setViolations($validation);
}
if ($entity->getViolations()->count()) {
throw new ValidationFailedException($entity->getViolations());
$violations = $this->validate($entity);
if ($violations instanceof ConstraintViolationList) {
if ($entity instanceof ValidatableInterface) {
$entity->setViolations($violations);
$entity->setValid(false);
}
throw new ValidationFailedException($violations);
}
$this->flush();

$this->flush($entity);
}

/**
Expand All @@ -451,7 +446,7 @@ public function validateExistence(CrudEntityInterface $entity)
),
$entity,
'id',
$entity->getId(),
$entity->getPrimaryKey(),
null,
404
);
Expand All @@ -472,8 +467,8 @@ public function filterCollection(ArrayCollection $collection, $replace = true)
{
$unitOfWork = new CrudUnitOfWork();
foreach ($collection as $entity) {
if ($entity->getId()) {
$crudEntity = $this->find(get_class($entity), $entity->getId());
if ($entity->getPrimaryKey()) {
$crudEntity = $this->find(get_class($entity), $entity->getPrimaryKey());
} else {
$this->crudTransformer->initializeClassMetadata(get_class($entity));
$conditions = $this->crudTransformer->getUniqueSearchConditions($entity);
Expand Down
6 changes: 3 additions & 3 deletions Services/CRUD/CrudTransformer.php
Original file line number Diff line number Diff line change
Expand Up @@ -296,11 +296,11 @@ private function processArrayValue(array $value, $targetClass)
'json'
);

if ($deserializedValue->getId()) {
$value = $this->entityManager->find($targetClass, $deserializedValue->getId());
if ($deserializedValue->getPrimaryKey()) {
$value = $this->entityManager->find($targetClass, $deserializedValue->getPrimaryKey());
}

if (!$value || !$deserializedValue->getId()) {
if (!$value || !$deserializedValue->getPrimaryKey()) {
$value = $deserializedValue;
}
return $value;
Expand Down
4 changes: 2 additions & 2 deletions Services/RequestCreator.php
Original file line number Diff line number Diff line change
Expand Up @@ -199,10 +199,10 @@ public function refresh(&$data)
{
if ($data instanceof ArrayCollection) {
foreach ($data as $item) {
$this->crudManager->refresh($item, $item->getId());
$this->crudManager->refresh($item, $item->getPrimaryKey());
}
} else {
$this->crudManager->refresh($data, $data->getId());
$this->crudManager->refresh($data, $data->getPrimaryKey());
}
}

Expand Down

0 comments on commit 0ce6929

Please sign in to comment.