Validator   A
last analyzed

Complexity

Total Complexity 13

Size/Duplication

Total Lines 103
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 0
Metric Value
wmc 13
lcom 1
cbo 0
dl 0
loc 103
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A parseStringRule() 0 15 2
A parseParameters() 0 8 2
A explodeRule() 0 4 1
C convertValidations() 0 44 8
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
0 ignored issues
show
Bug introduced by
There is no parameter named $rules. Was it maybe removed?

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 method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
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)
0 ignored issues
show
Bug Best Practice introduced by
The expression $attribute of type null|string is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
101
					$validationsConverted[$attribute] = $attributeValue ;
102
103
			}
104
			
105
		}
106
107
		return $validationsConverted ;
108
	}
109
110
}