1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Scaffolder\Support; |
4
|
|
|
|
5
|
|
|
use Illuminate\Support\Arr; |
6
|
|
|
use Illuminate\Support\Str; |
7
|
|
|
|
8
|
|
|
class Validator |
9
|
|
|
{ |
10
|
|
|
/** |
11
|
|
|
* Parse a string based rule. |
12
|
|
|
* |
13
|
|
|
* @param string $rules |
14
|
|
|
* @return array |
15
|
|
|
*/ |
16
|
|
|
public static function parseStringRule($rules) |
17
|
|
|
{ |
18
|
|
|
$parameters = []; |
19
|
|
|
|
20
|
|
|
// The format for specifying validation rules and parameters follows an |
21
|
|
|
// easy {rule}:{parameters} formatting convention. For instance the |
22
|
|
|
// rule "Max:3" states that the value may only be three letters. |
23
|
|
|
if (strpos($rules, ':') !== false) { |
24
|
|
|
list($rules, $parameter) = explode(':', $rules, 2); |
25
|
|
|
|
26
|
|
|
$parameters = self::parseParameters($rules, $parameter); |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
return [(trim($rules)), $parameters]; |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Parse a parameter list. |
34
|
|
|
* |
35
|
|
|
* @param string $rule |
36
|
|
|
* @param string $parameter |
37
|
|
|
* @return array |
38
|
|
|
*/ |
39
|
|
|
public static function parseParameters($rule, $parameter) |
40
|
|
|
{ |
41
|
|
|
if (strtolower($rule) == 'regex') { |
42
|
|
|
return [$parameter]; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
return str_getcsv($parameter); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Explode the rules into an array of rules. |
50
|
|
|
* |
51
|
|
|
* @param string $rules |
|
|
|
|
52
|
|
|
* @return array |
53
|
|
|
*/ |
54
|
|
|
public static function explodeRule($rule) |
55
|
|
|
{ |
56
|
|
|
return explode('|', $rule); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Convert laravel validations to theme validation angular js. |
61
|
|
|
* |
62
|
|
|
* @param string $validations |
63
|
|
|
* @return array |
64
|
|
|
*/ |
65
|
|
|
public static function convertValidations($validations, $blnSearch = false){ |
66
|
|
|
$validationsConverted = []; |
67
|
|
|
foreach (self::explodeRule($validations) as $validation) { |
68
|
|
|
$validation = self::parseStringRule($validation); |
69
|
|
|
|
70
|
|
|
if(isset($validation[0])){ |
71
|
|
|
|
72
|
|
|
$rule = $validation[0]; |
73
|
|
|
$values = $validation[1]; |
74
|
|
|
|
75
|
|
|
switch ($rule) { |
76
|
|
|
case 'required': |
77
|
|
|
if($blnSearch) { |
78
|
|
|
$attribute = null ; |
79
|
|
|
$attributeValue = null ; |
80
|
|
|
} |
81
|
|
|
else { |
82
|
|
|
$attribute = "required" ; |
83
|
|
|
$attributeValue = null ; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
break; |
87
|
|
|
|
88
|
|
|
case 'max': |
89
|
|
|
case 'min': |
90
|
|
|
$attribute = $rule.'length' ; |
91
|
|
|
$attributeValue = $values[0] ; |
92
|
|
|
break; |
93
|
|
|
|
94
|
|
|
default: |
95
|
|
|
$attribute = null ; |
96
|
|
|
$attributeValue = null ; |
97
|
|
|
break; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
if($attribute) |
|
|
|
|
101
|
|
|
$validationsConverted[$attribute] = $attributeValue ; |
102
|
|
|
|
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
return $validationsConverted ; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
} |
This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.
Consider the following example. The parameter
$italy
is not defined by the methodfinale(...)
.The most likely cause is that the parameter was removed, but the annotation was not.