1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Pinq\Analysis; |
4
|
|
|
|
5
|
|
|
use Pinq\PinqException; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* Static helper to generate and interpret type identifiers. |
9
|
|
|
* |
10
|
|
|
* @author Elliot Levin <[email protected]> |
11
|
|
|
*/ |
12
|
|
|
final class TypeId |
13
|
|
|
{ |
14
|
|
|
private function __construct() |
15
|
|
|
{ |
16
|
|
|
|
17
|
|
|
} |
18
|
|
|
|
19
|
|
|
public static function getObject($class) |
20
|
|
|
{ |
21
|
|
|
return 'object:' . $class; |
22
|
|
|
} |
23
|
|
|
|
24
|
|
|
public static function isObject($id) |
25
|
|
|
{ |
26
|
|
|
return strpos($id, 'object:') === 0; |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
public static function getClassTypeFromId($objectId) |
30
|
|
|
{ |
31
|
|
|
return substr($objectId, strlen('object:')); |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
public static function getComposite(array $typeIds) |
35
|
|
|
{ |
36
|
|
|
return 'composite<' . implode('|', $typeIds) . '>'; |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
public static function isComposite($id) |
40
|
|
|
{ |
41
|
|
|
return strpos($id, 'composite<') === 0 && $id[strlen($id) - 1] === '>'; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
public static function getComposedTypeIdsFromId($compositeId) |
45
|
|
|
{ |
46
|
|
|
return explode('|', substr($compositeId, strlen('composite<'), -strlen('>'))); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
public static function fromValue($value) |
50
|
|
|
{ |
51
|
|
|
switch (gettype($value)) { |
52
|
|
|
case 'string': |
53
|
|
|
return INativeType::TYPE_STRING; |
54
|
|
|
|
55
|
|
|
case 'integer': |
56
|
|
|
return INativeType::TYPE_INT; |
57
|
|
|
|
58
|
|
|
case 'boolean': |
59
|
|
|
return INativeType::TYPE_BOOL; |
60
|
|
|
|
61
|
|
|
case 'double': |
62
|
|
|
return INativeType::TYPE_DOUBLE; |
63
|
|
|
|
64
|
|
|
case 'NULL': |
65
|
|
|
return INativeType::TYPE_NULL; |
66
|
|
|
|
67
|
|
|
case 'array': |
68
|
|
|
return INativeType::TYPE_ARRAY; |
69
|
|
|
|
70
|
|
|
case 'resource': |
71
|
|
|
case 'unknown type': |
72
|
|
|
return INativeType::TYPE_RESOURCE; |
73
|
|
|
|
74
|
|
|
case 'object': |
75
|
|
|
return self::getObject(get_class($value)); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
throw new PinqException('Unknown variable type %s given', gettype($value)); |
79
|
|
|
} |
80
|
|
|
} |
81
|
|
|
|