Skip to content
This repository has been archived by the owner on Jan 30, 2020. It is now read-only.

Commit

Permalink
Merge pull request zendframework/zendframework#4510 from localheinz/f…
Browse files Browse the repository at this point in the history
…eature/number-filter

Introduce Zend\I18n\Filter\NumberParse based on Zend\I18n\Filter\NumberFormat
  • Loading branch information
weierophinney committed May 23, 2013
Show file tree
Hide file tree
Showing 4 changed files with 256 additions and 129 deletions.
144 changes: 16 additions & 128 deletions src/Filter/NumberFormat.php
Expand Up @@ -9,127 +9,11 @@

namespace Zend\I18n\Filter;

use NumberFormatter;
use Traversable;
use Zend\I18n\Exception;
use Zend\Stdlib\ErrorHandler;

class NumberFormat extends AbstractLocale
class NumberFormat extends NumberParse
{
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
Expand All @@ -140,24 +24,28 @@ public function getFormatter()
*/
public function filter($value)
{
$formatter = $this->getFormatter();
$type = $this->getType();
if (!is_int($value)
&& !is_float($value)) {

$result = parent::filter($value);

if (is_int($value) || is_float($value)) {
ErrorHandler::start();
$result = $formatter->format($value, $type);
ErrorHandler::stop();
} else {
$value = str_replace(array("\xC2\xA0", ' '), '', $value);

ErrorHandler::start();
$result = $formatter->parse($value, $type);

$result = $this->getFormatter()->format(
$value,
$this->getType()
);

ErrorHandler::stop();

}

if ($result === false) {
return $value;
if (false !== $result) {
return str_replace("\xC2\xA0", ' ', $result);
}

return str_replace("\xC2\xA0", ' ', $result);
return $value;
}
}
163 changes: 163 additions & 0 deletions src/Filter/NumberParse.php
@@ -0,0 +1,163 @@
<?php
/**
* Zend Framework (http://framework.zend.com/)
*
* @link http://github.com/zendframework/zf2 for the canonical source repository
* @copyright Copyright (c) 2005-2013 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;
}
}
1 change: 0 additions & 1 deletion test/Filter/NumberFormatTest.php
Expand Up @@ -5,7 +5,6 @@
* @link http://github.com/zendframework/zf2 for the canonical source repository
* @copyright Copyright (c) 2005-2013 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
* @package Zend_I18n
*/

namespace ZendTest\I18n\Filter;
Expand Down

0 comments on commit ad3ef4c

Please sign in to comment.