DefaultQueryValidationRules   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 96
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 9
lcom 0
cbo 1
dl 0
loc 96
ccs 25
cts 25
cp 1
rs 10
c 0
b 0
f 0

8 Methods

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