Completed
Push — master ( 1a67d2...dfab50 )
by Neomerx
11:35 queued 22s
created

DefaultQueryValidationRules::getFieldSetRules()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
crap 1
1
<?php namespace Limoncello\Flute\Validation\JsonApi\Rules;
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
     * @SuppressWarnings(PHPMD.StaticAccess)
49
     */
50 28
    public static function getSortsRule(): ?RuleInterface
51
    {
52 28
        return r::fail();
53
    }
54
55
    /**
56
     * @inheritdoc
57
     *
58
     * @SuppressWarnings(PHPMD.StaticAccess)
59
     */
60 28
    public static function getIncludesRule(): ?RuleInterface
61
    {
62 28
        return r::fail();
63
    }
64
65
    /**
66
     * @inheritdoc
67
     *
68
     * @SuppressWarnings(PHPMD.StaticAccess)
69
     */
70 28
    public static function getPageOffsetRule(): ?RuleInterface
71
    {
72
        // if not given (`null` as an input) then 0 otherwise input should be integer value >= 0
73 28
        return r::ifX(
74 28
            r::IS_NULL_CALLABLE,
75 28
            r::value(0),
76 28
            r::stringToInt(r::moreOrEquals(0))
77
        );
78
    }
79
80
    /**
81
     * @inheritdoc
82
     *
83
     * @SuppressWarnings(PHPMD.StaticAccess)
84
     */
85 28
    public static function getPageLimitRule(): ?RuleInterface
86
    {
87 28
        return static::getPageLimitRuleForDefaultAndMaxSizes(
88 28
            FluteSettings::DEFAULT_PAGE_SIZE,
89 28
            FluteSettings::DEFAULT_MAX_PAGE_SIZE
90
        );
91
    }
92
93
    /**
94
     * @param int $defaultSize
95
     * @param int $maxSize
96
     *
97
     * @return RuleInterface
98
     *
99
     * @SuppressWarnings(PHPMD.StaticAccess)
100
     */
101 28
    public static function getPageLimitRuleForDefaultAndMaxSizes(int $defaultSize, int $maxSize): RuleInterface
102
    {
103 28
        assert($maxSize > 1 && $defaultSize <= $maxSize);
104
105
        // if not given (`null` as an input) then default value otherwise input should be integer 1 <= value <= max
106 28
        return r::ifX(
107 28
            r::IS_NULL_CALLABLE,
108 28
            r::value($defaultSize),
109 28
            r::stringToInt(r::between(1, $maxSize))
110
        );
111
    }
112
}
113