/** * Validates a Locale * * @param string $locale The locale to be validated * @param Constraint $constraint Locale Constraint * * @throws UnexpectedTypeException */ public function validate($locale, Constraint $constraint) { if (null === $locale || '' === $locale) { return; } if (!is_scalar($locale) && !(is_object($locale) && method_exists($locale, '__toString'))) { throw new UnexpectedTypeException($locale, 'string'); } $locale = (string) $locale; if ($this->strictMode) { if (!in_array($locale, $this->getAllowedLocales())) { $this->context->addViolation($constraint->message, array('%string%' => $locale)); } } else { if ($this->intlExtension) { $primary = \Locale::getPrimaryLanguage($locale); } else { $splittedLocale = explode('_', $locale); $primary = count($splittedLocale) > 1 ? $splittedLocale[0] : $locale; } if (!in_array($locale, $this->getAllowedLocales()) && !in_array($primary, $this->getAllowedLocales())) { $this->context->addViolation($constraint->message, array('%string%' => $locale)); } } }
/** * Validates a Locale * * @param string $locale The locale to be validated * @param Constraint $constraint Locale Constraint * * @throws \Symfony\Component\Validator\Exception\UnexpectedTypeException */ public function validate($locale, Constraint $constraint) { if (null === $locale || '' === $locale) { return; } if (!is_scalar($locale) && !(is_object($locale) && method_exists($locale, '__toString'))) { throw new UnexpectedTypeException($locale, 'string'); } $locale = (string) $locale; if ($this->intlExtension) { $primary = \Locale::getPrimaryLanguage($locale); $region = \Locale::getRegion($locale); $locales = Intl::getLocaleBundle()->getLocales(); if (null !== $region && strtolower($primary) != strtolower($region) && !in_array($locale, $locales) && !in_array($primary, $locales)) { $this->context->addViolation($constraint->message, array('%string%' => $locale)); } } else { $splittedLocale = explode('_', $locale); $splitCount = count($splittedLocale); if ($splitCount == 1) { $primary = $splittedLocale[0]; if (!in_array($primary, $this->iso639)) { $this->context->addViolation($constraint->message, array('%string%' => $locale)); } } elseif ($splitCount == 2) { $primary = $splittedLocale[0]; $region = $splittedLocale[1]; if (!in_array($primary, $this->iso639) && !in_array($region, $this->iso3166)) { $this->context->addViolation($constraint->message, array('%string%' => $locale)); } } elseif ($splitCount > 2) { $primary = $splittedLocale[0]; $script = $splittedLocale[1]; $region = $splittedLocale[2]; if (!in_array($primary, $this->iso639) && !in_array($region, $this->iso3166) && !in_array($script, $this->script)) { $this->context->addViolation($constraint->message, array('%string%' => $locale)); } } } }