-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathTypes.php
More file actions
123 lines (114 loc) · 3.62 KB
/
Types.php
File metadata and controls
123 lines (114 loc) · 3.62 KB
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
114
115
116
117
118
119
120
121
122
123
<?php
namespace Clue\QDataStream;
final class Types
{
// https://github.com/sandsmark/QuasselDroid/blob/master/QuasselDroid/src/main/java/com/iskrembilen/quasseldroid/qtcomm/QMetaType.java
const TYPE_BOOL = 1;
const TYPE_INT = 2;
const TYPE_UINT = 3;
const TYPE_QCHAR = 7;
const TYPE_QVARIANT_MAP = 8;
const TYPE_QVARIANT_LIST = 9;
const TYPE_QSTRING = 10;
const TYPE_QSTRING_LIST = 11;
const TYPE_QBYTE_ARRAY = 12;
const TYPE_QTIME = 15;
const TYPE_QDATETIME = 16;
const TYPE_QUSER_TYPE = 127;
const TYPE_SHORT = 130;
const TYPE_CHAR = 131;
const TYPE_USHORT = 133;
const TYPE_UCHAR = 134;
/**
* Tries to guess the type constant based on the data type of the given value
*
* @param mixed $value
* @return int see TYPE_* constants
* @throws \InvalidArgumentException if type can not be guessed
*/
public static function getTypeByValue($value)
{
if (is_int($value)) {
return self::TYPE_INT;
} elseif (is_string($value)) {
return self::TYPE_QSTRING;
} elseif (is_bool($value)) {
return self::TYPE_BOOL;
} elseif (self::isList($value)) {
return self::TYPE_QVARIANT_LIST;
} elseif (self::isMap($value)) {
return self::TYPE_QVARIANT_MAP;
} elseif ($value instanceof \DateTime) {
return self::TYPE_QDATETIME;
} else {
throw new \InvalidArgumentException('Can not guess variant type for type "' . gettype($value) . '"');
}
}
/**
* Returns the type name string for the given type constant
*
* @param int $type
* @return string
* @throws \InvalidArgumentException
*/
public static function getNameByType($type)
{
static $map = array(
self::TYPE_BOOL => 'Bool',
self::TYPE_INT => 'Int',
self::TYPE_UINT => 'UInt',
self::TYPE_QCHAR => 'QChar',
self::TYPE_QVARIANT_MAP => 'QVariantMap',
self::TYPE_QVARIANT_LIST => 'QVariantList',
self::TYPE_QSTRING => 'QString',
self::TYPE_QSTRING_LIST => 'QStringList',
self::TYPE_QBYTE_ARRAY => 'QByteArray',
self::TYPE_QTIME => 'QTime',
self::TYPE_QDATETIME => 'QDateTime',
self::TYPE_QUSER_TYPE => 'QUserType',
self::TYPE_SHORT => 'Short',
self::TYPE_CHAR => 'Char',
self::TYPE_USHORT => 'UShort',
self::TYPE_UCHAR => 'UChar',
);
if (!isset($map[$type])) {
throw new \InvalidArgumentException('Invalid/unknown variant type (' . $type . ')');
}
return $map[$type];
}
/**
* Checks whether the given argument is a list (vector array)
*
* An empty array is considered both a list and a map.
*
* @param mixed $array
* @return bool
*/
public static function isList($array)
{
if (!is_array($array)) {
return false;
}
$expected = 0;
foreach ($array as $key => $unused) {
if ($key !== $expected++) {
return false;
}
}
return true;
}
/**
* Checks whether the given argument is a map (hash map / assoc array or stdClass)
*
* An empty array is considered both a list and a map.
*
* An empty stdClass is considered a map.
*
* @param mixed $value
* @return bool
*/
public static function isMap($value)
{
return ($value === array() || (is_array($value) && !self::isList($value)) || $value instanceof \stdClass);
}
}