SortsViaRequest::sortingRules()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 4
rs 10
1
<?php
2
3
namespace musa11971\SortRequest\Traits;
4
5
use Illuminate\Foundation\Http\FormRequest;
6
use musa11971\SortRequest\Exceptions\BadTraitImplementation;
7
use musa11971\SortRequest\Rules\SortParameter;
8
9
trait SortsViaRequest
10
{
11
    /**
12
     * Returns the columns that can be sorted on.
13
     *
14
     * @return array
15
     */
16
    abstract function getSortableColumns(): array;
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
17
18
    /** @var SortParameter $sortParameterRule */
19
    private $sortParameterRule;
20
21
    /** @noinspection PhpUnhandledExceptionInspection */
22
    public function __construct()
23
    {
24
        if(!($this instanceof FormRequest))
25
            throw new BadTraitImplementation(__TRAIT__);
26
27
        // Create an instance of the validation rule
28
        $this->sortParameterRule = new SortParameter($this->getSortableColumns());
29
    }
30
31
    /**
32
     * Returns the validated sorting rules.
33
     *
34
     * @return array
35
     */
36
    function validatedSortingRules()
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
37
    {
38
        return $this->sortParameterRule->sortingRules;
39
    }
40
41
    /**
42
     * Returns the transformed sortable columns.
43
     *
44
     * @return \musa11971\SortRequest\SortableColumnCollection
45
     */
46
    function transformedSortableColumns()
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
47
    {
48
        return $this->sortParameterRule->sortableColumns;
49
    }
50
51
    /**
52
     * Get the validation rules for the sorting.
53
     *
54
     * @param string $parameterName
55
     * @return array
56
     */
57
    protected function sortingRules($parameterName = 'sort')
58
    {
59
        return [
60
            $parameterName => [$this->sortParameterRule]
61
        ];
62
    }
63
}
64