|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace kalanis\kw_rules\Rules; |
|
4
|
|
|
|
|
5
|
|
|
|
|
6
|
|
|
use kalanis\kw_rules\Interfaces\IRuleFactory; |
|
7
|
|
|
use kalanis\kw_rules\Interfaces\IRules; |
|
8
|
|
|
use kalanis\kw_rules\Exceptions\RuleException; |
|
9
|
|
|
use ReflectionClass; |
|
10
|
|
|
use ReflectionException; |
|
11
|
|
|
|
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* Class Factory |
|
15
|
|
|
* @package kalanis\kw_rules\Rules |
|
16
|
|
|
* Factory for getting rules |
|
17
|
|
|
*/ |
|
18
|
|
|
class Factory implements IRuleFactory |
|
19
|
|
|
{ |
|
20
|
|
|
/** @var array<string, class-string<ARule>> */ |
|
|
|
|
|
|
21
|
|
|
protected static $map = [ |
|
22
|
|
|
IRules::MATCH_ALL => MatchAll::class, |
|
23
|
|
|
IRules::MATCH_ANY => MatchAny::class, |
|
24
|
|
|
IRules::MATCH_ENTRY => MatchByEntry::class, |
|
25
|
|
|
IRules::ALWAYS => Always::class, |
|
26
|
|
|
IRules::EQUALS => Equals::class, |
|
27
|
|
|
IRules::NOT_EQUALS => NotEquals::class, |
|
28
|
|
|
IRules::IN_ARRAY => IsInArray::class, |
|
29
|
|
|
IRules::NOT_IN_ARRAY => IsNotInArray::class, |
|
30
|
|
|
IRules::IS_GREATER_THAN => GreaterThan::class, |
|
31
|
|
|
IRules::IS_LOWER_THAN => LesserThan::class, |
|
32
|
|
|
IRules::IS_GREATER_THAN_EQUALS => GreaterEquals::class, |
|
33
|
|
|
IRules::IS_LOWER_THAN_EQUALS => LesserEquals::class, |
|
34
|
|
|
IRules::IS_NUMERIC => IsNumeric::class, |
|
35
|
|
|
IRules::IS_STRING => IsString::class, |
|
36
|
|
|
IRules::IS_BOOL => IsBool::class, |
|
37
|
|
|
IRules::MATCHES_PATTERN => MatchesPattern::class, |
|
38
|
|
|
IRules::LENGTH_MIN => LengthMin::class, |
|
39
|
|
|
IRules::LENGTH_MAX => LengthMax::class, |
|
40
|
|
|
IRules::LENGTH_EQUALS => LengthEquals::class, |
|
41
|
|
|
IRules::IN_RANGE => InRange::class, |
|
42
|
|
|
IRules::IN_RANGE_EQUALS => InRangeEquals::class, |
|
43
|
|
|
IRules::NOT_IN_RANGE => OutRange::class, |
|
44
|
|
|
IRules::NOT_IN_RANGE_EQUALS => OutRangeEquals::class, |
|
45
|
|
|
IRules::IS_FILLED => IsFilled::class, |
|
46
|
|
|
IRules::IS_NOT_EMPTY => IsFilled::class, |
|
47
|
|
|
IRules::IS_EMPTY => IsEmpty::class, |
|
48
|
|
|
IRules::SATISFIES_CALLBACK => ProcessCallback::class, |
|
49
|
|
|
IRules::IS_EMAIL => IsEmail::class, |
|
50
|
|
|
IRules::IS_DOMAIN => IsDomain::class, |
|
51
|
|
|
IRules::IS_ACTIVE_DOMAIN => IsActiveDomain::class, |
|
52
|
|
|
IRules::URL_EXISTS => UrlExists::class, |
|
53
|
|
|
IRules::IS_JSON_STRING => IsJsonString::class, |
|
54
|
|
|
// IRules::IS_POST_CODE => External\IsPostCode::class, // too many formats for simple check, use regex |
|
55
|
|
|
// IRules::IS_TELEPHONE => External\IsPhone::class, // too many formats for simple check, use regex |
|
56
|
|
|
// IRules::IS_EU_VAT => External\IsEuVat::class, // too many formats, needs some library for checking |
|
57
|
|
|
IRules::IS_DATE => External\IsDate::class, // too many formats, needs some library for checking |
|
58
|
|
|
IRules::IS_DATE_REGEX => External\IsDateRegex::class, // too many formats, needs some library for checking |
|
59
|
|
|
IRules::SAFE_EQUALS_BASIC => Safe\HashedBasicEquals::class, |
|
60
|
|
|
IRules::SAFE_EQUALS_FUNC => Safe\HashedFuncEquals::class, |
|
61
|
|
|
IRules::SAFE_EQUALS_PASS => Safe\HashedPassEquals::class, |
|
62
|
|
|
]; |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* @param string $ruleName |
|
66
|
|
|
* @throws RuleException |
|
67
|
|
|
* @return ARule |
|
68
|
|
|
*/ |
|
69
|
60 |
|
public function getRule(string $ruleName): ARule |
|
70
|
|
|
{ |
|
71
|
60 |
|
if (isset(static::$map[$ruleName])) { |
|
72
|
46 |
|
$rule = static::$map[$ruleName]; |
|
73
|
|
|
try { |
|
74
|
46 |
|
$ref = new ReflectionClass($rule); |
|
75
|
45 |
|
$class = $ref->newInstance(); |
|
76
|
45 |
|
if ($class instanceof ARule) { |
|
77
|
45 |
|
return $class; |
|
78
|
|
|
} |
|
79
|
1 |
|
} catch (ReflectionException $ex) { |
|
80
|
1 |
|
throw new RuleException($ex->getMessage(), $ex->getCode(), $ex); |
|
81
|
|
|
} |
|
82
|
|
|
} |
|
83
|
16 |
|
throw new RuleException(sprintf('Unknown rule %s', $ruleName)); |
|
84
|
|
|
} |
|
85
|
|
|
} |
|
86
|
|
|
|