ItemPostage.php 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. <?php
  2. /**
  3. *
  4. * PHP Pro Bid $Id$ qe9Wx/dr81R3cwIX9Grizhji5ZcxnsDhSy7OdUBxo8Y=
  5. *
  6. * @link http://www.phpprobid.com
  7. * @copyright Copyright (c) 2015 Online Ventures Software & CodeCube SRL
  8. * @license http://www.phpprobid.com/license Commercial License
  9. *
  10. * @version 7.7
  11. */
  12. /**
  13. * listing item postage validator class
  14. */
  15. namespace Ppb\Validate;
  16. use Cube\Validate\AbstractValidate,
  17. Ppb\Form\Element\ListingPostageLocations;
  18. class ItemPostage 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 item postage option has been entered and
  29. * if the price fields contain numeric values
  30. *
  31. * @return bool return true if the validation is successful
  32. */
  33. public function isValid()
  34. {
  35. $value = $this->getValue();
  36. if (isset($value[ListingPostageLocations::FIELD_METHOD])) {
  37. $values = array_filter($value[ListingPostageLocations::FIELD_METHOD]);
  38. if (count($values) > 0) {
  39. $prices = array_filter($value[ListingPostageLocations::FIELD_PRICE]);
  40. foreach ($prices as $price) {
  41. if (!is_numeric($price)) {
  42. $this->setMessage($this->_messages[self::PRICE_NOT_NUMERIC]);
  43. return false;
  44. }
  45. }
  46. return true;
  47. }
  48. }
  49. $this->setMessage($this->_messages[self::NO_POSTAGE]);
  50. return false;
  51. }
  52. }