/** * Setup environment */ public function setUp() { date_default_timezone_set("America/Chicago"); \Zend\Locale\Locale::setDefault('en'); Parser\TypeLoader::resetMap(); $this->_request = new \Zend\AMF\Request\StreamRequest(); }
/** * Setup environment */ public function setUp() { date_default_timezone_set('America/Chicago'); Locale::setDefault('en_US'); Parser\TypeLoader::resetMap(); $this->_response = new \Zend\AMF\Response\StreamResponse(); }
/** * Translate a message * You can give multiple params or an array of params. * If you want to output another locale just set it as last single parameter * Example 1: translate('%1\$s + %2\$s', $value1, $value2, $locale); * Example 2: translate('%1\$s + %2\$s', array($value1, $value2), $locale); * * @param string $messageid Id of the message to be translated * @return string|\Zend\View\Helper\Translate Translated message */ public function direct($messageid = null) { if ($messageid === null) { return $this; } $translate = $this->getTranslator(); $options = func_get_args(); array_shift($options); $count = count($options); $locale = null; if ($count > 0) { if (\Zend\Locale\Locale::isLocale($options[$count - 1]) !== false) { $locale = array_pop($options); } } if (count($options) === 1 and is_array($options[0]) === true) { $options = $options[0]; } if ($translate !== null) { $messageid = $translate->translate($messageid, $locale); } if (count($options) === 0) { return $messageid; } return vsprintf($messageid, $options); }
/** * Tests if the given language is really a language */ public function testIsLocale() { foreach ($this->_languages as $lang) { if (!Locale::isLocale($lang, true)) { $this->fail("Language directory '{$lang}' not a valid locale"); } } }
/** * Setup environment */ public function setUp() { $this->_originaltimezone = date_default_timezone_get(); date_default_timezone_set('America/Chicago'); Locale::setFallback('en_US'); Parser\TypeLoader::resetMap(); $this->_response = new \Zend\Amf\Response\StreamResponse(); }
/** * Setup environment */ public function setUp() { $this->_originaltimezone = date_default_timezone_get(); date_default_timezone_set("America/Chicago"); \Zend\Locale\Locale::setFallback('en'); Parser\TypeLoader::resetMap(); $this->_request = new \Zend\Amf\Request\StreamRequest(); }
/** * Retrieve locale object * * @return \Zend\Locale\Locale */ public function getLocale() { if (null === $this->_locale) { $options = $this->getOptions(); if (!isset($options['default'])) { $this->_locale = new SystemLocale\Locale(); } elseif (!isset($options['force']) || (bool) $options['force'] == false) { // Don't force any locale, just go for auto detection SystemLocale\Locale::setDefault($options['default']); $this->_locale = new SystemLocale\Locale(); } else { $this->_locale = new SystemLocale\Locale($options['default']); } $key = isset($options['registry_key']) && !is_numeric($options['registry_key']) ? $options['registry_key'] : self::DEFAULT_REGISTRY_KEY; \Zend\Registry::set($key, $this->_locale); } return $this->_locale; }
/** * Output a formatted currency * * @param integer|float $value Currency value to output * @param string|Zend_Locale|\Zend\Currency\Currency $currency OPTIONAL Currency to use for this call * @return string Formatted currency */ public function direct($value = null, $currency = null) { if ($value === null) { return $this; } if (is_string($currency) || $currency instanceof Locale\Locale) { if (Locale\Locale::isLocale($currency)) { $currency = array('locale' => $currency); } } if (is_string($currency)) { $currency = array('currency' => $currency); } if (is_array($currency)) { return $this->_currency->toCurrency($value, $currency); } return $this->_currency->toCurrency($value); }
/** * Translate a message * You can give multiple params or an array of params. * If you want to output another locale just set it as last single parameter * Example 1: translate('%1\$s + %2\$s', $value1, $value2, $locale); * Example 2: translate('%1\$s + %2\$s', array($value1, $value2), $locale); * * @param string $messageid Id of the message to be translated * @return string|\Zend\View\Helper\Translator Translated message */ public function __invoke($messageid = null) { if ($messageid === null) { return $this; } $translator = $this->getTranslator(); $options = func_get_args(); array_shift($options); $count = count($options); $locale = null; if ($count > 0) { if (Locale::isLocale($options[$count - 1]) !== false) { // Don't treat last option as the locale if doing so will result in an error if (is_array($options[0]) || @vsprintf($messageid, array_slice($options, 0, -1)) !== false) { $locale = array_pop($options); } } } if (count($options) === 1 and is_array($options[0]) === true) { $options = $options[0]; } if ($translator !== null) { $messageid = $translator->translate($messageid, $locale); } if (count($options) === 0) { return $messageid; } return vsprintf($messageid, $options); }
<?php // Set used namespaces use Zend\Loader\StandardAutoloader; use Zend\Locale\Locale; use Zend\Service\LiveDocx\Helper; // Turn up error reporting error_reporting(E_ALL | E_STRICT); // Library base $base = dirname(dirname(dirname(dirname(__DIR__)))); // Set up autoloader require_once "{$base}/library/Zend/Loader/StandardAutoloader.php"; $loader = new StandardAutoloader(); $loader->registerNamespace('Zend', "{$base}/library/Zend"); $loader->register(); // Include utility class require_once "{$base}/demos/Zend/Service/LiveDocx/library/Zend/Service/LiveDocx/Helper.php"; // Set fallback locale Locale::setFallback(Helper::LOCALE); // Ensure LiveDocx credentials are available if (false === Helper::credentialsAvailable()) { Helper::printLine(Helper::credentialsHowTo()); exit; } unset($base);
/** * Internal method for checking the options array * * @param array $options Options to check * @throws Zend\Currency\Exception On unknown position * @throws Zend\Currency\Exception On unknown locale * @throws Zend\Currency\Exception On unknown display * @throws Zend\Currency\Exception On precision not between -1 and 30 * @throws Zend\Currency\Exception On problem with script conversion * @throws Zend\Currency\Exception On unknown options * @return array */ protected function _checkOptions(array $options = array()) { if (count($options) === 0) { return $this->_options; } foreach ($options as $name => $value) { $name = strtolower($name); if ($name !== 'format') { if (gettype($value) === 'string') { $value = strtolower($value); } } switch ($name) { case 'position': if ($value !== self::STANDARD and $value !== self::RIGHT and $value !== self::LEFT) { throw new Exception\InvalidArgumentException("Unknown position '" . $value . "'"); } break; case 'format': if (empty($value) === false and Locale\Locale::isLocale($value) === false) { if (!is_string($value) || strpos($value, '0') === false) { throw new Exception\InvalidArgumentException('\'' . (gettype($value) === 'object' ? get_class($value) : $value) . '\' is no format token'); } } break; case 'display': if (is_numeric($value) and $value !== self::NO_SYMBOL and $value !== self::USE_SYMBOL and $value !== self::USE_SHORTNAME and $value !== self::USE_NAME) { throw new Exception\InvalidArgumentException("Unknown display '{$value}'"); } break; case 'precision': if ($value === null) { $value = -1; } if ($value < -1 or $value > 30) { throw new Exception\InvalidArgumentException("'{$value}' precision has to be between -1 and 30."); } break; case 'script': try { Locale\Format::convertNumerals(0, $options['script']); } catch (Locale\Exception $e) { throw new Exception\InvalidArgumentException($e->getMessage()); } break; default: break; } } return $options; }
/** * Checks if a string is translated within the source or not * returns boolean * * @param string $messageId Translation string * @param boolean $original (optional) Allow translation only for original language * when true, a translation for 'en_US' would give false when it can * be translated with 'en' only * @param string|\Zend\Locale\Locale $locale (optional) Locale/Language to use, identical with locale identifier, * see Zend_Locale for more information * @return boolean */ public function isTranslated($messageId, $original = false, $locale = null) { if ($original !== false and $original !== true) { $locale = $original; $original = false; } if ($locale === null) { $locale = $this->_options['locale']; } if (!Locale\Locale::isLocale($locale, true, false)) { if (!Locale\Locale::isLocale($locale, false, false)) { // language does not exist, return original string return false; } $locale = new Locale\Locale($locale); } $locale = (string) $locale; if ((is_string($messageId) || is_int($messageId)) && isset($this->_translate[$locale][$messageId])) { // return original translation return true; } else { if (strlen($locale) != 2 and $original === false) { // faster than creating a new locale and separate the leading part $locale = substr($locale, 0, -strlen(strrchr($locale, '_'))); if ((is_string($messageId) || is_int($messageId)) && isset($this->_translate[$locale][$messageId])) { // return regionless translation (en_US -> en) return true; } } } // No translation found, return original return false; }
/** * test setOption * expected boolean */ public function testSetOption() { $this->assertEquals(8, count(Format::setOptions(array('format_type' => 'php')))); $this->assertTrue(is_array(Format::setOptions())); try { $this->assertTrue(Format::setOptions(array('format_type' => 'xxx'))); $this->fail("exception expected"); } catch (InvalidArgumentException $e) { // success } try { $this->assertTrue(Format::setOptions(array('myformat' => 'xxx'))); $this->fail("exception expected"); } catch (InvalidArgumentException $e) { // success } $format = Format::setOptions(array('locale' => 'de', 'number_format' => Format::STANDARD)); $test = Cldr::getContent('de', 'decimalnumber'); $this->assertEquals($test, $format['number_format']); try { $this->assertFalse(Format::setOptions(array('number_format' => array('x' => 'x')))); $this->fail("exception expected"); } catch (InvalidArgumentException $e) { // success } $format = Format::setOptions(array('locale' => 'de', 'date_format' => Format::STANDARD)); $test = Format::getDateFormat('de'); $this->assertEquals($test, $format['date_format']); try { $this->assertFalse(Format::setOptions(array('date_format' => array('x' => 'x')))); $this->fail("exception expected"); } catch (InvalidArgumentException $e) { // success } try { $this->assertFalse(is_array(Format::setOptions(array('fix_date' => 'no')))); $this->fail("exception expected"); } catch (InvalidArgumentException $e) { // success } $format = Format::setOptions(array('locale' => Format::STANDARD)); $locale = new Locale(); $this->assertEquals($locale->toString(), $format['locale']); try { $this->assertFalse(is_array(Format::setOptions(array('locale' => 'nolocale')))); $this->fail("exception expected"); } catch (InvalidArgumentException $e) { // success } try { $this->assertFalse(is_array(Format::setOptions(array('precision' => 50)))); $this->fail("exception expected"); } catch (InvalidArgumentException $e) { // success } // test interaction between class-wide default date format and using locale's default format try { $result = array('date_format' => 'MMM d, y', 'locale' => 'en_US', 'month' => '7', 'day' => '4', 'year' => '2007'); Format::setOptions(array('format_type' => 'iso', 'date_format' => 'MMM d, y', 'locale' => 'en_US')); // test setUp } catch (InvalidArgumentException $e) { $this->fail("exception expected"); } try { // uses global date_format with global locale $this->assertSame($result, Format::getDate('July 4, 2007')); } catch (InvalidArgumentException $e) { $this->fail("exception expected"); } try { // uses global date_format with given locale $this->assertSame($result, Format::getDate('July 4, 2007', array('locale' => 'en_US'))); } catch (InvalidArgumentException $e) { $this->fail("exception expected"); } try { // sets a new global date format Format::setOptions(array('date_format' => 'M-d-y')); } catch (InvalidArgumentException $e) { $this->fail("exception expected"); } try { // uses global date format with given locale // but this date format differs from the original set... (MMMM d, yyyy != M-d-y) $expected = Format::getDate('July 4, 2007', array('locale' => 'en_US')); $this->assertSame($expected['year'], $result['year']); $this->assertSame($expected['month'], $result['month']); $this->assertSame($expected['day'], $result['day']); } catch (InvalidArgumentException $e) { $this->fail("exception expected"); } try { // the following should not be used... instead of null, Locale::ZFDEFAULT should be used // uses given local with standard format from this locale $this->assertSame($result, Format::getDate('July 4, 2007', array('locale' => 'en_US', 'date_format' => null))); } catch (InvalidArgumentException $e) { $this->fail("exception expected"); } try { // uses given locale with standard format from this locale $this->assertSame($result, Format::getDate('July 4, 2007', array('locale' => 'en_US', 'date_format' => Format::STANDARD))); } catch (InvalidArgumentException $e) { $this->fail("exception expected"); } try { // uses standard locale with standard format from this locale $expect = Format::getDate('July 4, 2007', array('locale' => Format::STANDARD)); $testlocale = new Locale(); $this->assertEquals($testlocale->toString(), $expect['locale']); } catch (InvalidArgumentException $e) { $this->fail("exception expected"); } Format::setOptions(array('date_format' => null, 'locale' => null)); // test tearDown }
/** * Determine the value of a localized string, and compare it to a given value * * @param string $value * @param boolean $yes * @param array $locale * @return boolean */ protected function _getLocalizedQuestion($value, $yes, $locale) { if ($yes == true) { $question = 'yes'; $return = true; } else { $question = 'no'; $return = false; } $str = Locale::getTranslation($question, 'question', $locale); $str = explode(':', $str); if (!empty($str)) { foreach ($str as $no) { if ($no == $value || strtolower($no) == strtolower($value)) { return $return; } } } }
/** * Finds the proper locale based on the input * Checks if it exists, degrades it when necessary * Detects registry locale and when all fails tries to detect a automatic locale * Returns the found locale as string * * @param string $locale * @throws \Zend\Locale\Exception When the given locale is no locale or the autodetection fails * @return string */ public static function findLocale($locale = null) { if ($locale === null) { if (Registry::isRegistered('Zend_Locale')) { $locale = Registry::get('Zend_Locale'); } } if ($locale === null) { $locale = new Locale(); } if (!Locale::isLocale($locale, true, false)) { if (!Locale::isLocale($locale, false, false)) { throw new Exception("The locale '{$locale}' is no known locale"); } $locale = new Locale($locale); } $locale = self::_prepareLocale($locale); return $locale; }
/** * Sets the locale option * * @param string|Locale $locale * @return Iban provides a fluent interface */ public function setLocale($locale = null) { if ($locale !== false) { $locale = Locale::findLocale($locale); if (strlen($locale) < 4) { throw new Exception\InvalidArgumentException('Region must be given for IBAN validation'); } } $this->locale = $locale; return $this; }
/** * Internal function for checking the locale * * @param string|\Zend\Locale $locale Locale to check * @return string */ private static function _checkLocale($locale) { if (empty($locale)) { $locale = new Locale(); } if (!Locale::isLocale((string) $locale)) { throw new Exception\InvalidArgumentException("Locale (" . (string) $locale . ") is a unknown locale"); } return (string) $locale; }
/** * Sets the locale to use * * @param string|\Zend\Locale\Locale $locale */ public function setLocale($locale = null) { $this->_locale = Locale\Locale::findLocale($locale); return $this; }
public function testDefaultLocale() { $lang = new Translator\Translator(Translator\Translator::AN_ARRAY, array('msg1' => 'message1')); $defaultLocale = new Locale\Locale(); $this->assertEquals($defaultLocale->toString(), $lang->getLocale()); }
/** * Returns detailed informations from the variant table * If no detail is given a complete table is returned * * @param string $locale Normalized locale * @param boolean $invert Invert output of the data * @param string|array $detail Detail to return information for * @return array */ public static function getDisplayVariant($locale, $invert = false, $detail = null) { if ($detail !== null) { return Locale::getDisplayVariant($locale); } else { $list = ZFLocale::getLocaleList(); foreach ($list as $key => $value) { $list[$key] = Locale::getDisplayVariant($key); } if ($invert) { array_flip($list); } return $list; } }
/** * Sets the locale to use * * @param string|\Zend\Locale\Locale $locale * @throws \Zend\Validator\Exception On unrecognised region * @throws \Zend\Validator\Exception On not detected format * @return \Zend\Validator\PostCode Provides fluid interface */ public function setLocale($locale = null) { $this->_locale = Locale\Locale::findLocale($locale); $locale = new Locale\Locale($this->_locale); $region = $locale->getRegion(); if (empty($region)) { throw new Exception\InvalidArgumentException("Unable to detect a region for the locale '$locale'"); } $format = Locale\Locale::getTranslation( $locale->getRegion(), 'postaltoterritory', $this->_locale ); if (empty($format)) { throw new Exception\InvalidArgumentException("Unable to detect a postcode format for the region '{$locale->getRegion()}'"); } $this->setFormat($format); return $this; }
/** * Internal function for checking the options array of proper input values * See {@link setOptions()} for details. * * @param array $options Array of options, keyed by option name: format_type = 'iso' | 'php', fix_date = true | false, * locale = Zend_Locale | locale string, precision = whole number between -1 and 30 * @throws \Zend\Locale\Exception\InvalidArgumentException * @return Options array if no option was given */ private static function _checkOptions(array $options = array()) { if (count($options) == 0) { return self::$_options; } foreach ($options as $name => $value) { $name = strtolower($name); if ($name !== 'locale') { if (gettype($value) === 'string') { $value = strtolower($value); } } switch ($name) { case 'number_format': if ($value == self::STANDARD) { $locale = self::$_options['locale']; if (isset($options['locale'])) { $locale = $options['locale']; } $options['number_format'] = Cldr::getContent($locale, 'decimalnumber'); } else { if (gettype($value) !== 'string' and $value !== NULL) { throw new Exception\InvalidArgumentException("Unknown number format type '" . gettype($value) . "'. " . "Format '{$value}' must be a valid number format string."); } } break; case 'date_format': if ($value == self::STANDARD) { $locale = self::$_options['locale']; if (isset($options['locale'])) { $locale = $options['locale']; } $options['date_format'] = self::getDateFormat($locale); } else { if (gettype($value) !== 'string' and $value !== NULL) { throw new Exception\InvalidArgumentException("Unknown dateformat type '" . gettype($value) . "'. " . "Format '{$value}' must be a valid ISO or PHP date format string."); } else { if (isset($options['format_type']) === true and $options['format_type'] == 'php' or isset($options['format_type']) === false and self::$_options['format_type'] == 'php') { $options['date_format'] = self::convertPhpToIsoFormat($value); } } } break; case 'format_type': if ($value != 'php' && $value != 'iso') { throw new Exception\InvalidArgumentException("Unknown date format type '{$value}'. Only 'iso' and 'php'" . " are supported."); } break; case 'fix_date': if ($value !== true && $value !== false) { throw new Exception\InvalidArgumentException("Enabling correction of dates must be either true or false" . "(fix_date='{$value}')."); } break; case 'locale': $options['locale'] = Locale::findLocale($value); break; case 'cache': if ($value instanceof \Zend\Cache\Core) { Cldr::setCache($value); } break; case 'disablecache': Cldr::disableCache($value); break; case 'precision': if ($value === NULL) { $value = -1; } if ($value < -1 || $value > 30) { throw new Exception\InvalidArgumentException("'{$value}' precision is not a whole number less than 30."); } break; default: throw new Exception\InvalidArgumentException("Unknown option: '{$name}' = '{$value}'"); break; } } return $options; }
/** * Internal method, called by xml element handler at start * * @param resource $file File handler * @param string $name Elements name * @param array $attrib Attributes for this element */ protected function _startElement($file, $name, $attrib) { if ($this->_seg !== null) { $this->_content .= "<" . $name; foreach ($attrib as $key => $value) { $this->_content .= " {$key}=\"{$value}\""; } $this->_content .= ">"; } else { switch (strtolower($name)) { case 'header': if (empty($this->_useId) && isset($attrib['srclang'])) { if (Locale\Locale::isLocale($attrib['srclang'])) { $this->_srclang = Locale\Locale::findLocale($attrib['srclang']); } else { if (!$this->_options['disableNotices']) { if ($this->_options['log']) { $this->_options['log']->notice("The language '{$attrib['srclang']}' can not be set because it does not exist."); } else { trigger_error("The language '{$attrib['srclang']}' can not be set because it does not exist.", E_USER_NOTICE); } } $this->_srclang = $attrib['srclang']; } } break; case 'tu': if (isset($attrib['tuid'])) { $this->_tu = $attrib['tuid']; } break; case 'tuv': if (isset($attrib['xml:lang'])) { if (Locale\Locale::isLocale($attrib['xml:lang'])) { $this->_tuv = Locale\Locale::findLocale($attrib['xml:lang']); } else { if (!$this->_options['disableNotices']) { if ($this->_options['log']) { $this->_options['log']->notice("The language '{$attrib['xml:lang']}' can not be set because it does not exist."); } else { trigger_error("The language '{$attrib['xml:lang']}' can not be set because it does not exist.", E_USER_NOTICE); } } $this->_tuv = $attrib['xml:lang']; } if (!isset($this->_data[$this->_tuv])) { $this->_data[$this->_tuv] = array(); } } break; case 'seg': $this->_seg = true; $this->_content = null; break; default: break; } } }
/** * @ZF-9488 */ public function testTerritoryToGetLocale() { $value = Locale::findLocale('US'); $this->assertEquals('en_US', $value); $value = new Locale('US'); $this->assertEquals('en_US', $value->toString()); $value = new Locale('TR'); $this->assertEquals('tr_TR', $value->toString()); }
/** * Finds the proper locale based on the input * Checks if it exists, degrades it when necessary * Detects registry locale and when all fails tries to detect a automatic locale * Returns the found locale as string * * @param string $locale * @throws \Zend\Locale\Exception\InvalidArgumentException When the given locale is no locale or the autodetection fails * @return string */ public static function findLocale($locale = null) { if ($locale === null) { if (Registry::isRegistered('Zend_Locale')) { $locale = Registry::get('Zend_Locale'); } } if ($locale === null) { $locale = new Locale(); } if (!Locale::isLocale($locale, true)) { if (!Locale::isLocale($locale, false)) { $locale = Locale::getLocaleToTerritory($locale); if (empty($locale)) { throw new Exception\InvalidArgumentException("The locale '{$locale}' is no known locale"); } } else { $locale = new self($locale); } } $locale = self::_prepareLocale($locale); return $locale; }
/** * Zend\Measure\AbstractMeasure is an abstract class for the different measurement types * * @param integer $value Value * @param string $type (Optional) A Zend\Measure\Number Type * @param string|Zend\Locale\Locale $locale (Optional) A Zend\Locale\Locale * @throws Zend\Measure\Exception When language is unknown * @throws Zend\Measure\Exception When type is unknown */ public function __construct($value, $type, $locale = null) { if ($type !== null and Locale\Locale::isLocale($type, null, false)) { $locale = $type; $type = null; } if ($locale === null) { $locale = new Locale\Locale(); } if (!Locale\Locale::isLocale($locale, true, false)) { if (!Locale\Locale::isLocale($locale, true, false)) { throw new Exception("Language (" . (string) $locale . ") is unknown"); } $locale = new Locale\Locale($locale); } $this->_locale = (string) $locale; if ($type === null) { $type = $this->_units['STANDARD']; } if (isset($this->_units[$type]) === false) { throw new Exception("Type ({$type}) is unknown"); } $this->setValue($value, $type, $this->_locale); }
/** * Set a new value * * @param integer|string $value Value as string, integer, real or float * @param string $type OPTIONAL A measure type f.e. Zend_Measure_Length::METER * @param string|Zend\Locale\Locale $locale OPTIONAL Locale for parsing numbers * @throws Zend\Measure\Exception * @return Zend\Measure\AbstractMeasure */ public function setValue($value, $type = null, $locale = null) { if (($type !== null) and (Locale\Locale::isLocale($type))) { $locale = $type; $type = null; } if ($locale === null) { $locale = $this->_locale; } $this->setLocale($locale, true); if ($type === null) { $type = $this->_units['STANDARD']; } if (empty($this->_units[$type])) { throw new Exception("Type ($type) is unknown"); } try { $value = Locale\Format::getNumber($value, array('locale' => $locale)); } catch(\Exception $e) { throw new Exception($e->getMessage(), $e->getCode(), $e); } $this->_value = $value; $this->setType($type); return $this; }
/** * test MultiPartLocales * expected boolean */ public function testLongLocale() { $locale = new LocaleTestHelper('de_Latn_DE'); $this->assertEquals('de_DE', $locale->toString()); $this->assertTrue(LocaleTestHelper::isLocale('de_Latn_CAR_DE_sup3_win')); $locale = new LocaleTestHelper('de_Latn_DE'); $this->assertEquals('de_DE', $locale->toString()); $this->assertEquals('fr_FR', Locale::findLocale('fr-Arab-FR')); }
/** * Checks if the given date is a real date or datepart. * Returns false if a expected datepart is missing or a datepart exceeds its possible border. * But the check will only be done for the expected dateparts which are given by format. * If no format is given the standard dateformat for the actual locale is used. * f.e. 30.February.2007 will return false if format is 'dd.MMMM.YYYY' * * @param string|array|\Zend\Date\Date $date Date to parse for correctness * @param string $format (Optional) Format for parsing the date string * @param string|\Zend\Locale\Locale $locale (Optional) Locale for parsing date parts * @return boolean True when all date parts are correct */ public static function isDate($date, $format = null, $locale = null) { if (!is_string($date) && !is_numeric($date) && !$date instanceof Date && !is_array($date)) { return false; } if ($format !== null && $format != 'ee' && $format != 'ss' && $format != 'GG' && $format != 'MM' && $format != 'EE' && $format != 'TT' && Locale::isLocale($format)) { $locale = $format; $format = null; } $locale = Locale::findLocale($locale); if ($format === null) { $format = Format::getDateFormat($locale); } else { if (self::$_options['format_type'] == 'php' && !defined($format)) { $format = Format::convertPhpToIsoFormat($format); } } $format = self::_getLocalizedToken($format, $locale); if (!is_array($date)) { try { $parsed = Format::getDate($date, array('locale' => $locale, 'date_format' => $format, 'format_type' => 'iso', 'fix_date' => false)); } catch (\Zend\Locale\Exception $e) { // Date can not be parsed return false; } } else { $parsed = $date; } if ((strpos($format, 'Y') !== false or strpos($format, 'y') !== false) and !isset($parsed['year'])) { // Year expected but not found return false; } if (strpos($format, 'M') !== false and !isset($parsed['month'])) { // Month expected but not found return false; } if (strpos($format, 'd') !== false and !isset($parsed['day'])) { // Day expected but not found return false; } if ((strpos($format, 'H') !== false or strpos($format, 'h') !== false) and !isset($parsed['hour'])) { // Hour expected but not found return false; } if (strpos($format, 'm') !== false and !isset($parsed['minute'])) { // Minute expected but not found return false; } if (strpos($format, 's') !== false and !isset($parsed['second'])) { // Second expected but not found return false; } // Set not given dateparts if (isset($parsed['hour']) === false) { $parsed['hour'] = 12; } if (isset($parsed['minute']) === false) { $parsed['minute'] = 0; } if (isset($parsed['second']) === false) { $parsed['second'] = 0; } if (isset($parsed['month']) === false) { $parsed['month'] = 1; } if (isset($parsed['day']) === false) { $parsed['day'] = 1; } if (isset($parsed['year']) === false) { $parsed['year'] = 1970; } if (self::isYearLeapYear($parsed['year'])) { $parsed['year'] = 1972; } else { $parsed['year'] = 1971; } $date = new self($parsed, null, $locale); $timestamp = $date->mktime($parsed['hour'], $parsed['minute'], $parsed['second'], $parsed['month'], $parsed['day'], $parsed['year']); if ($parsed['year'] != $date->date('Y', $timestamp)) { // Given year differs from parsed year return false; } if ($parsed['month'] != $date->date('n', $timestamp)) { // Given month differs from parsed month return false; } if ($parsed['day'] != $date->date('j', $timestamp)) { // Given day differs from parsed day return false; } if ($parsed['hour'] != $date->date('G', $timestamp)) { // Given hour differs from parsed hour return false; } if ($parsed['minute'] != $date->date('i', $timestamp)) { // Given minute differs from parsed minute return false; } if ($parsed['second'] != $date->date('s', $timestamp)) { // Given second differs from parsed second return false; } return true; }
/** * Internal function for checking the locale * * @param string|\Zend\Locale $locale Locale to check * @return string */ protected static function _checkLocale($locale) { if (empty($locale)) { $locale = new Locale(); } if (!(Locale::isLocale((string) $locale))) { throw new InvalidArgumentException( "Locale (" . (string) $locale . ") is no known locale" ); } return (string) $locale; }