-
-
Notifications
You must be signed in to change notification settings - Fork 119
/
Copy pathServerTypeInfo.php
122 lines (106 loc) · 3.03 KB
/
ServerTypeInfo.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
114
115
116
117
118
119
120
121
122
<?php
namespace Office365\Runtime;
use Exception;
use Office365\Complex;
use Office365\GraphServiceClient;
use Office365\SharePoint\ClientContext;
class ServerTypeInfo
{
/**
* @param string $namespace
* @param string $name
* @param boolean $collection
*/
public function __construct($namespace = null, $name = null, $collection=false)
{
$this->Namespace = $namespace;
$this->Name = $name;
$this->Id = null;
$this->Collection = $collection;
}
/**
* @param string $value
* @return ServerTypeInfo
*/
public static function fromFullName($value){
$parts = explode(".", $value);
$typeName = end($parts);
$namespace = implode(".", array_slice($parts, 0,-1));
return new ServerTypeInfo($namespace, $typeName);
}
/**
* @return ServerTypeInfo
* @throws Exception
*/
public static function primitive($typeName, $isCollection = false)
{
if (array_key_exists($typeName, self::$PrimitiveTypeMappings)) {
$primitiveTypeName = self::$PrimitiveTypeMappings[$typeName];
return new ServerTypeInfo("Edm", $primitiveTypeName, $isCollection);
}
throw new Exception("Unknown primitive type: $typeName");
}
/**
* @param ClientValue|ClientObject $type
* @return ServerTypeInfo
*/
public static function resolve($type)
{
if($type instanceof ClientValueCollection || $type instanceof ClientObjectCollection){
$itemTypeName = $type->getItemTypeName();
$collection = true;
}
else {
$itemTypeName = get_class($type);
$collection = false;
}
$parts = explode("\\", $itemTypeName);
$typeName = end($parts);
$namespace = null;
if($type instanceof ClientObject){
$context = $type->getContext();
if ($context instanceof ClientContext) {
if(is_null($namespace)) $namespace = "SP";
}
else if ($context instanceof GraphServiceClient) {
if(is_null($namespace)) $namespace = "microsoft.graph";
$typeName = lcfirst($typeName);
}
}
elseif($type instanceof Complex){
if(is_null($namespace)) $namespace = "microsoft.graph";
$typeName = lcfirst($typeName);
}
return new ServerTypeInfo($namespace, $typeName, $collection);
}
public function __toString()
{
$fullName = "$this->Namespace.$this->Name";
return $this->Collection ? "Collection($fullName)" : $fullName;
}
/**
* @var string
*/
public $Id;
/**
* @var string
*/
public $Namespace;
/**
* @var string
*/
public $Name;
/**
* @var boolean
*/
public $Collection;
/**
* @var string[]
*/
static $PrimitiveTypeMappings = array(
"string" => "String",
"bool" => "Boolean",
"integer" => "Int32",
"double" => "Double"
);
}