Request::putMethodRules()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 3
rs 10
1
<?php
2
/**
3
 * Created by PhpStorm.
4
 * User: Mohammad Shamaseen
5
 * Date: 09/10/18
6
 * Time: 01:01 م.
7
 */
8
9
namespace Shamaseen\Repository\Utility;
10
11
use Illuminate\Foundation\Http\FormRequest;
12
use Illuminate\Support\Str;
13
use ReflectionClass;
14
15
/**
16
 * Class BaseRequests.
17
 */
18
class Request extends FormRequest
19
{
20
    public AbstractRepository $repository;
21
22
    public function __construct(AbstractRepository $repository, array $query = [], array $request = [], array $attributes = [], array $cookies = [], array $files = [], array $server = [], $content = null)
23
    {
24
        parent::__construct($query, $request, $attributes, $cookies, $files, $server, $content);
25
        $this->repository = $repository;
26
    }
27
28
    /**
29
     * Define all the global rules for this request here.
30
     */
31
    protected array $rules = [
32
    ];
33
34
    public function rules(): array
35
    {
36
        $rules = [] + $this->rules;
37
38
        $method = $this->method();
39
40
        $httpValidatorName = 'Http'.ucfirst(Str::lower($method)).'Rules';
41
42
        if (method_exists($this, $httpValidatorName)) {
43
            $rules += $this->{$httpValidatorName}();
44
        }
45
46
        $route = $this->route();
47
        // make sure that it is accessed by a route and not command or job
48
        if ($route) {
49
            $routeName = $route->action['uses'];
50
            if ($routeName) {
51
                $lastPart = last(explode('@', $routeName));
52
                $validatorName = $lastPart.'Rules';
53
54
                $reflection = new ReflectionClass($this);
55
                // make sure the method is exists in the user defined request
56
                if ($reflection->hasMethod($validatorName) && $reflection->getMethod($validatorName)->class === $reflection->getName()) {
57
                    $rules += $this->{$validatorName}();
58
                }
59
            }
60
        }
61
62
        return $rules;
63
    }
64
65
    /**
66
     * Define all the rules for every get HTTP method on this request.
67
     */
68
    public function getMethodRules(): array
69
    {
70
        return [];
71
    }
72
73
    /**
74
     * Define all the rules for every post HTTP method on this request.
75
     */
76
    public function postMethodRules(): array
77
    {
78
        return [];
79
    }
80
81
    /**
82
     * Define all the rules for every patch HTTP method on this request.
83
     */
84
    public function patchMethodRules(): array
85
    {
86
        return [];
87
    }
88
89
    /**
90
     * Define all the rules for every put HTTP method on this request.
91
     */
92
    public function putMethodRules(): array
93
    {
94
        return [];
95
    }
96
97
    /**
98
     * Define all the rules for every delete HTTP method on this request.
99
     */
100
    public function deleteMethodRules(): array
101
    {
102
        return [];
103
    }
104
}
105