1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace AlgoWeb\ODataMetadata\IsOKTraits; |
4
|
|
|
|
5
|
|
|
use AlgoWeb\ODataMetadata\IsOK; |
6
|
|
|
|
7
|
|
|
trait IsOKToolboxTrait |
8
|
|
|
{ |
9
|
|
|
protected function isStringNotNullOrEmpty($str) |
10
|
|
|
{ |
11
|
|
|
if (!$this->isStringNotNull($str)) { |
12
|
|
|
return false; |
13
|
|
|
} |
14
|
|
|
if (empty(trim($str))) { |
15
|
|
|
return false; |
16
|
|
|
} |
17
|
|
|
return true; |
18
|
|
|
} |
19
|
|
|
|
20
|
|
|
protected function isStringNotNull($str) |
21
|
|
|
{ |
22
|
|
|
if (null == $str) { |
23
|
|
|
return false; |
24
|
|
|
} |
25
|
|
|
if (!is_string($str)) { |
26
|
|
|
return false; |
27
|
|
|
} |
28
|
|
|
return true; |
29
|
|
|
} |
30
|
|
|
|
31
|
|
View Code Duplication |
protected function isNotNullInstanceOf($var, $instanceOf) |
|
|
|
|
32
|
|
|
{ |
33
|
|
|
if (null == $var) { |
34
|
|
|
return false; |
35
|
|
|
} |
36
|
|
|
if (!($var instanceof $instanceOf)) { |
37
|
|
|
return false; |
38
|
|
|
} |
39
|
|
|
return true; |
40
|
|
|
} |
41
|
|
|
|
42
|
|
View Code Duplication |
protected function isNullInstanceOf($var, $instanceOf) |
|
|
|
|
43
|
|
|
{ |
44
|
|
|
if (null == $var) { |
45
|
|
|
return true; |
46
|
|
|
} |
47
|
|
|
if (!($var instanceof $instanceOf)) { |
48
|
|
|
return false; |
49
|
|
|
} |
50
|
|
|
return false; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
protected function isValidArray(array $arr, $instanceOf, $minCount = -1, $maxCount = -1) |
54
|
|
|
{ |
55
|
|
|
$numberOfItem = count($arr); |
56
|
|
|
if (-1 != $minCount && $numberOfItem < $minCount) { |
57
|
|
|
return false; |
58
|
|
|
} |
59
|
|
|
if (-1 != $maxCount && $numberOfItem > $maxCount) { |
60
|
|
|
return false; |
61
|
|
|
} |
62
|
|
|
foreach ($arr as $item) { |
63
|
|
|
if (!($item instanceof $instanceOf)) { |
64
|
|
|
return false; |
65
|
|
|
} |
66
|
|
|
} |
67
|
|
|
return true; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
protected function isChildArrayOK(array $arr, &$msg) |
71
|
|
|
{ |
72
|
|
|
foreach ($arr as $item) { |
73
|
|
|
if (!($item instanceof IsOK)) { |
74
|
|
|
$msg = "Child item is not an instance of IsOK"; |
75
|
|
|
return false; |
76
|
|
|
} |
77
|
|
|
if (!$item->isOK($msg)) { |
78
|
|
|
return false; |
79
|
|
|
} |
80
|
|
|
} |
81
|
|
|
return true; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
protected function isURLValid($url) |
85
|
|
|
{ |
86
|
|
|
if (!$this->isStringNotNull($url)) { |
87
|
|
|
return false; |
88
|
|
|
} |
89
|
|
|
if (false === filter_var($url, FILTER_VALIDATE_URL)) { |
90
|
|
|
return false; |
91
|
|
|
} |
92
|
|
|
return true; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
protected function isObjectNullOrOK(IsOK $object = null, &$msg = null) |
96
|
|
|
{ |
97
|
|
|
if (null == $object) { |
98
|
|
|
return true; |
99
|
|
|
} |
100
|
|
|
return $object->isOK($msg); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
protected function isObjectNullOrType($instanceOf, IsOK $object = null) |
104
|
|
|
{ |
105
|
|
|
if (null == $object) { |
106
|
|
|
return true; |
107
|
|
|
} |
108
|
|
|
return $object instanceof $instanceOf; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
protected function isValidArrayOK(array $arr, $instanceOf, &$msg = null, $minCount = -1, $maxCount = -1) |
112
|
|
|
{ |
113
|
|
|
$result = $this->isValidArray($arr, $instanceOf, $minCount, $maxCount); |
114
|
|
|
if (!$result) { |
115
|
|
|
$msg = "Supplied array not a valid array"; |
116
|
|
|
return false; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
return $this->isChildArrayOK($arr, $msg); |
120
|
|
|
} |
121
|
|
|
} |
122
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.