DiskSizeValidator.php 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. <?php
  2. namespace ModulesGarden\Servers\ProxmoxCloudVps\App\UI\VmClone\Validators;
  3. use ModulesGarden\ProxmoxAddon\App\Services\Cloud\ResourceManager;
  4. use ModulesGarden\Servers\ProxmoxCloudVps\Core\UI\Widget\Forms\Validators\BaseValidator;
  5. use ModulesGarden\Servers\ProxmoxCloudVps\Core\UI\Traits\WhmcsParams;
  6. use ModulesGarden\Servers\ProxmoxCloudVps\App\Enum\ConfigurableOption;
  7. class DiskSizeValidator extends BaseValidator
  8. {
  9. use WhmcsParams;
  10. protected $additionalField;
  11. protected $minValue = 0;
  12. protected $maxValue = 0;
  13. protected $required = true;
  14. public function __construct( $additionalField, $required = true)
  15. {
  16. $this->additionalField = $additionalField;
  17. $this->required = $required;
  18. }
  19. protected function validate($data, $additionalData = null)
  20. {
  21. if (!$this->required && empty($data))
  22. {
  23. return true;
  24. }
  25. $resurceManager = new ResourceManager();
  26. $additionalSize = 0;
  27. if($additionalData->get('formData')[$this->additionalField] ){
  28. $additionalSize = (int) $additionalData->get('formData')[$this->additionalField];
  29. }
  30. $diskResource = $resurceManager ->disk();
  31. $diskResource->setTotal($diskResource->getTotal()-$additionalSize);
  32. if ($this->isWhmcsConfigOption(ConfigurableOption::STORAGE)) {
  33. $this->maxValue = $this->getWhmcsConfigOption(ConfigurableOption::STORAGE) - $diskResource->getUsed();
  34. } else {
  35. $this->maxValue = $diskResource->free();
  36. }
  37. if ($this->maxValue > $diskResource->getMax()) {
  38. $this->addValidationError('PleaseProvideANumericValueBetween', false, ['minValue' => $this->minValue, 'maxValue' => $diskResource->getMax()]);
  39. return false;
  40. }
  41. $this->minValue = $diskResource->getMin();
  42. if(preg_match("/\./", $data)){
  43. $this->addValidationError('PleaseProvideANumericValueBetween', false, ['minValue' => $this->minValue, 'maxValue' => $this->maxValue]);
  44. return false;
  45. }
  46. if (is_numeric($data) && $this->minValue === 0 && $this->maxValue === 0)
  47. {
  48. return true;
  49. }
  50. //Min & Max
  51. if (is_numeric($data) && $this->minValue <= ((int)$data) && ((int)$data) <= $this->maxValue)
  52. {
  53. return true;
  54. }
  55. //Min
  56. else
  57. {
  58. if (is_numeric($data) && !is_numeric($this->maxValue) && $this->minValue <= ((int)$data))
  59. {
  60. return true;
  61. }
  62. }
  63. if ($this->minValue === $this->maxValue)
  64. {
  65. $this->addValidationError('PleaseProvideANumericValue');
  66. return false;
  67. }
  68. if (is_numeric($this->minValue) && is_numeric($this->maxValue))
  69. {
  70. $this->addValidationError('PleaseProvideANumericValueBetween', false, ['minValue' => $this->minValue, 'maxValue' => $this->maxValue]);
  71. }
  72. else
  73. {
  74. if (is_numeric($this->minValue) && !is_numeric($this->maxValue))
  75. {
  76. $this->addValidationError('PleaseProvideANumericValueFrom', false, ['minValue' => $this->minValue]);
  77. }
  78. }
  79. return false;
  80. }
  81. }