/** * Checks if the input contains a normalized or localized number * * @param string $input Localized number string * @param array $options Options: locale. See {@link setOptions()} for details. * @return boolean Returns true if a number was found */ public static function isNumber($input, array $options = array()) { if (!self::_getUniCodeSupport()) { trigger_error("Sorry, your PCRE extension does not support UTF8 which is needed for the I18N core", E_USER_NOTICE); } $options = self::_checkOptions($options) + self::$_options; // Get correct signs for this locale $symbols = IfwPsn_Vendor_Zend_Locale_Data::getList($options['locale'], 'symbols'); $regexs = IfwPsn_Vendor_Zend_Locale_Format::_getRegexForType('decimalnumber', $options); $regexs = array_merge($regexs, IfwPsn_Vendor_Zend_Locale_Format::_getRegexForType('scientificnumber', $options)); if (!empty($input) && $input[0] == $symbols['decimal']) { $input = 0 . $input; } foreach ($regexs as $regex) { preg_match($regex, $input, $found); if (isset($found[0])) { return true; } } return false; }
/** * 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|IfwPsn_Vendor_Zend_Date $date Date to parse for correctness * @param string $format (Optional) Format for parsing the date string * @param string|IfwPsn_Vendor_Zend_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 IfwPsn_Vendor_Zend_Date && !is_array($date)) { return false; } if ($format !== null && $format != 'ee' && $format != 'ss' && $format != 'GG' && $format != 'MM' && $format != 'EE' && $format != 'TT' && IfwPsn_Vendor_Zend_Locale::isLocale($format, null, false)) { $locale = $format; $format = null; } $locale = IfwPsn_Vendor_Zend_Locale::findLocale($locale); if ($format === null) { $format = IfwPsn_Vendor_Zend_Locale_Format::getDateFormat($locale); } else { if (self::$_options['format_type'] == 'php' && !defined($format)) { $format = IfwPsn_Vendor_Zend_Locale_Format::convertPhpToIsoFormat($format); } } $format = self::_getLocalizedToken($format, $locale); if (!is_array($date)) { try { $parsed = IfwPsn_Vendor_Zend_Locale_Format::getDate($date, array('locale' => $locale, 'date_format' => $format, 'format_type' => 'iso', 'fix_date' => false)); } catch (IfwPsn_Vendor_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; }
/** * Defined by IfwPsn_Vendor_Zend_Filter_Interface * * Normalizes the given input * * @param string $value Value to normalized * @return string|array The normalized value */ public function filter($value) { if (IfwPsn_Vendor_Zend_Locale_Format::isNumber($value, $this->_options)) { return IfwPsn_Vendor_Zend_Locale_Format::getNumber($value, $this->_options); } else { if ($this->_options['date_format'] === null && strpos($value, ':') !== false) { // Special case, no date format specified, detect time input return IfwPsn_Vendor_Zend_Locale_Format::getTime($value, $this->_options); } else { if (IfwPsn_Vendor_Zend_Locale_Format::checkDateFormat($value, $this->_options)) { // Detect date or time input return IfwPsn_Vendor_Zend_Locale_Format::getDate($value, $this->_options); } } } return $value; }
/** * Defined by IfwPsn_Vendor_Zend_Validate_Interface * * Returns true if and only if $value is a floating-point value * * @param string $value * @return boolean */ public function isValid($value) { if (!is_string($value) && !is_int($value) && !is_float($value)) { $this->_error(self::INVALID); return false; } if (is_float($value)) { return true; } $this->_setValue($value); try { if (!IfwPsn_Vendor_Zend_Locale_Format::isFloat($value, array('locale' => $this->_locale))) { $this->_error(self::NOT_FLOAT); return false; } } catch (IfwPsn_Vendor_Zend_Locale_Exception $e) { $this->_error(self::NOT_FLOAT); return false; } return true; }
/** * Check if the given date fits the given format * * @param string $value Date to check * @return boolean False when date does not fit the format */ private function _checkFormat($value) { try { require_once IFW_PSN_LIB_ROOT . 'IfwPsn/Vendor/Zend/Locale/Format.php'; $parsed = IfwPsn_Vendor_Zend_Locale_Format::getDate($value, array('date_format' => $this->_format, 'format_type' => 'iso', 'fix_date' => false)); if (isset($parsed['year']) and (strpos(strtoupper($this->_format), 'YY') !== false and strpos(strtoupper($this->_format), 'YYYY') === false)) { $parsed['year'] = IfwPsn_Vendor_Zend_Date::getFullYear($parsed['year']); } } catch (Exception $e) { // Date can not be parsed return false; } if ((strpos($this->_format, 'Y') !== false or strpos($this->_format, 'y') !== false) and !isset($parsed['year'])) { // Year expected but not found return false; } if (strpos($this->_format, 'M') !== false and !isset($parsed['month'])) { // Month expected but not found return false; } if (strpos($this->_format, 'd') !== false and !isset($parsed['day'])) { // Day expected but not found return false; } if ((strpos($this->_format, 'H') !== false or strpos($this->_format, 'h') !== false) and !isset($parsed['hour'])) { // Hour expected but not found return false; } if (strpos($this->_format, 'm') !== false and !isset($parsed['minute'])) { // Minute expected but not found return false; } if (strpos($this->_format, 's') !== false and !isset($parsed['second'])) { // Second expected but not found return false; } // Date fits the format return true; }
/** * Defined by IfwPsn_Vendor_Zend_Filter_Interface * * Normalizes the given input * * @param string $value Value to normalized * @return string|array The normalized value */ public function filter($value) { if (is_array($value)) { require_once IFW_PSN_LIB_ROOT . 'IfwPsn/Vendor/Zend/Date.php'; $date = new IfwPsn_Vendor_Zend_Date($value, $this->_options['locale']); return $date->toString($this->_options['date_format']); } else { if ($this->_options['precision'] === 0) { return IfwPsn_Vendor_Zend_Locale_Format::toInteger($value, $this->_options); } else { if ($this->_options['precision'] === null) { return IfwPsn_Vendor_Zend_Locale_Format::toFloat($value, $this->_options); } } } return IfwPsn_Vendor_Zend_Locale_Format::toNumber($value, $this->_options); }
/** * Defined by IfwPsn_Vendor_Zend_Validate_Interface * * Returns true if and only if $value is a valid integer * * @param string|integer $value * @return boolean */ public function isValid($value) { if (!is_string($value) && !is_int($value) && !is_float($value)) { $this->_error(self::INVALID); return false; } if (is_int($value)) { return true; } $this->_setValue($value); if ($this->_locale === null) { $locale = localeconv(); $valueFiltered = str_replace($locale['decimal_point'], '.', $value); $valueFiltered = str_replace($locale['thousands_sep'], '', $valueFiltered); if (strval(intval($valueFiltered)) != $valueFiltered) { $this->_error(self::NOT_INT); return false; } } else { try { if (!IfwPsn_Vendor_Zend_Locale_Format::isInteger($value, array('locale' => $this->_locale))) { $this->_error(self::NOT_INT); return false; } } catch (IfwPsn_Vendor_Zend_Locale_Exception $e) { $this->_error(self::NOT_INT); return false; } } return true; }