| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465 |
- <?php
- namespace App\Module\Farm\Validators;
- use App\Module\Farm\Models\FarmCrop;
- use App\Module\Farm\Enums\GROWTH_STAGE;
- use UCore\Validator;
- /**
- * 摘取状态验证器
- * 验证作物是否满足摘取条件
- */
- class PickableStatusValidator extends Validator
- {
- private ?FarmCrop $crop = null;
- /**
- * 验证作物摘取状态
- *
- * @param mixed $value 作物ID
- * @param array $data 验证数据
- * @return bool
- */
- public function validate(mixed $value, array $data): bool
- {
- $cropId = (int)$value;
- // 验证作物是否存在
- $this->crop = FarmCrop::find($cropId);
- if (!$this->crop) {
- $this->addError('作物不存在');
- return false;
- }
- // 检查作物是否处于成熟期
- if ($this->crop->growth_stage !== GROWTH_STAGE::MATURE) {
- $this->addError('作物未成熟,无法摘取');
- return false;
- }
- // 检查是否有最终产出
- if ($this->crop->final_output_amount <= 0) {
- $this->addError('作物没有产出,无法摘取');
- return false;
- }
- // 检查是否有可摘取数量
- if ($this->crop->pickable_amount <= 0) {
- $this->addError('作物没有可摘取数量');
- return false;
- }
- return true;
- }
- /**
- * 获取验证通过的作物信息
- *
- * @return FarmCrop|null
- */
- public function getCrop(): ?FarmCrop
- {
- return $this->crop;
- }
- }
|