Example #1
0
    /**
     * 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;
    }
Example #2
0
 /**
  * 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;
 }
Example #3
0
 /**
  * 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;
 }
 /**
  * Defined by Zend_Filter_Interface
  *
  * Normalizes the given input
  *
  * @param  string $value Value to normalized
  * @return string|array The normalized value
  */
 public function filter($value)
 {
     if (Format::isNumber($value, $this->_options)) {
         return 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 Format::getTime($value, $this->_options);
         } else {
             if (Format::checkDateFormat($value, $this->_options)) {
                 // Detect date or time input
                 return Format::getDate($value, $this->_options);
             }
         }
     }
     return $value;
 }
Example #5
0
File: Int.php Project: kingsj/core
 /**
  * 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 (!\Zend\Locale\Format::isInteger($value, array('locale' => $this->_locale))) {
                 $this->error(self::NOT_INT);
                 return false;
             }
         } catch (\Zend\Locale\Exception $e) {
             $this->error(self::NOT_INT);
             return false;
         }
     }
     return true;
 }
Example #6
0
    /**
     * 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 (!Locale\Format::isFloat($value, array('locale' => $this->_locale))) {
                $this->_error(self::NOT_FLOAT);
                return false;
            }
        } catch (Locale\Exception $e) {
            $this->_error(self::NOT_FLOAT);
            return false;
        }

        return true;
    }
Example #7
0
 /**
  * @group ZF-9319
  */
 public function testToNumberWithoutFormatWithPrecision()
 {
     $options = array('locale' => 'de_AT', 'precision' => 2);
     $this->assertEquals('3,99', \Zend\Locale\Format::toNumber(3.99, $options));
     $this->assertEquals('3,99', \Zend\Locale\Format::toNumber(3.994, $options));
     $this->assertEquals('4,00', \Zend\Locale\Format::toNumber(3.995, $options));
     $this->assertEquals('4,00', \Zend\Locale\Format::toNumber(3.999, $options));
     $this->assertEquals('4,00', \Zend\Locale\Format::toNumber(4, $options));
 }
    /**
     * Defined by 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)) {
            $date = new Date($value, $this->_options['locale']);
            return $date->toString($this->_options['date_format']);
        } else if ($this->_options['precision'] === 0) {
            return Format::toInteger($value, $this->_options);
        } else if ($this->_options['precision'] === null) {
            return Format::toFloat($value, $this->_options);
        }

        return Format::toNumber($value, $this->_options);
    }
Example #9
0
 /**
  * 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 {
         $parsed = \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'] = ZendDate\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;
 }
Example #10
0
 /**
  * Set a new value
  *
  * @param  integer                   $value  Value
  * @param  string                    $type   (Optional) A Zend\Measure\Number Type
  * @param  string|Zend\Locale\Locale $locale (Optional) A Zend\Locale\Locale Type
  * @throws Zend\Measure\Exception
  */
 public function setValue($value, $type = null, $locale = null)
 {
     if (empty($locale)) {
         $locale = $this->_locale;
     }
     if (empty($this->_units[$type])) {
         throw new Exception('unknown type of number:' . $type);
     }
     switch ($type) {
         case 'BINARY':
             preg_match('/[01]+/', $value, $ergebnis);
             $value = $ergebnis[0];
             break;
         case 'TERNARY':
             preg_match('/[012]+/', $value, $ergebnis);
             $value = $ergebnis[0];
             break;
         case 'QUATERNARY':
             preg_match('/[0123]+/', $value, $ergebnis);
             $value = $ergebnis[0];
             break;
         case 'QUINARY':
             preg_match('/[01234]+/', $value, $ergebnis);
             $value = $ergebnis[0];
             break;
         case 'SENARY':
             preg_match('/[012345]+/', $value, $ergebnis);
             $value = $ergebnis[0];
             break;
         case 'SEPTENARY':
             preg_match('/[0123456]+/', $value, $ergebnis);
             $value = $ergebnis[0];
             break;
         case 'OCTAL':
             preg_match('/[01234567]+/', $value, $ergebnis);
             $value = $ergebnis[0];
             break;
         case 'NONARY':
             preg_match('/[012345678]+/', $value, $ergebnis);
             $value = $ergebnis[0];
             break;
         case 'DUODECIMAL':
             preg_match('/[0123456789AB]+/', strtoupper($value), $ergebnis);
             $value = $ergebnis[0];
             break;
         case 'HEXADECIMAL':
             preg_match('/[0123456789ABCDEF]+/', strtoupper($value), $ergebnis);
             $value = $ergebnis[0];
             break;
         case 'ROMAN':
             preg_match('/[IVXLCDM_]+/', strtoupper($value), $ergebnis);
             $value = $ergebnis[0];
             break;
         default:
             try {
                 $value = Locale\Format::getInteger($value, array('locale' => $locale));
             } catch (\Exception $e) {
                 throw new Exception($e->getMessage(), $e->getCode(), $e);
             }
             if (call_user_func(Math::$comp, $value, 0) < 0) {
                 $value = call_user_func(Math::$sqrt, call_user_func(Math::$pow, $value, 2));
             }
             break;
     }
     $this->_value = $value;
     $this->_type = $type;
 }
Example #11
0
 /**
  * @group ZF-11837
  */
 public function testCheckDateFormatDoesNotEmitNoticeWhenNoOptionsAreNotProvided()
 {
     try {
         setlocale(LC_ALL, 'en_US');
         // test setup
         Format::setOptions(array('date_format' => 'yyyy-MM-dd'));
         $this->assertTrue(Format::checkDateFormat('2011-10-21', array()));
     } catch (\PHPUnit_Framework_Error_Notice $ex) {
         $this->fail('Zend_Locale_Format::checkDateFormat emitted unexpected E_NOTICE');
     }
 }