ProjectMemberUpdateRequest.php
1.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
<?php
namespace App\Http\Container\AppSection\Requests;
use Hikoon\LaravelApi\Support\ApiRequest;
use Closure;
use App\Models\Pivots\ProjectMemberPivot;
class ProjectMemberUpdateRequest extends ApiRequest
{
    protected function passedValidation(): void
    {
        if ($this->has('is_top') && $this->input('is_top') === 1) {
            $this->getInputSource()?->set('is_top', now()->timestamp);
            optional($this->getValidatorInstance())->setData($this->all());
        }
    }
    public function rules(): array
    {
        return [
            'is_top' => ['sometimes', 'integer', 'in:0,1', function (string $attribute, int $value, Closure $fail) {
                if ($value === 1 && $this->getTopCount() >= 3) {
                    $fail('最多允许置顶3条内容~');
                }
            }]
        ];
    }
    /**
     * @return string[]
     */
    public function messages(): array
    {
        return [
            'is_top.in' => '请选择置顶状态'
        ];
    }
    /**
     * @return int
     */
    public function getTopCount(): int
    {
        $project_id = ProjectMemberPivot::query()->where('id',$this->member)->value('project_id');
        return ProjectMemberPivot::query()
            ->where('project_id',$project_id)
            ->where('is_top', '!=', 0)
            ->count();
    }
}