|
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-2016 LibreWorks contributors |
|
19
|
|
|
* @license http://opensource.org/licenses/Apache-2.0 Apache 2.0 License |
|
20
|
|
|
*/ |
|
21
|
|
|
namespace Caridea\Validate\Rule; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* Rules for empty values |
|
25
|
|
|
* |
|
26
|
|
|
* @copyright 2015-2016 LibreWorks contributors |
|
27
|
|
|
* @license http://opensource.org/licenses/Apache-2.0 Apache 2.0 License |
|
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
|
|
|
* Validates the provided value. |
|
48
|
|
|
* |
|
49
|
|
|
* @param mixed $value A value to validate against the rule |
|
50
|
|
|
* @param array|object $data The dataset which contains this field |
|
51
|
|
|
* @return array An array of error codes or null if validation succeeded |
|
52
|
|
|
*/ |
|
53
|
3 |
|
public function apply($value, $data = []) |
|
54
|
|
|
{ |
|
55
|
3 |
|
switch ($this->operator) { |
|
56
|
3 |
|
case "required": |
|
57
|
1 |
|
return $value === null || $value === '' ? ['REQUIRED'] : null; |
|
58
|
2 |
|
case "empty": |
|
59
|
1 |
|
return $value === '' ? ['CANNOT_BE_EMPTY'] : null; |
|
60
|
1 |
|
case "list": |
|
61
|
1 |
|
if (empty($value)) { |
|
62
|
1 |
|
return ['CANNOT_BE_EMPTY']; |
|
63
|
1 |
|
} elseif (!is_array($value) && !($value instanceof \Countable)) { |
|
64
|
1 |
|
return ['FORMAT_ERROR']; |
|
65
|
|
|
} |
|
66
|
1 |
|
return count($value) === 0 ? ['CANNOT_BE_EMPTY'] : null; |
|
67
|
|
|
} |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* Gets a rule that requires values to be non-null and not empty string. |
|
72
|
|
|
* |
|
73
|
|
|
* @return \Caridea\Validate\Rule\Blank the created rule |
|
74
|
|
|
*/ |
|
75
|
1 |
|
public static function required(): Blank |
|
76
|
|
|
{ |
|
77
|
1 |
|
return new Blank('required'); |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
/** |
|
81
|
|
|
* Gets a rule that requires strings to be non-empty. |
|
82
|
|
|
* |
|
83
|
|
|
* @return \Caridea\Validate\Rule\Blank |
|
84
|
|
|
*/ |
|
85
|
1 |
|
public static function notEmpty(): Blank |
|
86
|
|
|
{ |
|
87
|
1 |
|
return new Blank('empty'); |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
/** |
|
91
|
|
|
* Gets a rule that requires an array or `Countable` to be non-empty. |
|
92
|
|
|
* |
|
93
|
|
|
* @return \Caridea\Validate\Rule\Blank |
|
94
|
|
|
*/ |
|
95
|
1 |
|
public static function notEmptyList(): Blank |
|
96
|
|
|
{ |
|
97
|
1 |
|
return new Blank('list'); |
|
98
|
|
|
} |
|
99
|
|
|
} |
|
100
|
|
|
|