DiskSizeValidator.php 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. <?php
  2. namespace ModulesGarden\Servers\ProxmoxCloudVps\App\UI\MountPoint\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. $size = $vm->getMounPoints()->findMountPointById($diskId)->getGb();
  24. $diskResource = $resurceManager ->disk();
  25. $diskResource->setUsed($diskResource->getUsed() - $size);
  26. $this->maxValue = $diskResource->free();
  27. $this->minValue = $diskResource->getMin();
  28. if(preg_match("/\./", $data)){
  29. $this->addValidationError('PleaseProvideANumericValueBetween', false, ['minValue' => $this->minValue, 'maxValue' => $this->maxValue]);
  30. return false;
  31. }
  32. if (is_numeric($data) && $this->minValue === 0 && $this->maxValue === 0)
  33. {
  34. return true;
  35. }
  36. //Min & Max
  37. if (is_numeric($data) && $this->minValue <= ((int)$data) && ((int)$data) <= $this->maxValue)
  38. {
  39. return true;
  40. }
  41. //Min
  42. else
  43. {
  44. if (is_numeric($data) && !is_numeric($this->maxValue) && $this->minValue <= ((int)$data))
  45. {
  46. return true;
  47. }
  48. }
  49. if ($this->minValue === $this->maxValue)
  50. {
  51. $this->addValidationError('PleaseProvideANumericValue');
  52. return false;
  53. }
  54. if (is_numeric($this->minValue) && is_numeric($this->maxValue))
  55. {
  56. $this->addValidationError('PleaseProvideANumericValueBetween', false, ['minValue' => $this->minValue, 'maxValue' => $this->maxValue]);
  57. }
  58. else
  59. {
  60. if (is_numeric($this->minValue) && !is_numeric($this->maxValue))
  61. {
  62. $this->addValidationError('PleaseProvideANumericValueFrom', false, ['minValue' => $this->minValue]);
  63. }
  64. }
  65. return false;
  66. }
  67. }