LocalizedNumeric.php 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. <?php
  2. /**
  3. *
  4. * PHP Pro Bid $Id$ YiS2qE8mFFUB1lpn9ufd2/gZfLszl9WpY+Szb5+Qd34=
  5. *
  6. * @link http://www.phpprobid.com
  7. * @copyright Copyright (c) 2016 Online Ventures Software & CodeCube SRL
  8. * @license http://www.phpprobid.com/license Commercial License
  9. *
  10. * @version 7.8
  11. */
  12. /**
  13. * localized numeric only validator class
  14. *
  15. * based on the amount format selected in admin, will accept:
  16. *
  17. * DEFAULT: 1234567.90
  18. * and
  19. * US: 1,234,567.90
  20. * or
  21. * EU: 1.234.567,90
  22. */
  23. namespace Ppb\Validate;
  24. use Cube\Validate\Numeric,
  25. Cube\Locale\Format as LocaleFormat;
  26. class LocalizedNumeric extends Numeric
  27. {
  28. protected $_message = "'%s' must contain a localized numeric value.";
  29. /**
  30. *
  31. * checks if the variable contains a localized numeric value
  32. *
  33. * @return bool return true if the validation is successful
  34. */
  35. public function isValid()
  36. {
  37. $value = $this->getValue();
  38. if (empty($value)) {
  39. return true;
  40. }
  41. $value = LocaleFormat::getInstance()->localizedToNumeric($value);
  42. if ($value !== false) {
  43. if (preg_match('#^-?\d*\.?\d+$#', $value)) {
  44. return true;
  45. }
  46. }
  47. return false;
  48. }
  49. }