Completed
Push — develop ( 26b032...524257 )
by Neomerx
18:05 queued 09:45
created

DefaultQueryValidationRules   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 81
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 8
lcom 0
cbo 1
dl 0
loc 81
rs 10
c 0
b 0
f 0
ccs 21
cts 21
cp 1

7 Methods

Rating   Name   Duplication   Size   Complexity  
A getFilterRules() 0 4 1
A getFieldSetRules() 0 4 1
A getSortsRule() 0 4 1
A getIncludesRule() 0 4 1
A getPageOffsetRule() 0 9 1
A getPageLimitRule() 0 4 1
A getPageLimitRuleForDefaultAndMaxSizes() 0 13 2
1
<?php namespace Limoncello\Flute\Validation\JsonApi;
2
3
/**
4
 * Copyright 2015-2017 [email protected]
5
 *
6
 * Licensed under the Apache License, Version 2.0 (the "License");
7
 * you may not use this file except in compliance with the License.
8
 * You may obtain a copy of the License at
9
 *
10
 * http://www.apache.org/licenses/LICENSE-2.0
11
 *
12
 * Unless required by applicable law or agreed to in writing, software
13
 * distributed under the License is distributed on an "AS IS" BASIS,
14
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
 * See the License for the specific language governing permissions and
16
 * limitations under the License.
17
 */
18
19
use Limoncello\Flute\Contracts\Validation\JsonApiQueryRulesInterface;
20
use Limoncello\Flute\Package\FluteSettings;
21
use Limoncello\Validation\Contracts\Rules\RuleInterface;
22
use Limoncello\Validation\Rules as r;
23
24
/**
25
 * @package Limoncello\Flute
26
 */
27
class DefaultQueryValidationRules implements JsonApiQueryRulesInterface
28
{
29
    /**
30
     * @inheritdoc
31
     */
32 28
    public static function getFilterRules(): ?array
33
    {
34 28
        return [];
35
    }
36
37
    /**
38
     * @inheritdoc
39
     */
40 28
    public static function getFieldSetRules(): ?array
41
    {
42 28
        return [];
43
    }
44
45
    /**
46
     * @inheritdoc
47
     */
48 28
    public static function getSortsRule(): ?RuleInterface
49
    {
50 28
        return null;
51
    }
52
53
    /**
54
     * @inheritdoc
55
     */
56 28
    public static function getIncludesRule(): ?RuleInterface
57
    {
58 28
        return null;
59
    }
60
61
    /**
62
     * @inheritdoc
63
     *
64
     * @SuppressWarnings(PHPMD.StaticAccess)
65
     */
66 28
    public static function getPageOffsetRule(): ?RuleInterface
67
    {
68
        // if not given (`null` as an input) then 0 otherwise input should be integer value >= 0
69 28
        return r::ifX(
70 28
            r::IS_NULL_CALLABLE,
71 28
            r::value(0),
72 28
            r::stringToInt(r::moreOrEquals(0))
73
        );
74
    }
75
76
    /**
77
     * @inheritdoc
78
     *
79
     * @SuppressWarnings(PHPMD.StaticAccess)
80
     */
81 28
    public static function getPageLimitRule(): ?RuleInterface
82
    {
83 28
        return static::getPageLimitRuleForDefaultAndMaxSizes();
84
    }
85
86
    /**
87
     * @param int $defaultSize
88
     * @param int $maxSize
89
     *
90
     * @return RuleInterface
91
     *
92
     * @SuppressWarnings(PHPMD.StaticAccess)
93
     */
94 28
    public static function getPageLimitRuleForDefaultAndMaxSizes(
95
        int $defaultSize = FluteSettings::DEFAULT_PAGE_SIZE,
96
        int $maxSize = FluteSettings::DEFAULT_MAX_PAGE_SIZE
97
    ): RuleInterface {
98 28
        assert($maxSize > 1 && $defaultSize <= $maxSize);
99
100
        // if not given (`null` as an input) then default value otherwise input should be integer 1 <= value <= max
101 28
        return r::ifX(
102 28
            r::IS_NULL_CALLABLE,
103 28
            r::value($defaultSize),
104 28
            r::stringToInt(r::between(1, $maxSize))
105
        );
106
    }
107
}
108