1
|
|
|
<?php namespace Limoncello\Flute\Validation\JsonApi\Rules; |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Copyright 2015-2018 [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
|
|
|
* @SuppressWarnings(PHPMD.StaticAccess) |
33
|
|
|
*/ |
34
|
28 |
|
public static function getIdentityRule(): ?RuleInterface |
35
|
|
|
{ |
36
|
28 |
|
return r::success(); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @inheritdoc |
41
|
|
|
*/ |
42
|
28 |
|
public static function getFilterRules(): ?array |
43
|
|
|
{ |
44
|
28 |
|
return []; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @inheritdoc |
49
|
|
|
*/ |
50
|
28 |
|
public static function getFieldSetRules(): ?array |
51
|
|
|
{ |
52
|
28 |
|
return []; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* @inheritdoc |
57
|
|
|
* |
58
|
|
|
* @SuppressWarnings(PHPMD.StaticAccess) |
59
|
|
|
*/ |
60
|
28 |
|
public static function getSortsRule(): ?RuleInterface |
61
|
|
|
{ |
62
|
28 |
|
return r::fail(); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* @inheritdoc |
67
|
|
|
* |
68
|
|
|
* @SuppressWarnings(PHPMD.StaticAccess) |
69
|
|
|
*/ |
70
|
28 |
|
public static function getIncludesRule(): ?RuleInterface |
71
|
|
|
{ |
72
|
28 |
|
return r::fail(); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* @inheritdoc |
77
|
|
|
* |
78
|
|
|
* @SuppressWarnings(PHPMD.StaticAccess) |
79
|
|
|
*/ |
80
|
28 |
|
public static function getPageOffsetRule(): ?RuleInterface |
81
|
|
|
{ |
82
|
|
|
// if not given (`null` as an input) then 0 otherwise input should be integer value >= 0 |
83
|
28 |
|
return r::ifX( |
84
|
28 |
|
r::IS_NULL_CALLABLE, |
85
|
28 |
|
r::value(0), |
86
|
28 |
|
r::stringToInt(r::moreOrEquals(0)) |
87
|
|
|
); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* @inheritdoc |
92
|
|
|
* |
93
|
|
|
* @SuppressWarnings(PHPMD.StaticAccess) |
94
|
|
|
*/ |
95
|
28 |
|
public static function getPageLimitRule(): ?RuleInterface |
96
|
|
|
{ |
97
|
28 |
|
return static::getPageLimitRuleForDefaultAndMaxSizes( |
98
|
28 |
|
FluteSettings::DEFAULT_PAGE_SIZE, |
99
|
28 |
|
FluteSettings::DEFAULT_MAX_PAGE_SIZE |
100
|
|
|
); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* @param int $defaultSize |
105
|
|
|
* @param int $maxSize |
106
|
|
|
* |
107
|
|
|
* @return RuleInterface |
108
|
|
|
* |
109
|
|
|
* @SuppressWarnings(PHPMD.StaticAccess) |
110
|
|
|
*/ |
111
|
28 |
|
public static function getPageLimitRuleForDefaultAndMaxSizes(int $defaultSize, int $maxSize): RuleInterface |
112
|
|
|
{ |
113
|
28 |
|
assert($maxSize > 1 && $defaultSize <= $maxSize); |
114
|
|
|
|
115
|
|
|
// if not given (`null` as an input) then default value otherwise input should be integer 1 <= value <= max |
116
|
28 |
|
return r::ifX( |
117
|
28 |
|
r::IS_NULL_CALLABLE, |
118
|
28 |
|
r::value($defaultSize), |
119
|
28 |
|
r::stringToInt(r::between(1, $maxSize)) |
120
|
|
|
); |
121
|
|
|
} |
122
|
|
|
} |
123
|
|
|
|