Ejemplo n.º 1
0
 /**
  * Set a new value
  *
  * @param  $value  mixed  - Value as string, integer, real or float
  * @param  $type   type   - OPTIONAL a Zend_Measure_Number Type
  * @param  $locale locale - OPTIONAL a Zend_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])) {
         require_once 'Zend/Measure/Exception.php';
         throw new Zend_Measure_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 = Zend_Locale_Format::getInteger($value, array('locale' => $locale));
             } catch (Exception $e) {
                 require_once 'Zend/Measure/Exception.php';
                 throw new Zend_Measure_Exception($e->getMessage());
             }
             if (call_user_func(Zend_Locale_Math::$comp, $value, 0) < 0) {
                 $value = call_user_func(Zend_Locale_Math::$sqrt, call_user_func(Zend_Locale_Math::$pow, $value, 2));
             }
             break;
     }
     $this->_value = $value;
     $this->_type = $type;
 }
Ejemplo n.º 2
0
 /**
  * Set a new value
  *
  * @param  $value  mixed  - Value as string, integer, real or float
  * @param  $type   type   - OPTIONAL a Zend_Measure_Number Type
  * @param  $locale locale - OPTIONAL a Zend_Locale Type
  * @throws Zend_Measure_Exception
  */
 public function setValue($value, $type, $locale = false)
 {
     if (empty($locale)) {
         $locale = $this->_Locale;
     }
     if (empty(self::$_UNITS[$type])) {
         throw Zend::exception('Zend_Measure_Exception', 'unknown type of number:' . $type);
     }
     switch ($type) {
         case 'Number::BINARY':
             preg_match('/[01]+/', $value, $ergebnis);
             $value = $ergebnis[0];
             break;
         case 'Number::TERNARY':
             preg_match('/[012]+/', $value, $ergebnis);
             $value = $ergebnis[0];
             break;
         case 'Number::QUATERNARY':
             preg_match('/[0123]+/', $value, $ergebnis);
             $value = $ergebnis[0];
             break;
         case 'Number::QUINARY':
             preg_match('/[01234]+/', $value, $ergebnis);
             $value = $ergebnis[0];
             break;
         case 'Number::SENARY':
             preg_match('/[012345]+/', $value, $ergebnis);
             $value = $ergebnis[0];
             break;
         case 'Number::SEPTENARY':
             preg_match('/[0123456]+/', $value, $ergebnis);
             $value = $ergebnis[0];
             break;
         case 'Number::OCTAL':
             preg_match('/[01234567]+/', $value, $ergebnis);
             $value = $ergebnis[0];
             break;
         case 'Number::NONARY':
             preg_match('/[012345678]+/', $value, $ergebnis);
             $value = $ergebnis[0];
             break;
         case 'Number::DUODECIMAL':
             preg_match('/[0123456789AB]+/', strtoupper($value), $ergebnis);
             $value = $ergebnis[0];
             break;
         case 'Number::HEXADECIMAL':
             preg_match('/[0123456789ABCDEF]+/', strtoupper($value), $ergebnis);
             $value = $ergebnis[0];
             break;
         case 'Number::ROMAN':
             preg_match('/[IVXLCDM_]+/', strtoupper($value), $ergebnis);
             $value = $ergebnis[0];
             break;
         default:
             try {
                 $value = Zend_Locale_Format::getInteger($value, $locale);
             } catch (Exception $e) {
                 throw Zend::exception('Zend_Measure_Exception', $e->getMessage());
             }
             if (bccomp($value, 0) < 0) {
                 $value = bcsqrt(bcpow($value, 2));
             }
             break;
     }
     parent::setValue($value, $type, $locale);
     parent::setType($type);
 }
Ejemplo n.º 3
0
    /**
     * test getInteger
     * expected integer
     */
    public function testgetInteger()
    {
        $this->assertEquals(       0, Zend_Locale_Format::getInteger(       0        ));
        $this->assertEquals(-1234567, Zend_Locale_Format::getInteger(-1234567        ));
        $this->assertEquals( 1234567, Zend_Locale_Format::getInteger( 1234567        ));
        $this->assertEquals(       0, Zend_Locale_Format::getInteger(       0.1234567));
        $this->assertEquals(-1234567, Zend_Locale_Format::getInteger(-1234567.12345  ));
        $this->assertEquals( 1234567, Zend_Locale_Format::getInteger( 1234567.12345  ));

        $options = array('locale' => 'de');
        $this->assertEquals(       0, Zend_Locale_Format::getInteger(       '0',         $options));
        $this->assertEquals(-1234567, Zend_Locale_Format::getInteger('-1234567',         $options));
        $this->assertEquals( 1234567, Zend_Locale_Format::getInteger( '1234567',         $options));
        $this->assertEquals(       0, Zend_Locale_Format::getInteger(       '0,1234567', $options));
        $this->assertEquals(-1234567, Zend_Locale_Format::getInteger('-1.234.567,12345', $options));
        $this->assertEquals( 1234567, Zend_Locale_Format::getInteger( '1.234.567,12345', $options));

        $options = array('locale' => 'de_AT');
        $this->assertEquals(       0, Zend_Locale_Format::getInteger(         '0',         $options));
        $this->assertEquals(-1234567, Zend_Locale_Format::getInteger(  '-1234567',         $options));
        $this->assertEquals( 1234567, Zend_Locale_Format::getInteger( '1.234.567',         $options));
        $this->assertEquals(       0, Zend_Locale_Format::getInteger(         '0,1234567', $options));
        $this->assertEquals(-1234567, Zend_Locale_Format::getInteger('-1.234.567,12345',   $options));
        $this->assertEquals( 1234567, Zend_Locale_Format::getInteger( '1.234.567,12345',   $options));
    }
Ejemplo n.º 4
0
 public function testShortNotation()
 {
     $this->assertEquals(0.12345, Zend_Locale_Format::getNumber(0.12345));
     $options = array('locale' => 'de');
     $this->assertEquals(0.12345, Zend_Locale_Format::getNumber(',12345', $options));
     $options = array('locale' => 'de_AT');
     $this->assertEquals(0.12345, Zend_Locale_Format::getNumber(',12345', $options));
     $this->assertEquals('0,75', Zend_Locale_Format::toNumber(0.75, array('locale' => 'de_DE', 'precision' => 2)));
     $this->assertTrue(Zend_Locale_Format::isNumber(',12345', array('locale' => 'de_AT')));
     $this->assertEquals(0.12345, Zend_Locale_Format::getFloat(0.12345));
     $options = array('locale' => 'de');
     $this->assertEquals(0.12345, Zend_Locale_Format::getFloat(',12345', $options));
     $options = array('locale' => 'de_AT');
     $this->assertEquals(0.12345, Zend_Locale_Format::getFloat(',12345', $options));
     $options = array('locale' => 'de_AT');
     $this->assertEquals('0,12345', Zend_Locale_Format::toFloat(0.12345, $options));
     $options = array('locale' => 'ar_QA');
     $this->assertEquals('0,12345', Zend_Locale_Format::toFloat(0.12345, $options));
     $this->assertTrue(Zend_Locale_Format::isFloat(',12345', array('locale' => 'de_AT')));
     $this->assertEquals(0, Zend_Locale_Format::getInteger(0.1234567));
     $options = array('locale' => 'de');
     $this->assertEquals(0, Zend_Locale_Format::getInteger(',12345', $options));
     $options = array('locale' => 'de_AT');
     $this->assertEquals(0, Zend_Locale_Format::getInteger(',12345', $options));
     $this->assertEquals('0', Zend_Locale_Format::toInteger(0.123, array('locale' => 'de')));
     $options = array('locale' => 'de_AT');
     $this->assertEquals('0', Zend_Locale_Format::toInteger(0.12345, $options));
     $this->assertFalse(Zend_Locale_Format::isInteger(',12345', array('locale' => 'de_AT')));
     $options = array('locale' => 'de_AT');
     $this->assertEquals('0,567', Zend_Locale_Format::toNumber(0.5669999999999999, $options));
 }
Ejemplo n.º 5
0
 /**
  * test getInteger
  * expected integer
  */
 public function testgetInteger()
 {
     $this->assertEquals(Zend_Locale_Format::getInteger(0), 0, "value 0 expected");
     $this->assertEquals(Zend_Locale_Format::getInteger(-1234567), -1234567, "value -1234567 expected");
     $this->assertEquals(Zend_Locale_Format::getInteger(1234567), 1234567, "value 1234567 expected");
     $this->assertEquals(Zend_Locale_Format::getInteger(0.1234567), 0, "value 0 expected");
     $this->assertEquals(Zend_Locale_Format::getInteger(-1234567.12345), -1234567, "value -1234567 expected");
     $this->assertEquals(Zend_Locale_Format::getInteger(1234567.12345), 1234567, "value 1234567 expected");
     $options = array('locale' => 'de');
     $this->assertEquals(Zend_Locale_Format::getInteger('0', $options), 0, "value 0 expected");
     $this->assertEquals(Zend_Locale_Format::getInteger('-1234567', $options), -1234567, "value -1234567 expected");
     $this->assertEquals(Zend_Locale_Format::getInteger('1234567', $options), 1234567, "value 1234567 expected");
     $this->assertEquals(Zend_Locale_Format::getInteger('0,1234567', $options), 0, "value 0 expected");
     $this->assertEquals(Zend_Locale_Format::getInteger('-1.234.567,12345', $options), -1234567, "value -1234567 expected");
     $this->assertEquals(Zend_Locale_Format::getInteger('1.234.567,12345', $options), 1234567, "value 1234567 expected");
     $options = array('locale' => 'de_AT');
     $this->assertEquals(Zend_Locale_Format::getInteger('0', $options), 0, "value 0 expected");
     $this->assertEquals(Zend_Locale_Format::getInteger('-1234567', $options), -1234567, "value -1234567 expected");
     $this->assertEquals(Zend_Locale_Format::getInteger('1.234.567', $options), 1234567, "value 1234567 expected");
     $this->assertEquals(Zend_Locale_Format::getInteger('0,1234567', $options), 0, "value 0 expected");
     $this->assertEquals(Zend_Locale_Format::getInteger('-1.234.567,12345', $options), -1234567, "value -1234567 expected");
     $this->assertEquals(Zend_Locale_Format::getInteger('1.234.567,12345', $options), 1234567, "value 1234567 expected");
 }
Ejemplo n.º 6
0
 /**
  * test positive seperation language locale
  * expected integer
  */
 public function testIntegerRegionSeperatedPositive()
 {
     $value = Zend_Locale_Format::getInteger('1.234.567,12345', 'de_AT');
     $this->assertEquals($value, 1234567, "value 1234567 expected");
 }
Ejemplo n.º 7
0
 /**
  * Reads a localized value, normalizes and returns it.
  * Returns an empty string if no value is passed.
  * @param mixed $value
  * @param string $type (boolean|date|time|integer|float|decimal|currency)
  * @return mixed
  */
 private function _normalize($value, $type, $num_of_decimals)
 {
     if (strlen($value) == 0) {
         return '';
     }
     switch ($type) {
         case 'boolean':
             $yes_no = Zend_Locale_Data::getContent($this->_locale_engine, 'questionstrings');
             $yes_regexp = '/^(' . str_replace(':', '|', $yes_no['yes']) . ')$/i';
             if (preg_match($yes_regexp, $value)) {
                 return 1;
             }
             return 0;
         case 'date':
             $date = Zend_Locale_Format::getDate($value, array('locale' => $this->_locale_engine, 'fix_date' => true));
             $date['month'] = str_pad($date['month'], 2, 0, STR_PAD_LEFT);
             $date['day'] = str_pad($date['day'], 2, 0, STR_PAD_LEFT);
             return "{$date['year']}-{$date['month']}-{$date['day']}";
         case 'time':
             $date_format = Zend_Locale_Format::getTimeFormat($this->_locale_engine);
             $date = Zend_Locale_Format::getDate($value, array('date_format' => $date_format, 'locale' => $this->_locale_engine));
             if (!isset($date['hour'])) {
                 $date['hour'] = '00';
             }
             if (!isset($date['minute'])) {
                 $date['minute'] = '00';
             }
             if (!isset($date['second'])) {
                 $date['second'] = '00';
             }
             return "{$date['hour']}:{$date['minute']}:{$date['second']}";
         case 'datetime':
             $date = Zend_Locale_Format::getDateTime($value, array('locale' => $this->_locale_engine, 'fix_date' => true));
             $date['month'] = str_pad($date['month'], 2, 0, STR_PAD_LEFT);
             $date['day'] = str_pad($date['day'], 2, 0, STR_PAD_LEFT);
             $date['hour'] = str_pad($date['hour'], 2, 0, STR_PAD_LEFT);
             $date['minute'] = str_pad($date['minute'], 2, 0, STR_PAD_LEFT);
             $date['second'] = str_pad($date['second'], 2, 0, STR_PAD_LEFT);
             return "{$date['year']}-{$date['month']}-{$date['day']} {$date['hour']}:{$date['minute']}:{$date['second']}";
             break;
         case 'integer':
             return Zend_Locale_Format::getInteger($value, array('locale' => $this->_locale_engine));
         case 'float':
             if ($num_of_decimals === null) {
                 $num_of_decimals = 3;
             }
             return Zend_Locale_Format::getFloat($value, array('precision' => $num_of_decimals, 'locale' => $this->_locale_engine));
         case 'decimal':
             if ($num_of_decimals === null) {
                 $num_of_decimals = 2;
             }
             return Zend_Locale_Format::getFloat($value, array('precision' => $num_of_decimals, 'locale' => $this->_locale_engine));
     }
     return $value;
 }
Ejemplo n.º 8
0
 /**
  * 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 ($this->_options['date_format'] === null && strpos($value, ':') !== false) {
         // Special case, no date format specified, detect time input
         return Zend_Locale_Format::getTime($value, $this->_options);
     } else {
         if (Zend_Locale_Format::checkDateFormat($value, $this->_options)) {
             // Detect date or time input
             return Zend_Locale_Format::getDate($value, $this->_options);
         } else {
             if ($this->_options['precision'] === 0 && Zend_Locale_Format::isInteger($value, $this->_options)) {
                 // Detect integer
                 return Zend_Locale_Format::getInteger($value, $this->_options);
             } else {
                 if ($this->_options['precision'] === null && Zend_Locale_Format::isFloat($value, $this->_options)) {
                     // Detect float
                     return Zend_Locale_Format::getFloat($value, $this->_options);
                 } else {
                     if (Zend_Locale_Format::isNumber($value, $this->_options)) {
                         // Detect all other numbers
                         return Zend_Locale_Format::getNumber($value, $this->_options);
                     }
                 }
             }
         }
     }
     return $value;
 }