This repository has been archived by the owner on Jan 30, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 49
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'feature/filters-refactor' of https://github.com/denixpo…
…rt/zf2 into feature/filter-i18n
- Loading branch information
Showing
3 changed files
with
338 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
<?php | ||
/** | ||
* Zend Framework | ||
* | ||
* LICENSE | ||
* | ||
* This source file is subject to the new BSD license that is bundled | ||
* with this package in the file LICENSE.txt. | ||
* It is also available through the world-wide-web at this URL: | ||
* http://framework.zend.com/license/new-bsd | ||
* If you did not receive a copy of the license and are unable to | ||
* obtain it through the world-wide-web, please send an email | ||
* to [email protected] so we can send you a copy immediately. | ||
* | ||
* @category Zend | ||
* @package Zend_Filter | ||
* @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com) | ||
* @license http://framework.zend.com/license/new-bsd New BSD License | ||
*/ | ||
|
||
namespace Zend\I18n\Filter; | ||
|
||
use Zend\ServiceManager\AbstractPluginManager; | ||
use Zend\Filter\FilterInterface; | ||
use Zend\I18n\Exception; | ||
|
||
/** | ||
* Plugin manager implementation for the filter chain. | ||
* | ||
* Enforces that filters retrieved are either callbacks or instances of | ||
* FilterInterface. Additionally, it registers a number of default filters | ||
* available, as well as aliases for them. | ||
* | ||
* @category Zend | ||
* @package Zend_Filter | ||
* @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com) | ||
* @license http://framework.zend.com/license/new-bsd New BSD License | ||
*/ | ||
class FilterPluginManager extends AbstractPluginManager | ||
{ | ||
/** | ||
* Default set of filters | ||
* | ||
* @var array | ||
*/ | ||
protected $invokableClasses = array( | ||
'numberformat' => 'Zend\I18n\Filter\NumberFormat', | ||
); | ||
|
||
/** | ||
* Validate the plugin | ||
* | ||
* Checks that the filter loaded is either a valid callback or an instance | ||
* of FilterInterface. | ||
* | ||
* @param mixed $plugin | ||
* @return void | ||
* @throws Exception\RuntimeException if invalid | ||
*/ | ||
public function validatePlugin($plugin) | ||
{ | ||
if ($plugin instanceof FilterInterface) { | ||
// we're okay | ||
return; | ||
} | ||
if (is_callable($plugin)) { | ||
// also okay | ||
return; | ||
} | ||
|
||
throw new Exception\RuntimeException(sprintf( | ||
'Plugin of type %s is invalid; must implement %s\FilterInterface or be callable', | ||
(is_object($plugin) ? get_class($plugin) : gettype($plugin)), | ||
__NAMESPACE__ | ||
)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,169 @@ | ||
<?php | ||
/** | ||
* Zend Framework | ||
* | ||
* LICENSE | ||
* | ||
* This source file is subject to the new BSD license that is bundled | ||
* with this package in the file LICENSE.txt. | ||
* It is also available through the world-wide-web at this URL: | ||
* http://framework.zend.com/license/new-bsd | ||
* If you did not receive a copy of the license and are unable to | ||
* obtain it through the world-wide-web, please send an email | ||
* to [email protected] so we can send you a copy immediately. | ||
* | ||
* @category Zend | ||
* @package Zend_I18n | ||
* @subpackage Filter | ||
* @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com) | ||
* @license http://framework.zend.com/license/new-bsd New BSD License | ||
*/ | ||
|
||
namespace Zend\I18n\Filter; | ||
|
||
use Zend\Filter\AbstractLocale; | ||
use Zend\I18n\Exception; | ||
use NumberFormatter; | ||
use Traversable; | ||
|
||
class NumberFormat extends AbstractLocale | ||
{ | ||
protected $options = array( | ||
'locale' => null, | ||
'style' => NumberFormatter::DEFAULT_STYLE, | ||
'type' => NumberFormatter::TYPE_DOUBLE | ||
); | ||
|
||
/** | ||
* @var NumberFormatter | ||
*/ | ||
protected $formatter = null; | ||
|
||
public function __construct($options = null) | ||
{ | ||
if ($options !== null) { | ||
if ($options instanceof Traversable) { | ||
$options = iterator_to_array($options); | ||
} | ||
|
||
if (!is_array($options)) { | ||
$args = func_get_args(); | ||
if (isset($args[0])) { | ||
$this->setLocale($args[0]); | ||
} | ||
if (isset($args[1])) { | ||
$this->setStyle($args[1]); | ||
} | ||
if (isset($args[2])) { | ||
$this->setType($args[2]); | ||
} | ||
} else { | ||
$this->setOptions($options); | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* @param 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) | ||
{ | ||
$formatter = $this->getFormatter(); | ||
$type = $this->getType(); | ||
|
||
if (is_int($value) || is_float($value)) { | ||
$result = @numfmt_format($formatter, $value, $type); | ||
} else { | ||
$value = str_replace(array("\xC2\xA0", ' '), '', $value); | ||
$result = @numfmt_parse($formatter, $value, $type); | ||
} | ||
|
||
if ($result === false) { | ||
return $value; | ||
} | ||
|
||
return str_replace("\xC2\xA0", ' ', $result); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
<?php | ||
|
||
namespace ZendTest\I18n\Filter; | ||
|
||
use PHPUnit_Framework_TestCase as TestCase; | ||
use Zend\I18n\Filter\NumberFormat as NumberFormatFilter; | ||
use NumberFormatter; | ||
|
||
class NumberFormatTest extends TestCase | ||
{ | ||
/** | ||
* @param $locale | ||
* @param $style | ||
* @param $type | ||
* @param $value | ||
* @param $expected | ||
* @dataProvider numberToFormattedProvider | ||
*/ | ||
public function testNumberToFormatted($locale, $style, $type, $value, $expected) | ||
{ | ||
$filter = new NumberFormatFilter($locale, $style, $type); | ||
$this->assertEquals($expected, $filter->filter($value)); | ||
} | ||
|
||
/** | ||
* @param $locale | ||
* @param $style | ||
* @param $type | ||
* @param $value | ||
* @param $expected | ||
* @dataProvider formattedToNumberProvider | ||
*/ | ||
public function testFormattedToNumber($locale, $style, $type, $value, $expected) | ||
{ | ||
$filter = new NumberFormatFilter($locale, $style, $type); | ||
$this->assertEquals($expected, $filter->filter($value)); | ||
} | ||
|
||
static public function numberToFormattedProvider() | ||
{ | ||
return array( | ||
array( | ||
'en_US', | ||
null, | ||
null, | ||
1234567.8912346, | ||
'1,234,567.891' | ||
), | ||
array( | ||
'de_DE', | ||
null, | ||
null, | ||
1234567.8912346, | ||
'1.234.567,891' | ||
), | ||
array( | ||
'ru_RU', | ||
null, | ||
null, | ||
1234567.8912346, | ||
'1 234 567,891' | ||
), | ||
); | ||
} | ||
|
||
static public function formattedToNumberProvider() | ||
{ | ||
return array( | ||
array( | ||
'en_US', | ||
null, | ||
null, | ||
'1,234,567.891', | ||
1234567.891, | ||
), | ||
array( | ||
'de_DE', | ||
null, | ||
null, | ||
'1.234.567,891', | ||
1234567.891, | ||
), | ||
array( | ||
'ru_RU', | ||
null, | ||
null, | ||
'1 234 567,891', | ||
1234567.891, | ||
), | ||
); | ||
} | ||
} |