• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

RonasIT / laravel-helpers / 33723583272

03 Sep 2026 06:31AM UTC coverage: 87.009% (+0.06%) from 86.948%
33723583272

push

github

web-flow
Merge pull request #277 from RonasIT/add-methods-to-base-request

feat: add init and before hooks to validateResolved in base request

6 of 7 new or added lines in 1 file covered. (85.71%)

1286 of 1478 relevant lines covered (87.01%)

20.51 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

85.37
/src/Http/BaseRequest.php
1
<?php
2

3
namespace RonasIT\Support\Http;
4

5
use Illuminate\Foundation\Http\FormRequest;
6
use Illuminate\Support\Arr;
7
use RonasIT\Support\Exceptions\InvalidModelException;
8

9
class BaseRequest extends FormRequest
10
{
11
    public function authorize(): bool
12
    {
13
        return true;
3 ✔
14
    }
15

16
    public function rules(): array
17
    {
18
        return [];
×
19
    }
20

21
    /**
22
     * @param  array  $keys
23
     *
24
     * Sorts and filters request parameters. Returns parameters specified only in the function rules().
25
     * It needs to avoid troubles in cases where array-parameter declared in rules below its content.
26
     *
27
     * @return array;
28
     */
29
    public function onlyValidated(array $keys = []): array
30
    {
31
        $rules = array_keys($this->rules());
3 ✔
32

33
        $this->sortByStrlen($rules);
3 ✔
34

35
        $validatedFields = $this->filterOnlyValidated(parent::all(), array_undot(array_flip($rules)));
3 ✔
36

37
        return (empty($keys))
3 ✔
38
            ? $validatedFields
1 ✔
39
            : Arr::only($validatedFields, $keys);
3 ✔
40
    }
41

42
    public function validateResolved(): void
43
    {
44
        $this->init();
3 ✔
45

46
        $this->callBeforeAuthorizationHandlers();
3 ✔
47

48
        parent::validateResolved();
3 ✔
49
    }
50

51
    protected function init(): void
52
    {
53
        // Override in child classes if needed
NEW
54
    }
×
55

56
    /**
57
     * Returns the handlers to run before the authorization check.
58
     *
59
     * Every element must be an already instantiated invokable object.
60
     * Handlers are invoked without arguments, in the order
61
     * of the returned array, and may interrupt the request only by throwing an exception.
62
     *
63
     * @return object[]
64
     */
65
    protected function beforeAuthorization(): array
66
    {
67
        return [];
1 ✔
68
    }
69

70
    protected function callBeforeAuthorizationHandlers(): void
71
    {
72
        foreach ($this->beforeAuthorization() as $handler) {
3 ✔
73
            $handler();
1 ✔
74
        }
75
    }
76

77
    protected function getOrderableFields(string $modelName, array $additionalFields = []): string
78
    {
79
        if (!class_exists($modelName)) {
2 ✔
80
            throw new InvalidModelException("The model {$modelName} does not exist.");
×
81
        }
82

83
        $fields = array_merge($modelName::getFields(), $additionalFields);
2 ✔
84

85
        return implode(',', $fields);
2 ✔
86
    }
87

88
    protected function filterOnlyValidated($fields, $validation): array
89
    {
90
        $result = [];
3 ✔
91

92
        foreach ($validation as $fieldName => $validatedKeys) {
3 ✔
93
            if (Arr::has($fields, $fieldName) || $fieldName === '*') {
3 ✔
94
                $validatedItem = Arr::get($fields, $fieldName);
3 ✔
95

96
                if ($this->isNotNestedRule($validatedKeys)) {
3 ✔
97
                    $result[$fieldName] = $validatedItem;
3 ✔
98
                } elseif (Arr::has($validatedKeys, '*')) {
3 ✔
99
                    $result[$fieldName] = $this->processNestedRule($validatedKeys['*'], $validatedItem);
3 ✔
100
                } elseif ($fieldName === '*') {
×
101
                    $result = $this->processNestedRule($validatedKeys, $fields);
×
102
                } else {
103
                    $result[$fieldName] = $this->filterOnlyValidated($validatedItem, $validatedKeys);
×
104
                }
105
            }
106
        }
107

108
        return $result;
3 ✔
109
    }
110

111
    protected function processNestedRule($validatedKeys, $validatedItem)
112
    {
113
        if ($this->isNotNestedRule($validatedKeys)) {
3 ✔
114
            return $validatedItem;
3 ✔
115
        }
116

117
        return array_map(function ($item) use ($validatedKeys) {
3 ✔
118
            return $this->filterOnlyValidated($item, $validatedKeys);
3 ✔
119
        }, $validatedItem);
3 ✔
120
    }
121

122
    protected function isNotNestedRule($validatedKeys): bool
123
    {
124
        return is_int($validatedKeys);
3 ✔
125
    }
126

127
    private function sortByStrlen(array &$array): void
128
    {
129
        $collection = collect($array)->sortBy(function ($string) {
3 ✔
130
            return strlen($string);
3 ✔
131
        });
3 ✔
132

133
        $array = $collection->toArray();
3 ✔
134
    }
135
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE TRIAL · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc