Skip to content

Commit

Permalink
Merge branch 'hotfix/zendframework#6598-catch-intl-exceptions'
Browse files Browse the repository at this point in the history
  • Loading branch information
Ocramius committed Jan 3, 2015
2 parents 38782ac + b2e9d8d commit 19b8cba
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 12 deletions.
22 changes: 17 additions & 5 deletions library/Zend/I18n/Validator/DateTime.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use Locale;
use IntlDateFormatter;
use Traversable;
use IntlException;
use Zend\I18n\Exception as I18nException;
use Zend\Validator\AbstractValidator;
use Zend\Validator\Exception as ValidatorException;
Expand Down Expand Up @@ -263,15 +264,26 @@ public function isValid($value)
}

$this->setValue($value);
$formatter = $this->getIntlDateFormatter();

if (intl_is_failure($formatter->getErrorCode())) {
throw new ValidatorException\InvalidArgumentException($formatter->getErrorMessage());
try {
$formatter = $this->getIntlDateFormatter();

if (intl_is_failure($formatter->getErrorCode())) {
throw new ValidatorException\InvalidArgumentException($formatter->getErrorMessage());
}
} catch (IntlException $intlException) {
throw new ValidatorException\InvalidArgumentException($e->getMessage(), 0, $intlException);
}

$timestamp = $formatter->parse($value);

if (intl_is_failure($formatter->getErrorCode()) || $timestamp === false) {
try {
$timestamp = $formatter->parse($value);

if (intl_is_failure($formatter->getErrorCode()) || $timestamp === false) {
$this->error(self::INVALID_DATETIME);
return false;
}
} catch (IntlException $intlException) {
$this->error(self::INVALID_DATETIME);
return false;
}
Expand Down
9 changes: 7 additions & 2 deletions library/Zend/I18n/Validator/Float.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use Locale;
use NumberFormatter;
use Traversable;
use IntlException;
use Zend\I18n\Exception as I18nException;
use Zend\Stdlib\ArrayUtils;
use Zend\Stdlib\StringUtils;
Expand Down Expand Up @@ -122,8 +123,12 @@ public function isValid($value)
// Need to check if this is scientific formatted string. If not, switch to decimal.
$formatter = new NumberFormatter($this->getLocale(), NumberFormatter::SCIENTIFIC);

if (intl_is_failure($formatter->getErrorCode())) {
throw new Exception\InvalidArgumentException($formatter->getErrorMessage());
try {
if (intl_is_failure($formatter->getErrorCode())) {
throw new Exception\InvalidArgumentException($formatter->getErrorMessage());
}
} catch (IntlException $intlException) {
throw new Exception\InvalidArgumentException($e->getMessage(), 0, $intlException);
}

if (StringUtils::hasPcreUnicodeSupport()) {
Expand Down
21 changes: 16 additions & 5 deletions library/Zend/I18n/Validator/Int.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,13 @@
use Locale;
use NumberFormatter;
use Traversable;
use IntlException;
use Zend\I18n\Exception as I18nException;
use Zend\Stdlib\ArrayUtils;
use Zend\Validator\AbstractValidator;
use Zend\Validator\Exception;


class Int extends AbstractValidator
{
const INVALID = 'intInvalid';
Expand Down Expand Up @@ -107,13 +109,22 @@ public function isValid($value)
$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");
try {
$format = new NumberFormatter($locale, NumberFormatter::DECIMAL);
if (intl_is_failure($format->getErrorCode())) {
throw new Exception\InvalidArgumentException("Invalid locale string given");
}
} catch (IntlException $intlException) {
throw new Exception\InvalidArgumentException("Invalid locale string given", 0, $intlException);
}

$parsedInt = $format->parse($value, NumberFormatter::TYPE_INT64);
if (intl_is_failure($format->getErrorCode())) {
try {
$parsedInt = $format->parse($value, NumberFormatter::TYPE_INT64);
if (intl_is_failure($format->getErrorCode())) {
$this->error(self::NOT_INT);
return false;
}
} catch (IntlException $intlException) {
$this->error(self::NOT_INT);
return false;
}
Expand Down

0 comments on commit 19b8cba

Please sign in to comment.