FlatRatesPostage.php 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. <?php
  2. /**
  3. *
  4. * PHP Pro Bid $Id$ 2AdOPQ31IORBMAbg8Rmn+R4VbZW7h+520TXKZwbErKU=
  5. *
  6. * @link http://www.phpprobid.com
  7. * @copyright Copyright (c) 2017 Online Ventures Software & CodeCube SRL
  8. * @license http://www.phpprobid.com/license Commercial License
  9. *
  10. * @version 7.10 [rev.7.10.01]
  11. */
  12. /**
  13. * flat rates location groups postage validator class
  14. */
  15. namespace Ppb\Validate;
  16. use Cube\Validate\AbstractValidate,
  17. Ppb\Form\Element\FlatRatesLocationGroups;
  18. class FlatRatesPostage extends AbstractValidate
  19. {
  20. const NO_POSTAGE = 1;
  21. const PRICE_NOT_NUMERIC = 2;
  22. protected $_messages = array(
  23. self::NO_POSTAGE => "'%s' is required and cannot be empty.",
  24. self::PRICE_NOT_NUMERIC => "'%s': the price fields only accept numeric values.",
  25. );
  26. /**
  27. *
  28. * checks if at least one row has been added and that the price fields contain localized numeric values
  29. *
  30. * @return bool return true if the validation is successful
  31. */
  32. public function isValid()
  33. {
  34. $value = $this->getValue();
  35. if (isset($value[FlatRatesLocationGroups::FIELD_NAME])) {
  36. $values = array_filter($value[FlatRatesLocationGroups::FIELD_NAME]);
  37. if (count($values) > 0) {
  38. $prices = array_filter($value[FlatRatesLocationGroups::FIELD_FIRST]) + array_filter($value[FlatRatesLocationGroups::FIELD_ADDL]);
  39. foreach ($prices as $price) {
  40. if (!is_numeric($price)) {
  41. $this->setMessage($this->_messages[self::PRICE_NOT_NUMERIC]);
  42. return false;
  43. }
  44. }
  45. return true;
  46. }
  47. }
  48. $this->setMessage($this->_messages[self::NO_POSTAGE]);
  49. return false;
  50. }
  51. }