1
|
|
|
<?php namespace Sample\Validation; |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Copyright 2015-2016 [email protected] (www.neomerx.com) |
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\Validation\Contracts\RuleInterface; |
20
|
|
|
use Limoncello\Validation\Validator as BaseValidator; |
21
|
|
|
use Limoncello\Validation\Validator as v; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @package Sample |
25
|
|
|
*/ |
26
|
|
|
class Validator extends BaseValidator |
27
|
|
|
{ |
28
|
|
|
/** |
29
|
|
|
* @return RuleInterface |
30
|
|
|
*/ |
31
|
|
|
public static function isEmail() |
32
|
|
|
{ |
33
|
|
|
$condition = function ($input) { |
34
|
|
|
return filter_var($input, FILTER_VALIDATE_EMAIL) !== false; |
35
|
|
|
}; |
36
|
|
|
|
37
|
|
|
return v::andX(v::isString(), v::ifX($condition, v::success(), v::fail(Translator::IS_EMAIL))); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @param int $maxLength |
42
|
|
|
* |
43
|
|
|
* @return RuleInterface |
44
|
|
|
*/ |
45
|
|
|
public static function isRequiredString($maxLength) |
46
|
|
|
{ |
47
|
|
|
return v::andX(v::isString(), v::andX(v::required(), v::stringLength(1, $maxLength))); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @param int $maxLength |
52
|
|
|
* |
53
|
|
|
* @return RuleInterface |
54
|
|
|
*/ |
55
|
|
|
public static function isNullOrNonEmptyString($maxLength) |
56
|
|
|
{ |
57
|
|
|
return v::ifX('is_null', v::success(), v::andX(v::isString(), v::stringLength(1, $maxLength))); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @return RuleInterface |
62
|
|
|
*/ |
63
|
|
|
public static function isExistingPaymentPlan() |
64
|
|
|
{ |
65
|
|
|
// emulate database request |
66
|
|
|
$existsInDatabase = function ($recordId) { |
67
|
|
|
return $recordId < 3; |
68
|
|
|
}; |
69
|
|
|
|
70
|
|
|
return v::callableX($existsInDatabase, Translator::IS_EXISTING_PAYMENT_PLAN); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* @return RuleInterface |
75
|
|
|
*/ |
76
|
|
|
public static function isListOfStrings() |
77
|
|
|
{ |
78
|
|
|
return v::eachX(v::andX(v::isString(), v::stringLength(1))); |
79
|
|
|
} |
80
|
|
|
} |
81
|
|
|
|