DiskSizeValidator.php 3.2 KB

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