1
+ <?php
2
+
3
+ namespace Office365 \Runtime ;
4
+
5
+ use Exception ;
6
+ use Office365 \GraphServiceClient ;
7
+ use Office365 \SharePoint \ClientContext ;
8
+
9
+ class ServerTypeInfo
10
+ {
11
+
12
+ /**
13
+ * @param string $namespace
14
+ * @param string $name
15
+ * @param boolean $collection
16
+ */
17
+ public function __construct ($ namespace = null , $ name = null , $ collection =false )
18
+ {
19
+ $ this ->Namespace = $ namespace ;
20
+ $ this ->Name = $ name ;
21
+ $ this ->Id = null ;
22
+ $ this ->Collection = $ collection ;
23
+ }
24
+
25
+ /**
26
+ * @param string $value
27
+ * @return ServerTypeInfo
28
+ */
29
+ public static function fromFullName ($ value ){
30
+ $ parts = explode (". " , $ value );
31
+ $ typeName = end ($ parts );
32
+ $ namespace = implode (". " , array_slice ($ parts , 0 ,-1 ));
33
+ return new ServerTypeInfo ($ namespace , $ typeName );
34
+ }
35
+
36
+ /**
37
+ * @return ServerTypeInfo
38
+ * @throws Exception
39
+ */
40
+ public static function primitive ($ typeName , $ isCollection = false )
41
+ {
42
+ if (array_key_exists ($ typeName , self ::$ PrimitiveTypeMappings )) {
43
+ $ primitiveTypeName = self ::$ PrimitiveTypeMappings [$ typeName ];
44
+ return new ServerTypeInfo ("Edm " , $ primitiveTypeName , $ isCollection );
45
+ }
46
+ throw new Exception ("Unknown primitive type: $ typeName " );
47
+ }
48
+
49
+ /**
50
+ * @param ClientValue|ClientObject $type
51
+ * @return ServerTypeInfo
52
+ */
53
+ public static function resolve ($ type )
54
+ {
55
+ if ($ type instanceof ClientValueCollection || $ type instanceof ClientObjectCollection){
56
+ $ itemTypeName = $ type ->getItemTypeName ();
57
+ $ collection = true ;
58
+ }
59
+ else {
60
+ $ itemTypeName = get_class ($ type );
61
+ $ collection = false ;
62
+ }
63
+ $ parts = explode ("\\" , $ itemTypeName );
64
+ $ typeName = end ($ parts );
65
+ //$namespace = implode(".", array_slice($parts, 1, count($parts) - 2));
66
+ return new ServerTypeInfo (null , $ typeName , $ collection );
67
+ }
68
+
69
+ /**
70
+ * @param ClientRuntimeContext $context
71
+ */
72
+ public function patch ($ context ){
73
+ if ($ context instanceof ClientContext) {
74
+ if (is_null ($ this ->Namespace )) $ this ->Namespace = "SP " ;
75
+ }
76
+ else if ($ context instanceof GraphServiceClient) {
77
+ if (is_null ($ this ->Namespace )) $ this ->Namespace = "microsoft.graph " ;
78
+ $ this ->Name = lcfirst ($ this ->Name );
79
+ }
80
+ return $ this ;
81
+ }
82
+
83
+ public function __toString ()
84
+ {
85
+ $ fullName = "$ this ->Namespace . $ this ->Name " ;
86
+ return $ this ->Collection ? "Collection( $ fullName) " : $ fullName ;
87
+ }
88
+
89
+
90
+ /**
91
+ * @var string
92
+ */
93
+ public $ Id ;
94
+
95
+ /**
96
+ * @var string
97
+ */
98
+ public $ Namespace ;
99
+
100
+ /**
101
+ * @var string
102
+ */
103
+ public $ Name ;
104
+
105
+
106
+ /**
107
+ * @var boolean
108
+ */
109
+ public $ Collection ;
110
+
111
+ /**
112
+ * @var string[]
113
+ */
114
+ static $ PrimitiveTypeMappings = array (
115
+ "string " => "String " ,
116
+ "bool " => "Boolean " ,
117
+ "integer " => "Int32 " ,
118
+ "double " => "Double "
119
+ );
120
+
121
+ }
0 commit comments