/** * 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; }
/** * Returns the calculated time * * @param string $calc Calculation to make * @param string|integer|array|\Zend\Date\Date $time Time to calculate with, if null the actual time is taken * @param string $format Timeformat for parsing input * @param string|\Zend\Locale\Locale $locale Locale for parsing input * @return integer|\Zend\Date\Date new time * @throws \Zend\Date\Exception */ private function _time($calc, $time, $format, $locale) { if ($time === null) { throw new Exception\InvalidArgumentException('parameter $time must be set, null is not allowed'); } if ($time instanceof Date) { // extract time from object $time = $time->toString('HH:mm:ss', 'iso'); } else { if (is_array($time)) { if (isset($time['hour']) === true or isset($time['minute']) === true or isset($time['second']) === true) { $parsed = $time; } else { throw new Exception\InvalidArgumentException("no hour, minute or second given in array"); } } else { if (self::$_options['format_type'] == 'php') { $format = Format::convertPhpToIsoFormat($format); } try { if ($locale === null) { $locale = $this->getLocale(); } $parsed = Format::getTime($time, array('date_format' => $format, 'locale' => $locale, 'format_type' => 'iso')); } catch (\Zend\Locale\Exception $e) { throw new Exception\InvalidArgumentException($e->getMessage(), 0, $e); } } if (!array_key_exists('hour', $parsed)) { $parsed['hour'] = 0; } if (!array_key_exists('minute', $parsed)) { $parsed['minute'] = 0; } if (!array_key_exists('second', $parsed)) { $parsed['second'] = 0; } $time = str_pad($parsed['hour'], 2, '0', STR_PAD_LEFT) . ":"; $time .= str_pad($parsed['minute'], 2, '0', STR_PAD_LEFT) . ":"; $time .= str_pad($parsed['second'], 2, '0', STR_PAD_LEFT); } $return = $this->_calcdetail($calc, $time, self::TIMES, 'de'); if ($calc != 'cmp') { return $this; } return $return; }
/** * test getTime * expected array */ public function testgetTime() { try { $value = Format::getTime('no content'); $this->fail("exception expected"); } catch (InvalidArgumentException $e) { // success } $this->assertTrue(is_array(Format::getTime('13:14:55', array('date_format' => 'HH:mm:ss')))); $options = array('date_format' => 'h:mm:ss a', 'locale' => 'en'); $this->assertTrue(is_array(Format::getTime('11:14:55 am', $options))); $this->assertTrue(is_array(Format::getTime('12:14:55 am', $options))); $this->assertTrue(is_array(Format::getTime('11:14:55 pm', $options))); $this->assertTrue(is_array(Format::getTime('12:14:55 pm', $options))); try { $value = Format::getTime('13:14:55', array('date_format' => 'nocontent')); $this->fail("exception expected"); } catch (InvalidArgumentException $e) { // success } try { $value = Format::getTime('13:14:55', array('date_format' => 'ZZZZ')); $this->fail("exception expected"); } catch (InvalidArgumentException $e) { // success } $value = Format::getTime('13:14:55', array('date_format' => 'HH:mm:ss.x')); $this->assertEquals(13, $value['hour']); $this->assertEquals(14, $value['minute']); $this->assertEquals(55, $value['second']); $this->assertEquals(5, count(Format::getTime('13:14:55', array('date_format' => 'HH:mm:ss')))); $value = Format::getTime('13:14:55', array('date_format' => 'HH:mm:ss')); $this->assertEquals(13, $value['hour']); $this->assertEquals(14, $value['minute']); $this->assertEquals(55, $value['second']); $value = Format::getTime('131455', array('date_format' => 'HH:mm:ss')); $this->assertEquals(13, $value['hour']); $this->assertEquals(14, $value['minute']); $this->assertEquals(55, $value['second']); }