/** * Transforms an object (City) to a string. * * @param City|null $city * @return string */ public function transform($city) { if (null === $city) { return ""; } $cityName = $city->getName(); $state = $city->getState(); $stateName = $state->getAcronym(); return sprintf("%s, %s", $cityName, $stateName); }
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 registerTextualCity(\Symfony\Component\Form\FormEvent $event) { $data = $event->getForm()->getData(); if (!$data instanceof SelectData) { return; } $form = $event->getForm(); if ($form->has('city_text')) { $cityTextInput = $form->get('city_text')->getData(); $cityText = ucwords(strtolower(trim($cityTextInput))); if (!$cityText) { return; } if (!$data->getState()) { throw new LcValidationException('required.field.state'); } $isPreferred = $this->em->getRepository('LoginCidadaoCoreBundle:Country')->isPreferred($data->getCountry()); if ($isPreferred) { throw new LcValidationException('restrict.location.creation'); } $repo = $this->em->getRepository('LoginCidadaoCoreBundle:City'); $city = $repo->findOneBy(array('name' => $cityText, 'state' => $data->getState())); if (!$city) { $city = new City(); $city->setName($cityText); $city->setState($data->getState()); $this->em->persist($city); } $data->setCity($city); } }