DiskSizeValidator.php 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. <?php
  2. namespace ModulesGarden\Servers\ProxmoxCloudVps\App\UI\Disk\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 $minValue = 0;
  8. protected $maxValue = 0;
  9. protected $required = true;
  10. public function __construct( $required = true)
  11. {
  12. $this->required = $required;
  13. }
  14. protected function validate($data, $additionalData = null)
  15. {
  16. if (!$this->required && empty($data))
  17. {
  18. return true;
  19. }
  20. $resurceManager = new ResourceManager();
  21. $diskId = $additionalData->get('formData')['id'];
  22. $vm = \ModulesGarden\ProxmoxAddon\Core\Helper\sl('Vm')->getVm();
  23. $hdd = $vm->getHardDiskRepostiory()->findById($diskId);
  24. $size = $hdd->getGb();
  25. $diskResource = $resurceManager ->disk();
  26. $diskResource->setUsed($diskResource->getUsed() - $size);
  27. $this->maxValue = $diskResource->free();
  28. $this->minValue = $diskResource->getMin();
  29. if(!$hdd->isMaster()){
  30. $this->minValue = $size;
  31. }
  32. if(preg_match("/\./", $data)){
  33. $this->addValidationError('PleaseProvideANumericValueBetween', false, ['minValue' => $this->minValue, 'maxValue' => $this->maxValue]);
  34. return false;
  35. }
  36. if (is_numeric($data) && $this->minValue === 0 && $this->maxValue === 0)
  37. {
  38. return true;
  39. }
  40. //Min & Max
  41. if (is_numeric($data) && $this->minValue <= ((int)$data) && ((int)$data) <= $this->maxValue)
  42. {
  43. return true;
  44. }
  45. //Min
  46. else
  47. {
  48. if (is_numeric($data) && !is_numeric($this->maxValue) && $this->minValue <= ((int)$data))
  49. {
  50. return true;
  51. }
  52. }
  53. if ($this->minValue === $this->maxValue)
  54. {
  55. $this->addValidationError('PleaseProvideANumericValue');
  56. return false;
  57. }
  58. if (is_numeric($this->minValue) && is_numeric($this->maxValue))
  59. {
  60. $this->addValidationError('PleaseProvideANumericValueBetween', false, ['minValue' => $this->minValue, 'maxValue' => $this->maxValue]);
  61. }
  62. else
  63. {
  64. if (is_numeric($this->minValue) && !is_numeric($this->maxValue))
  65. {
  66. $this->addValidationError('PleaseProvideANumericValueFrom', false, ['minValue' => $this->minValue]);
  67. }
  68. }
  69. return false;
  70. }
  71. }