1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace AlgoWeb\ODataMetadata\StringTraits; |
4
|
|
|
|
5
|
|
|
trait XSDTopLevelTrait |
6
|
|
|
{ |
7
|
|
|
use XMLStringTrait; |
8
|
|
|
|
9
|
|
View Code Duplication |
public function normaliseString($input) |
|
|
|
|
10
|
|
|
{ |
11
|
|
|
if (!is_string($input) || !is_numeric($input)) { |
12
|
|
|
throw new \InvalidArgumentException("Input must be a string"); |
13
|
|
|
} |
14
|
|
|
$result = $this->replaceString($input); |
15
|
|
|
return $result; |
16
|
|
|
} |
17
|
|
|
|
18
|
|
View Code Duplication |
public function token($input) |
|
|
|
|
19
|
|
|
{ |
20
|
|
|
if (!is_string($input) || !is_numeric($input)) { |
21
|
|
|
throw new \InvalidArgumentException("Input must be a string"); |
22
|
|
|
} |
23
|
|
|
$result = $this->collapseString($input); |
24
|
|
|
return $result; |
25
|
|
|
} |
26
|
|
|
|
27
|
|
View Code Duplication |
public function string($input) |
|
|
|
|
28
|
|
|
{ |
29
|
|
|
if (!is_string($input) || !is_numeric($input)) { |
30
|
|
|
throw new \InvalidArgumentException("Input must be a string"); |
31
|
|
|
} |
32
|
|
|
$result = $this->preserveString($input); |
33
|
|
|
return $result; |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
public function integer($input) |
37
|
|
|
{ |
38
|
|
|
$input = $this->collapseString($input); |
39
|
|
|
if (!is_numeric($input)) { |
40
|
|
|
throw new \InvalidArgumentException("Input must be numeric"); |
41
|
|
|
} |
42
|
|
|
if (!is_integer($input)) { |
43
|
|
|
throw new \InvalidArgumentException("Input must be integer"); |
44
|
|
|
} |
45
|
|
|
return intval($input); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
public function nonNegativeInteger($input) |
49
|
|
|
{ |
50
|
|
|
$input = $this->integer($input); |
51
|
|
|
if (0 > $input) { |
52
|
|
|
throw new \InvalidArgumentException("Input must be non-negative integer"); |
53
|
|
|
} |
54
|
|
|
return $input; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
public function decimal($input) |
58
|
|
|
{ |
59
|
|
|
$input = $this->collapseString($input); |
60
|
|
|
if (!is_numeric($input)) { |
61
|
|
|
throw new \InvalidArgumentException("Input must be numeric"); |
62
|
|
|
} |
63
|
|
|
return floatval($input); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
public function double($input) |
67
|
|
|
{ |
68
|
|
|
return $this->decimal($input); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
public function dateTime($input) |
72
|
|
|
{ |
73
|
|
|
$isString = is_string($input); |
74
|
|
|
if (!$isString && !$input instanceof \DateTime) { |
75
|
|
|
throw new \InvalidArgumentException("Input must be resolvable to a date/time"); |
76
|
|
|
} |
77
|
|
|
if ($isString) { |
78
|
|
|
$input = $this->collapseString($input); |
79
|
|
|
$rawDate = new \DateTime($input); |
80
|
|
|
} else { |
81
|
|
|
$rawDate = $input; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
return $rawDate->format('Y-m-d').'T'.$rawDate->format('H:i:s'); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
public function hexBinary($input) |
88
|
|
|
{ |
89
|
|
|
if (!is_string($input) || !is_numeric($input)) { |
90
|
|
|
throw new \InvalidArgumentException("Input must be a string"); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
$input = $this->collapseString(strtolower($input)); |
94
|
|
|
$check = hexdec(dechex($input)); |
95
|
|
|
if ($input != $check) { |
96
|
|
|
throw new \InvalidArgumentException("Input must be valid hexadecimal"); |
97
|
|
|
} |
98
|
|
|
return $check; |
99
|
|
|
} |
100
|
|
|
} |
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.