NumberValidator.php 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. <?php
  2. /* * ********************************************************************
  3. * ProxmoxAddon product developed. (Aug 23, 2018)
  4. * *
  5. *
  6. * CREATED BY MODULESGARDEN -> http://modulesgarden.com
  7. * CONTACT -> contact@modulesgarden.com
  8. *
  9. *
  10. * This software is furnished under a license and may be used and copied
  11. * only in accordance with the terms of such license and with the
  12. * inclusion of the above copyright notice. This software or any other
  13. * copies thereof may not be provided or otherwise made available to any
  14. * other person. No title to and ownership of the software is hereby
  15. * transferred.
  16. *
  17. *
  18. * ******************************************************************** */
  19. namespace ModulesGarden\Servers\ProxmoxCloudVps\App\UI\Validators;
  20. use ModulesGarden\Servers\ProxmoxCloudVps\Core\UI\Widget\Forms\Validators\BaseValidator;
  21. /**
  22. * Description of NumberValidator
  23. *
  24. * @author Pawel Kopec <pawelk@modulesgardne.com>
  25. */
  26. class NumberValidator extends BaseValidator
  27. {
  28. protected $minValue = 0;
  29. protected $maxValue = 0;
  30. protected $required = false;
  31. public function __construct($min = 0, $max = 0, $required = false)
  32. {
  33. $this->minValue = (int)$min;
  34. $this->maxValue = $max;
  35. $this->required = $required;
  36. }
  37. protected function validate($data, $additionalData = null)
  38. {
  39. if (!$this->required && empty($data))
  40. {
  41. return true;
  42. }
  43. if(preg_match("/\./", $data)){
  44. $this->addValidationError('PleaseProvideANumericValueBetween', false, ['minValue' => $this->minValue, 'maxValue' => $this->maxValue]);
  45. return false;
  46. }
  47. if (is_numeric($data) && $this->minValue === 0 && $this->maxValue === 0)
  48. {
  49. return true;
  50. }
  51. //Min & Max
  52. if (is_numeric($data) && $this->minValue <= ((int)$data) && ((int)$data) <= $this->maxValue)
  53. {
  54. return true;
  55. }
  56. //Min
  57. else
  58. {
  59. if (is_numeric($data) && !is_numeric($this->maxValue) && $this->minValue <= ((int)$data))
  60. {
  61. return true;
  62. }
  63. }
  64. if ($this->minValue === $this->maxValue)
  65. {
  66. $this->addValidationError('PleaseProvideANumericValue');
  67. return false;
  68. }
  69. if (is_numeric($this->minValue) && is_numeric($this->maxValue))
  70. {
  71. $this->addValidationError('PleaseProvideANumericValueBetween', false, ['minValue' => $this->minValue, 'maxValue' => $this->maxValue]);
  72. }
  73. else
  74. {
  75. if (is_numeric($this->minValue) && !is_numeric($this->maxValue))
  76. {
  77. $this->addValidationError('PleaseProvideANumericValueFrom', false, ['minValue' => $this->minValue]);
  78. }
  79. }
  80. return false;
  81. }
  82. }