/**
  * Transforms an object (State) to a string.
  *
  * @param State|null $state
  * @return string
  */
 public function transform($state)
 {
     if (null === $state) {
         return "";
     }
     $stateName = $state->getName();
     $country = $state->getCountry();
     $countryName = $country->getIso2();
     return sprintf("%s, %s", $stateName, $countryName);
 }
 private function checkAddressLocation(&$address, $form, &$em)
 {
     $form = $form->get('location');
     if (!$address->getCountry()) {
         $form->get('country')->addError(new FormError($this->get('translator')->trans('required.field')));
         return false;
     }
     $isPreferred = $em->getRepository('LoginCidadaoCoreBundle:Country')->isPreferred($address->getCountry());
     if (!$address->getState() && !$isPreferred) {
         $steppe = ucwords(strtolower(trim($form->get('statesteppe')->getData())));
         if ($steppe) {
             $repo = $em->getRepository('LoginCidadaoCoreBundle:State');
             $ent = $repo->findOneBy(array('name' => $steppe, 'country' => $address->getCountry()));
             if (!$ent) {
                 $ent = new State();
                 $ent->setName($steppe);
                 $ent->setCountry($address->getCountry());
                 $em->persist($ent);
             }
             $address->setState($ent);
         }
     }
     if (!$address->getState()) {
         $form->get('state')->addError(new FormError($this->get('translator')->trans('required.field')));
         return false;
     }
     if (!$address->getCity() && !$isPreferred) {
         $steppe = ucwords(strtolower(trim($form->get('citysteppe')->getData())));
         if ($address->getState()) {
             $state = $address->getState();
         } elseif (isset($ent)) {
             $state = $ent;
         } else {
             $state = null;
         }
         if ($state && $steppe) {
             $repo = $em->getRepository('LoginCidadaoCoreBundle:City');
             $ent = $repo->findOneBy(array('name' => $steppe, 'state' => $state));
             if (!$ent) {
                 $ent = new City();
                 $ent->setName($steppe);
                 $ent->setState($state);
                 $em->persist($ent);
             }
             $address->setCity($ent);
         }
     }
     if (!$address->getCity()) {
         $form->get('city')->addError(new FormError($this->get('translator')->trans('required.field')));
         return false;
     }
     return true;
 }
 private function registerTextualState(\Symfony\Component\Form\FormEvent $event)
 {
     $data = $event->getForm()->getData();
     if (!$data instanceof SelectData) {
         return;
     }
     $form = $event->getForm();
     if ($form->has('state_text')) {
         $stateTextInput = $form->get('state_text')->getData();
         $stateText = ucwords(strtolower(trim($stateTextInput)));
         if (!$stateText) {
             return;
         }
         if (!$data->getCountry()) {
             throw new LcValidationException('required.field.country');
         }
         $isPreferred = $this->em->getRepository('LoginCidadaoCoreBundle:Country')->isPreferred($data->getCountry());
         if ($isPreferred) {
             throw new LcValidationException('restrict.location.creation');
         }
         $repo = $this->em->getRepository('LoginCidadaoCoreBundle:State');
         $state = $repo->findOneBy(array('name' => $stateText, 'country' => $data->getCountry()));
         if (!$state) {
             $state = new State();
             $state->setName($stateText);
             $state->setCountry($data->getCountry());
             $this->em->persist($state);
             $this->em->flush($state);
         }
         $data->setState($state)->setCity(null);
     }
 }