Esempio n. 1
0
 /**
  * Returns true if and only if $value meets the validation requirements
  *
  * @param  mixed $value
  * @param array $context The rest of the form this validator is/may be associated with
  * @return bool
  * @throws Exception\RuntimeException If validation of $value is impossible
  */
 public function isValid($value, $context = null)
 {
     $value = strtolower($value);
     // Set the value lowercase to avoid any issues with weird casing
     if ($context !== NULL) {
         $this->setCountryCode($context['country']);
     }
     $this->setValue($value);
     $countryCode = $this->getCountryCode();
     $countryCodeValidator = new CountryCode();
     if (!$countryCodeValidator->isValid($countryCode)) {
         $this->error(self::MSG_INVALID_COUNTRY_CODE, $countryCode);
         return false;
     }
     $regions = $this->getRegions();
     if (empty($regions) || !is_array($regions)) {
         throw new Exception\RuntimeException('Regions Array is not valid');
     }
     $allRegions = array();
     if ($this->getIncludeSubRegions()) {
         foreach ($regions as $region) {
             $allRegions[] = strtolower($region['name']);
             if (array_key_exists('subregions', $region) && is_array($region['subregions'])) {
                 foreach ($region['subregions'] as $subregion) {
                     $allRegions[] = strtolower($subregion['name']);
                 }
             }
         }
     } else {
         foreach ($regions as $region) {
             $allRegions[] = strtolower($region['name']);
         }
     }
     if (!in_array($value, $allRegions)) {
         if ($this->getIncludeSubRegions()) {
             $this->error(self::MSG_INVALID_REGION_SUBREGION_NAME);
         } else {
             $this->error(self::MSG_INVALID_REGION_NAME);
         }
         return false;
     }
     return true;
 }
Esempio n. 2
0
 public function getRegionNameByAbbreviation($abbreviation)
 {
     $value = strtoupper($abbreviation);
     // Set the value lowercase to avoid any issues with weird casing
     $countryCode = $this->getCountryCode();
     $countryCodeValidator = new CountryCode();
     if (!$countryCodeValidator->isValid($countryCode)) {
         return false;
     }
     $regions = $this->getRegions();
     if (empty($regions) || !is_array($regions)) {
         throw new RuntimeException('Regions Array is not valid');
     }
     $allRegions = array();
     if ($this->getIncludeSubRegions()) {
         foreach ($regions as $abbv => $region) {
             $allRegions[$abbv] = ucwords($region['name']);
             if (array_key_exists('subregions', $region) && is_array($region['subregions'])) {
                 foreach ($region['subregions'] as $abbv => $subregion) {
                     $allRegions[strtoupper($abbv)] = ucwords($subregion['name']);
                 }
             }
         }
     } else {
         foreach ($regions as $abbv => $region) {
             $allRegions[strtoupper($abbv)] = ucwords($region['name']);
         }
     }
     if (!array_key_exists($value, $allRegions)) {
         return false;
     }
     return $allRegions[$value];
 }