1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
namespace Limoncello\Validation\Validator; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* Copyright 2015-2019 [email protected] |
7
|
|
|
* |
8
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License"); |
9
|
|
|
* you may not use this file except in compliance with the License. |
10
|
|
|
* You may obtain a copy of the License at |
11
|
|
|
* |
12
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0 |
13
|
|
|
* |
14
|
|
|
* Unless required by applicable law or agreed to in writing, software |
15
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, |
16
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
17
|
|
|
* See the License for the specific language governing permissions and |
18
|
|
|
* limitations under the License. |
19
|
|
|
*/ |
20
|
|
|
|
21
|
|
|
use Limoncello\Validation\Contracts\Rules\RuleInterface; |
22
|
|
|
use Limoncello\Validation\Rules\Converters\StringArrayToIntArray; |
23
|
|
|
use Limoncello\Validation\Rules\Converters\StringToArray; |
24
|
|
|
use Limoncello\Validation\Rules\Converters\StringToBool; |
25
|
|
|
use Limoncello\Validation\Rules\Converters\StringToDateTime; |
26
|
|
|
use Limoncello\Validation\Rules\Converters\StringToFloat; |
27
|
|
|
use Limoncello\Validation\Rules\Converters\StringToInt; |
28
|
|
|
use Limoncello\Validation\Rules\Generic\AndOperator; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @package Limoncello\Validation |
32
|
|
|
*/ |
33
|
|
|
trait Converters |
34
|
|
|
{ |
35
|
|
|
/** |
36
|
|
|
* @param RuleInterface|null $next |
37
|
|
|
* |
38
|
|
|
* @return RuleInterface |
39
|
|
|
*/ |
40
|
2 |
|
protected static function stringToBool(RuleInterface $next = null): RuleInterface |
41
|
|
|
{ |
42
|
2 |
|
return $next === null ? new StringToBool() : new AndOperator(new StringToBool(), $next); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @param string $format |
47
|
|
|
* @param RuleInterface|null $next |
48
|
|
|
* |
49
|
|
|
* @return RuleInterface |
50
|
|
|
*/ |
51
|
2 |
|
protected static function stringToDateTime(string $format, RuleInterface $next = null): RuleInterface |
52
|
|
|
{ |
53
|
2 |
|
return $next === null ? new StringToDateTime($format) : new AndOperator(new StringToDateTime($format), $next); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @param RuleInterface|null $next |
58
|
|
|
* |
59
|
|
|
* @return RuleInterface |
60
|
|
|
*/ |
61
|
2 |
|
protected static function stringToFloat(RuleInterface $next = null): RuleInterface |
62
|
|
|
{ |
63
|
2 |
|
return $next === null ? new StringToFloat() : new AndOperator(new StringToFloat(), $next); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* @param RuleInterface|null $next |
68
|
|
|
* |
69
|
|
|
* @return RuleInterface |
70
|
|
|
*/ |
71
|
4 |
|
protected static function stringToInt(RuleInterface $next = null): RuleInterface |
72
|
|
|
{ |
73
|
4 |
|
return $next === null ? new StringToInt() : new AndOperator(new StringToInt(), $next); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* @param RuleInterface|null $next |
78
|
|
|
* |
79
|
|
|
* @return RuleInterface |
80
|
|
|
*/ |
81
|
1 |
|
protected static function stringArrayToIntArray(RuleInterface $next = null): RuleInterface |
82
|
|
|
{ |
83
|
1 |
|
return $next === null ? new StringArrayToIntArray() : new AndOperator(new StringArrayToIntArray(), $next); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* @param string $delimiter |
88
|
|
|
* @param int $limit |
89
|
|
|
* @param RuleInterface|null $next |
90
|
|
|
* |
91
|
|
|
* @return RuleInterface |
92
|
|
|
*/ |
93
|
1 |
|
protected static function stringToArray( |
94
|
|
|
string $delimiter, |
95
|
|
|
int $limit = PHP_INT_MAX, |
96
|
|
|
RuleInterface $next = null |
97
|
|
|
): RuleInterface { |
98
|
1 |
|
return $next === null ? |
99
|
1 |
|
new StringToArray($delimiter, $limit) : new AndOperator(new StringToArray($delimiter, $limit), $next); |
100
|
|
|
} |
101
|
|
|
} |
102
|
|
|
|