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

RonasIT / laravel-helpers / 12366912065

17 Dec 2024 05:22AM UTC coverage: 78.385% (-0.01%) from 78.395%
12366912065

push

github

web-flow
Merge pull request #164 from RonasIT/161-ability-to-validate-order-by-field

#161: Ability to validate order_by field

3 of 4 new or added lines in 1 file covered. (75.0%)

1019 of 1300 relevant lines covered (78.38%)

11.82 hits per line

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

8.82
/src/BaseRequest.php
1
<?php
2

3
namespace RonasIT\Support;
4

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

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

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

21
    /**
22
     * @param array|string $keys
23
     * @param mixed $default
24
     *
25
     * Sorts and filters request parameters. Returns parameters specified only in the function rules().
26
     * It needs to avoid troubles in cases where array-parameter declared in rules below its content.
27
     *
28
     * @return array;
29
     */
30

31
    public function onlyValidated($keys = null, $default = null): array
32
    {
33
        $rules = array_keys($this->rules());
×
34

35
        $this->sortByStrlen($rules);
×
36

37
        $validatedFields = $this->filterOnlyValidated(parent::all(), array_undot(array_flip($rules)));
×
38

39
        if (!empty($keys)) {
×
40
            return is_array($keys) ? Arr::only($validatedFields, $keys) : Arr::get($validatedFields, $keys, $default);
×
41
        }
42

43
        return $validatedFields;
×
44
    }
45

46
    protected function getOrderableFields(string $modelName, array $additionalFields = []): string
47
    {
48
        if (!class_exists($modelName)) {
2✔
NEW
49
            throw new InvalidModelException("The model {$modelName} does not exist.");
×
50
        }
51

52
        $fields = array_merge($modelName::getFields(), $additionalFields);
2✔
53

54
        return implode(',', $fields);
2✔
55
    }
56

57
    protected function filterOnlyValidated($fields, $validation): array
58
    {
59
        $result = [];
×
60

61
        foreach ($validation as $fieldName => $validatedKeys) {
×
62
            if (Arr::has($fields, $fieldName) || $fieldName === '*') {
×
63
                $validatedItem = Arr::get($fields, $fieldName);
×
64

65
                if ($this->isNotNestedRule($validatedKeys)) {
×
66
                    $result[$fieldName] = $validatedItem;
×
67
                } elseif (Arr::has($validatedKeys, '*')) {
×
68
                    $result[$fieldName] = $this->processNestedRule($validatedKeys['*'], $validatedItem);
×
69
                } elseif ($fieldName === '*') {
×
70
                    $result = $this->processNestedRule($validatedKeys, $fields);
×
71
                } else {
72
                    $result[$fieldName] = $this->filterOnlyValidated($validatedItem, $validatedKeys);
×
73
                }
74
            }
75
        }
76

77
        return $result;
×
78
    }
79

80
    protected function processNestedRule($validatedKeys, $validatedItem)
81
    {
82
        if ($this->isNotNestedRule($validatedKeys)) {
×
83
            return $validatedItem;
×
84
        }
85

86
        return array_map(function ($item) use ($validatedKeys) {
×
87
            return $this->filterOnlyValidated($item, $validatedKeys);
×
88
        }, $validatedItem);
×
89
    }
90

91
    protected function isNotNestedRule($validatedKeys): bool
92
    {
93
        return is_integer($validatedKeys);
×
94
    }
95

96
    private function sortByStrlen(array &$array): void
97
    {
98
        $collection = collect($array)->sortBy(function ($string) {
×
99
            return strlen($string);
×
100
        });
×
101

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

© 2025 Coveralls, Inc