PickableStatusValidator.php 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. <?php
  2. namespace App\Module\Farm\Validators;
  3. use App\Module\Farm\Models\FarmCrop;
  4. use App\Module\Farm\Enums\GROWTH_STAGE;
  5. use UCore\Validator;
  6. /**
  7. * 摘取状态验证器
  8. * 验证作物是否满足摘取条件
  9. */
  10. class PickableStatusValidator extends Validator
  11. {
  12. private ?FarmCrop $crop = null;
  13. /**
  14. * 验证作物摘取状态
  15. *
  16. * @param mixed $value 作物ID
  17. * @param array $data 验证数据
  18. * @return bool
  19. */
  20. public function validate(mixed $value, array $data): bool
  21. {
  22. $cropId = (int)$value;
  23. // 验证作物是否存在
  24. $this->crop = FarmCrop::find($cropId);
  25. if (!$this->crop) {
  26. $this->addError('作物不存在');
  27. return false;
  28. }
  29. // 检查作物是否处于成熟期
  30. if ($this->crop->growth_stage !== GROWTH_STAGE::MATURE) {
  31. $this->addError('作物未成熟,无法摘取');
  32. return false;
  33. }
  34. // 检查是否有最终产出
  35. if ($this->crop->final_output_amount <= 0) {
  36. $this->addError('作物没有产出,无法摘取');
  37. return false;
  38. }
  39. // 检查是否有可摘取数量
  40. if ($this->crop->pickable_amount <= 0) {
  41. $this->addError('作物没有可摘取数量');
  42. return false;
  43. }
  44. return true;
  45. }
  46. /**
  47. * 获取验证通过的作物信息
  48. *
  49. * @return FarmCrop|null
  50. */
  51. public function getCrop(): ?FarmCrop
  52. {
  53. return $this->crop;
  54. }
  55. }