Source of file NumberParse.php
Size: 3,792 Bytes - Last Modified: 2014-03-12T23:21:18+01:00
/home/theseer/Downloads/ZendFramework-2.3.0/library/Zend/I18n/Filter/NumberParse.php
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164 | <?php /** * Zend Framework (http://framework.zend.com/) * * @link http://github.com/zendframework/zf2 for the canonical source repository * @copyright Copyright (c) 2005-2014 Zend Technologies USA Inc. (http://www.zend.com) * @license http://framework.zend.com/license/new-bsd New BSD License */ namespace Zend\I18n\Filter; use NumberFormatter; use Traversable; use Zend\I18n\Exception; use Zend\Stdlib\ErrorHandler; class NumberParse extends AbstractLocale { protected $options = array( 'locale' => null, 'style' => NumberFormatter::DEFAULT_STYLE, 'type' => NumberFormatter::TYPE_DOUBLE ); /** * @var NumberFormatter */ protected $formatter = null; /** * @param array|Traversable|string|null $localeOrOptions * @param int $style * @param int $type */ public function __construct( $localeOrOptions = null, $style = NumberFormatter::DEFAULT_STYLE, $type = NumberFormatter::TYPE_DOUBLE ) { parent::__construct(); if ($localeOrOptions !== null) { if ($localeOrOptions instanceof Traversable) { $localeOrOptions = iterator_to_array($localeOrOptions); } if (!is_array($localeOrOptions)) { $this->setLocale($localeOrOptions); $this->setStyle($style); $this->setType($type); } else { $this->setOptions($localeOrOptions); } } } /** * @param string|null $locale * @return NumberFormat */ public function setLocale($locale = null) { $this->options['locale'] = $locale; $this->formatter = null; return $this; } /** * @param int $style * @return NumberFormat */ public function setStyle($style) { $this->options['style'] = (int) $style; $this->formatter = null; return $this; } /** * @return int */ public function getStyle() { return $this->options['style']; } /** * @param int $type * @return NumberFormat */ public function setType($type) { $this->options['type'] = (int) $type; return $this; } /** * @return int */ public function getType() { return $this->options['type']; } /** * @param NumberFormatter $formatter * @return NumberFormat */ public function setFormatter(NumberFormatter $formatter) { $this->formatter = $formatter; return $this; } /** * @return NumberFormatter * @throws Exception\RuntimeException */ public function getFormatter() { if ($this->formatter === null) { $formatter = NumberFormatter::create($this->getLocale(), $this->getStyle()); if (!$formatter) { throw new Exception\RuntimeException( 'Can not create NumberFormatter instance; ' . intl_get_error_message() ); } $this->formatter = $formatter; } return $this->formatter; } /** * Defined by Zend\Filter\FilterInterface * * @see Zend\Filter\FilterInterface::filter() * @param mixed $value * @return mixed */ public function filter($value) { if (!is_int($value) && !is_float($value) ) { ErrorHandler::start(); $result = $this->getFormatter()->parse( $value, $this->getType() ); ErrorHandler::stop(); if (false !== $result) { return $result; } } return $value; } } |