DiskSizeValidator.php 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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(!$hdd->getName() == 'disk-0'){
  33. $this->minValue = $size;
  34. }
  35. if(preg_match("/\./", $data)){
  36. $this->addValidationError('PleaseProvideANumericValueBetween', false, ['minValue' => $this->minValue, 'maxValue' => $this->maxValue]);
  37. return false;
  38. }
  39. if (is_numeric($data) && $this->minValue === 0 && $this->maxValue === 0)
  40. {
  41. return true;
  42. }
  43. //Min & Max
  44. if (is_numeric($data) && $this->minValue <= ((int)$data) && ((int)$data) <= $this->maxValue)
  45. {
  46. return true;
  47. }
  48. //Min
  49. else
  50. {
  51. if (is_numeric($data) && !is_numeric($this->maxValue) && $this->minValue <= ((int)$data))
  52. {
  53. return true;
  54. }
  55. }
  56. if ($this->minValue === $this->maxValue)
  57. {
  58. $this->addValidationError('PleaseProvideANumericValue');
  59. return false;
  60. }
  61. if (is_numeric($this->minValue) && is_numeric($this->maxValue))
  62. {
  63. $this->addValidationError('PleaseProvideANumericValueBetween', false, ['minValue' => $this->minValue, 'maxValue' => $this->maxValue]);
  64. }
  65. else
  66. {
  67. if (is_numeric($this->minValue) && !is_numeric($this->maxValue))
  68. {
  69. $this->addValidationError('PleaseProvideANumericValueFrom', false, ['minValue' => $this->minValue]);
  70. }
  71. }
  72. return false;
  73. }
  74. }