-
Notifications
You must be signed in to change notification settings - Fork 0
/
DataType.php
113 lines (97 loc) · 2.91 KB
/
DataType.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
<?php
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
/**
* Description of DataType
*
* @author opravil.jan
*/
namespace Femoz\Utils;
class DataType extends \Nette\Object {
//put your code here
private static $accepStringBool = false;
public static function setAcceptStringBool($value){
self::$accepStringBool = $value;
}
public static function isStringBool($value){
$value = \Nette\Utils\Strings::trim(\Nette\Utils\Strings::normalize(\Nette\Utils\Strings::upper(\Nette\Utils\Strings::fixEncoding($value))));
$result = \Nette\Utils\Strings::match($value, "/TRUE|FALSE/");
if ($result !== null){
return true;
}
return false;
}
private static function fixStringBool($value){
$value = \Nette\Utils\Strings::trim(\Nette\Utils\Strings::normalize(\Nette\Utils\Strings::upper(\Nette\Utils\Strings::fixEncoding($value))));
switch ($value){
case "TRUE" :
return true;
break;
case "FALSE" :
return false;
break;
}
}
public static function isBool($value) {
if (is_bool($value) === true) {
return true;
}
return false;
}
public static function isInt($value) {
if (is_numeric($value) === true) {
if (ctype_digit($value) === true) {
return true;
}
}
return false;
}
public static function isFloat($value) {
if (is_numeric($value) === true) {
if (ctype_digit($value) === false) {
return true;
}
}
return false;
}
public static function isHexadecimal($value) {
if (ctype_xdigit($value) === true) {
return true;
}
return false;
}
public static function isArray($value) {
if (is_array($value) === true) {
return true;
}
return false;
}
public static function isString($value) {
if (is_string($value) === true) {
return true;
}
return false;
}
public static function fixDataType($value){
if (self::$accepStringBool === true){
if (self::isStringBool($value) === true){
return self::fixStringBool($value);
}
}
if(self::isBool($value) === true){
return (bool) $value;
} elseif (self::isInt($value) === true){
return (int) $value;
} elseif(self::isFloat($value)){
return (float) $value;
} elseif (self::isArray($value) === true){
return $value;
} elseif(self::isString($value)){
return \Nette\Utils\Strings::fixEncoding(\Nette\Utils\Strings::trim(\Nette\Utils\Strings::normalize($value)));
}
return $value;
}
}
?>