Created
April 26, 2012 17:32
-
-
Save frosas/2501178 to your computer and use it in GitHub Desktop.
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
<?php | |
namespace Redconvive\Bundle\Entity; | |
use Symfony\Component\Validator\Validator as SymfonyValidator; | |
use Doctrine\ORM\Event\OnFlushEventArgs; | |
use Doctrine\ORM\EntityManager; | |
class Validator | |
{ | |
private $validator; | |
function __construct(SymfonyValidator $validator) | |
{ | |
$this->validator = $validator; | |
} | |
function onFlush(OnFlushEventArgs $args) | |
{ | |
foreach ($this->getEntitiesToValidate($args->getEntityManager()) as $entity) { | |
$violations = $this->validator->validate($entity); | |
if (count($violations)) { | |
throw new \Exception("Invalid entity ($violations)"); | |
} | |
} | |
} | |
private function getEntitiesToValidate(EntityManager $entityManager) | |
{ | |
return array_merge( | |
$entityManager->getUnitOfWork()->getScheduledEntityInsertions(), | |
$entityManager->getUnitOfWork()->getScheduledEntityUpdates()); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment