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 'i18n-number-validators' of https://github.com/cgmartin/zf2
- Loading branch information
Showing
4 changed files
with
639 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,145 @@ | ||
<?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 license@zend.com so we can send you a copy immediately. | ||
* | ||
* @category Zend | ||
* @package Zend_Validate | ||
* @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\Validator; | ||
|
||
use Locale; | ||
use NumberFormatter; | ||
use Traversable; | ||
use Zend\Stdlib\ArrayUtils; | ||
use Zend\Validator\AbstractValidator; | ||
use Zend\Validator\Exception; | ||
|
||
/** | ||
* @category Zend | ||
* @package Zend_Validate | ||
* @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 Float extends AbstractValidator | ||
{ | ||
const INVALID = 'floatInvalid'; | ||
const NOT_FLOAT = 'notFloat'; | ||
|
||
/** | ||
* @var array | ||
*/ | ||
protected $messageTemplates = array( | ||
self::INVALID => "Invalid type given. String, integer or float expected", | ||
self::NOT_FLOAT => "The input does not appear to be a float", | ||
); | ||
|
||
/** | ||
* Optional locale | ||
* | ||
* @var string|null | ||
*/ | ||
protected $locale; | ||
|
||
/** | ||
* Constructor for the integer validator | ||
* | ||
* @param array|Traversable $options | ||
*/ | ||
public function __construct($options = array()) | ||
{ | ||
if ($options instanceof Traversable) { | ||
$options = ArrayUtils::iteratorToArray($options); | ||
} | ||
|
||
if (array_key_exists('locale', $options)) { | ||
$this->setLocale($options['locale']); | ||
} | ||
|
||
parent::__construct($options); | ||
} | ||
|
||
/** | ||
* Returns the set locale | ||
* | ||
* @return string | ||
*/ | ||
public function getLocale() | ||
{ | ||
if (null === $this->locale) { | ||
$this->locale = Locale::getDefault(); | ||
} | ||
return $this->locale; | ||
} | ||
|
||
/** | ||
* Sets the locale to use | ||
* | ||
* @param string|null $locale | ||
* @return Float | ||
*/ | ||
public function setLocale($locale) | ||
{ | ||
$this->locale = $locale; | ||
return $this; | ||
} | ||
|
||
|
||
/** | ||
* Returns true if and only if $value is a floating-point value | ||
* | ||
* @param string $value | ||
* @return boolean | ||
* @throws Exception\InvalidArgumentException | ||
*/ | ||
public function isValid($value) | ||
{ | ||
if (!is_string($value) && !is_int($value) && !is_float($value)) { | ||
$this->error(self::INVALID); | ||
return false; | ||
} | ||
|
||
$this->setValue($value); | ||
|
||
if (is_float($value)) { | ||
return true; | ||
} | ||
|
||
$locale = $this->getLocale(); | ||
$format = new NumberFormatter($locale, NumberFormatter::DECIMAL); | ||
if (intl_is_failure($format->getErrorCode())) { | ||
throw new Exception\InvalidArgumentException("Invalid locale string given"); | ||
} | ||
|
||
$parsedFloat = $format->parse($value, NumberFormatter::TYPE_DOUBLE); | ||
if (intl_is_failure($format->getErrorCode())) { | ||
$this->error(self::NOT_FLOAT); | ||
return false; | ||
} | ||
|
||
$decimalSep = $format->getSymbol(NumberFormatter::DECIMAL_SEPARATOR_SYMBOL); | ||
$groupingSep = $format->getSymbol(NumberFormatter::GROUPING_SEPARATOR_SYMBOL); | ||
|
||
$valueFiltered = str_replace($groupingSep, '', $value); | ||
$valueFiltered = str_replace($decimalSep, '.', $valueFiltered); | ||
|
||
if (strval($parsedFloat) !== $valueFiltered) { | ||
$this->error(self::NOT_FLOAT); | ||
return false; | ||
} | ||
|
||
return true; | ||
} | ||
} |
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,142 @@ | ||
<?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 license@zend.com so we can send you a copy immediately. | ||
* | ||
* @category Zend | ||
* @package Zend_Validate | ||
* @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\Validator; | ||
|
||
use Locale; | ||
use NumberFormatter; | ||
use Traversable; | ||
use Zend\Stdlib\ArrayUtils; | ||
use Zend\Validator\AbstractValidator; | ||
use Zend\Validator\Exception; | ||
|
||
/** | ||
* @category Zend | ||
* @package Zend_Validate | ||
* @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 Int extends AbstractValidator | ||
{ | ||
const INVALID = 'intInvalid'; | ||
const NOT_INT = 'notInt'; | ||
|
||
/** | ||
* @var array | ||
*/ | ||
protected $messageTemplates = array( | ||
self::INVALID => "Invalid type given. String or integer expected", | ||
self::NOT_INT => "The input does not appear to be an integer", | ||
); | ||
|
||
/** | ||
* Optional locale | ||
* | ||
* @var string|null | ||
*/ | ||
protected $locale; | ||
|
||
/** | ||
* Constructor for the integer validator | ||
* | ||
* @param array|Traversable $options | ||
*/ | ||
public function __construct($options = array()) | ||
{ | ||
if ($options instanceof Traversable) { | ||
$options = ArrayUtils::iteratorToArray($options); | ||
} | ||
|
||
if (array_key_exists('locale', $options)) { | ||
$this->setLocale($options['locale']); | ||
} | ||
|
||
parent::__construct($options); | ||
} | ||
|
||
/** | ||
* Returns the set locale | ||
*/ | ||
public function getLocale() | ||
{ | ||
if (null === $this->locale) { | ||
$this->locale = Locale::getDefault(); | ||
} | ||
return $this->locale; | ||
} | ||
|
||
/** | ||
* Sets the locale to use | ||
* | ||
* @param string $locale | ||
* @return Int | ||
*/ | ||
public function setLocale($locale) | ||
{ | ||
$this->locale = $locale; | ||
return $this; | ||
} | ||
|
||
/** | ||
* Returns true if and only if $value is a valid integer | ||
* | ||
* @param string|integer $value | ||
* @return boolean | ||
* @throws Exception\InvalidArgumentException | ||
*/ | ||
public function isValid($value) | ||
{ | ||
if (!is_string($value) && !is_int($value) && !is_float($value)) { | ||
$this->error(self::INVALID); | ||
return false; | ||
} | ||
|
||
if (is_int($value)) { | ||
return true; | ||
} | ||
|
||
$this->setValue($value); | ||
|
||
$locale = $this->getLocale(); | ||
$format = new NumberFormatter($locale, NumberFormatter::DECIMAL); | ||
if (intl_is_failure($format->getErrorCode())) { | ||
throw new Exception\InvalidArgumentException("Invalid locale string given"); | ||
} | ||
|
||
$parsedInt = $format->parse($value, NumberFormatter::TYPE_INT64); | ||
if (intl_is_failure($format->getErrorCode())) { | ||
$this->error(self::NOT_INT); | ||
return false; | ||
} | ||
|
||
$decimalSep = $format->getSymbol(NumberFormatter::DECIMAL_SEPARATOR_SYMBOL); | ||
$groupingSep = $format->getSymbol(NumberFormatter::GROUPING_SEPARATOR_SYMBOL); | ||
|
||
$valueFiltered = str_replace($groupingSep, '', $value); | ||
$valueFiltered = str_replace($decimalSep, '.', $valueFiltered); | ||
|
||
if (strval($parsedInt) !== $valueFiltered) { | ||
$this->error(self::NOT_INT); | ||
return false; | ||
} | ||
|
||
return true; | ||
} | ||
} |
Oops, something went wrong.