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
|
|
|
/** |
24
|
|
|
* Rules for empty values |
25
|
|
|
* |
26
|
|
|
* @copyright 2015-2018 LibreWorks contributors |
27
|
|
|
* @license Apache-2.0 |
28
|
|
|
*/ |
29
|
|
|
class Blank implements \Caridea\Validate\Rule |
30
|
|
|
{ |
31
|
|
|
/** |
32
|
|
|
* @var string The operator type |
33
|
|
|
*/ |
34
|
|
|
private $operator; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Creates a new EmptyRule. |
38
|
|
|
* |
39
|
|
|
* @param string $operator The operator type |
40
|
|
|
*/ |
41
|
3 |
|
protected function __construct(string $operator) |
42
|
|
|
{ |
43
|
3 |
|
$this->operator = $operator; |
44
|
3 |
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* {@inheritDoc} |
48
|
|
|
*/ |
49
|
3 |
|
public function apply($value, $data = []): ?array |
50
|
|
|
{ |
51
|
3 |
|
switch ($this->operator) { |
52
|
3 |
|
case "required": |
|
|
|
|
53
|
1 |
|
return $value === null || $value === '' ? ['REQUIRED'] : null; |
54
|
2 |
|
case "empty": |
55
|
1 |
|
return $value === '' ? ['CANNOT_BE_EMPTY'] : null; |
56
|
1 |
|
case "list": |
57
|
1 |
|
if (empty($value)) { |
58
|
1 |
|
return ['CANNOT_BE_EMPTY']; |
59
|
1 |
|
} elseif (!is_array($value) && !($value instanceof \Countable)) { |
60
|
1 |
|
return ['FORMAT_ERROR']; |
61
|
|
|
} |
62
|
1 |
|
return count($value) === 0 ? ['CANNOT_BE_EMPTY'] : null; |
63
|
|
|
} |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Gets a rule that requires values to be non-null and not empty string. |
68
|
|
|
* |
69
|
|
|
* @return \Caridea\Validate\Rule\Blank the created rule |
70
|
|
|
*/ |
71
|
1 |
|
public static function required(): Blank |
72
|
|
|
{ |
73
|
1 |
|
return new Blank('required'); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* Gets a rule that requires strings to be non-empty. |
78
|
|
|
* |
79
|
|
|
* @return \Caridea\Validate\Rule\Blank |
80
|
|
|
*/ |
81
|
1 |
|
public static function notEmpty(): Blank |
82
|
|
|
{ |
83
|
1 |
|
return new Blank('empty'); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* Gets a rule that requires an array or `Countable` to be non-empty. |
88
|
|
|
* |
89
|
|
|
* @return \Caridea\Validate\Rule\Blank |
90
|
|
|
*/ |
91
|
1 |
|
public static function notEmptyList(): Blank |
92
|
|
|
{ |
93
|
1 |
|
return new Blank('list'); |
94
|
|
|
} |
95
|
|
|
} |
96
|
|
|
|
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.