PortValidator.php 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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 PortValidator extends BaseValidator
  27. {
  28. protected $minValue = 1;
  29. protected $maxValue = 65535;
  30. protected $required = false;
  31. public function __construct($required = false)
  32. {
  33. $this->required = $required;
  34. }
  35. protected function validate($data, $additionalData = null)
  36. {
  37. if (!$this->required && empty($data))
  38. {
  39. return true;
  40. }
  41. if (is_numeric($data) && $this->minValue === 0 && $this->maxValue === 0)
  42. {
  43. return true;
  44. }
  45. if($data && preg_match("/\:/",$data)){
  46. return true;
  47. }
  48. //Min & Max
  49. if (is_numeric($data) && $this->minValue <= ((int)$data) && ((int)$data) <= $this->maxValue)
  50. {
  51. return true;
  52. }
  53. //Min
  54. else
  55. {
  56. if (is_numeric($data) && !is_numeric($this->maxValue) && $this->minValue <= ((int)$data))
  57. {
  58. return true;
  59. }
  60. }
  61. if ($this->minValue === $this->maxValue)
  62. {
  63. $this->addValidationError('PleaseProvideANumericValue');
  64. return false;
  65. }
  66. if (is_numeric($this->minValue) && is_numeric($this->maxValue))
  67. {
  68. $this->addValidationError('PleaseProvideANumericValueBetween', false, ['minValue' => $this->minValue, 'maxValue' => $this->maxValue]);
  69. }
  70. else
  71. {
  72. if (is_numeric($this->minValue) && !is_numeric($this->maxValue))
  73. {
  74. $this->addValidationError('PleaseProvideANumericValueFrom', false, ['minValue' => $this->minValue]);
  75. }
  76. }
  77. return false;
  78. }
  79. }