1
|
|
|
<?php |
2
|
|
|
declare(strict_types=1); |
3
|
|
|
/** |
4
|
|
|
* Caridea |
5
|
|
|
* |
6
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License"); you may not |
7
|
|
|
* use this file except in compliance with the License. You may obtain a copy of |
8
|
|
|
* 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, WITHOUT |
14
|
|
|
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
15
|
|
|
* License for the specific language governing permissions and limitations under |
16
|
|
|
* the License. |
17
|
|
|
* |
18
|
|
|
* @copyright 2015-2018 LibreWorks contributors |
19
|
|
|
* @license Apache-2.0 |
20
|
|
|
*/ |
21
|
|
|
namespace Caridea\Validate\Rule; |
22
|
|
|
|
23
|
|
|
use Caridea\Validate\Parser; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* Compares values to some type |
27
|
|
|
* |
28
|
|
|
* @copyright 2015-2018 LibreWorks contributors |
29
|
|
|
* @license Apache-2.0 |
30
|
|
|
*/ |
31
|
|
|
class Type implements \Caridea\Validate\Rule |
32
|
|
|
{ |
33
|
|
|
/** |
34
|
|
|
* @var string The operator type |
35
|
|
|
*/ |
36
|
|
|
private $operator; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Creates a new CompareRule. |
40
|
|
|
* |
41
|
|
|
* @param string $operator The operator type |
42
|
|
|
*/ |
43
|
2 |
|
protected function __construct(string $operator) |
44
|
|
|
{ |
45
|
2 |
|
$this->operator = $operator; |
46
|
2 |
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* {@inheritDoc} |
50
|
|
|
*/ |
51
|
2 |
|
public function apply($value, $data = []): ?array |
52
|
|
|
{ |
53
|
2 |
|
switch ($this->operator) { |
54
|
2 |
|
case "string": |
|
|
|
|
55
|
1 |
|
return is_scalar($value) ? null : ['FORMAT_ERROR']; |
56
|
1 |
|
case "object": |
57
|
1 |
|
return is_object($value) || (is_array($value) && Parser::isAssociative($value)) ? |
58
|
1 |
|
null : ['FORMAT_ERROR']; |
59
|
|
|
} |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Gets a rule that matches a value against another value. |
64
|
|
|
* |
65
|
|
|
* @return \Caridea\Validate\Rule\Type the created rule |
66
|
|
|
*/ |
67
|
1 |
|
public static function string(): Type |
68
|
|
|
{ |
69
|
1 |
|
return new Type('string'); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* Gets a rule that matches a value against a list of accepted values. |
74
|
|
|
* |
75
|
|
|
* @return \Caridea\Validate\Rule\Compare the created rule |
76
|
|
|
*/ |
77
|
1 |
|
public static function anyObject(): Type |
78
|
|
|
{ |
79
|
1 |
|
return new Type('object'); |
80
|
|
|
} |
81
|
|
|
} |
82
|
|
|
|
As per the PSR-2 coding standard, case statements should not be wrapped in curly braces. There is no need for braces, since each case is terminated by the next
break
.There is also the option to use a semicolon instead of a colon, this is discouraged because many programmers do not even know it works and the colon is universal between programming languages.
To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.