Created
October 2, 2012 07:33
-
-
Save leevigraham/3817066 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 NSM\UserBundle\Form\DataTransformer; | |
use NSM\UserBundle\Entity\Invitation; | |
use Doctrine\ORM\EntityManager; | |
use Symfony\Component\Form\DataTransformerInterface; | |
use Symfony\Component\Form\Exception\UnexpectedTypeException; | |
/** | |
* Transforms an Invitation to an invitation code. | |
*/ | |
class InvitationToCodeTransformer implements DataTransformerInterface | |
{ | |
protected $entityManager; | |
public function __construct(EntityManager $entityManager) | |
{ | |
$this->entityManager = $entityManager; | |
} | |
public function transform($value) | |
{ | |
// Generally I wouldn't have to check for false | |
// But false is set in the reverse transform | |
if (null === $value || false === $value) { | |
return null; | |
} | |
if (!$value instanceof Invitation) { | |
throw new UnexpectedTypeException($value, 'NSM\UserBundle\Entity\Invitation'); | |
} | |
return $value->getCode(); | |
} | |
public function reverseTransform($value) | |
{ | |
if (null === $value || '' === $value) { | |
return null; | |
} | |
if (!is_string($value)) { | |
throw new UnexpectedTypeException($value, 'string'); | |
} | |
// Try and grab the invite | |
$invite = $this->entityManager | |
->getRepository('NSM\UserBundle\Entity\Invitation') | |
->findOneBy(array( | |
'code' => $value, | |
'user' => null, | |
) | |
); | |
// return false if there is no entity otherwise return the entity | |
return (null === $invite) ? false : $invite; | |
} | |
} |
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 NSM\UserBundle\Validator\Constraints; | |
use Symfony\Component\Validator\Constraint; | |
use Symfony\Component\Validator\ConstraintValidator; | |
class InviteValidator extends ConstraintValidator | |
{ | |
public function validate($value, Constraint $constraint) | |
{ | |
if(false === $value) { | |
$this->context->addViolation($constraint->notFoundMessage, array('%string%' => $value)); | |
return false; | |
} | |
// Check expiration | |
// Check assignment to email | |
return true; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment