Skip to content

Commit 62fb425

Browse files
committed
Optimize odata request payload serializing, fixes for saving multi-choice field value (#261)
1 parent 04a2b2c commit 62fb425

27 files changed

+290
-95
lines changed

examples/SharePoint/ListItems/SetLookupFieldValue.php renamed to examples/SharePoint/ListItems/SetFieldValues.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use Office365\Runtime\Auth\ClientCredential;
88
use Office365\SharePoint\ClientContext;
99
use Office365\SharePoint\FieldLookupValue;
10+
use Office365\SharePoint\FieldMultiChoiceValue;
1011
use Office365\SharePoint\FieldMultiLookupValue;
1112
use Office365\SharePoint\FieldUserValue;
1213
use Office365\SharePoint\ListItem;
@@ -28,6 +29,7 @@
2829
'Title' => "New task N#" . rand(1, 100000),
2930
'ParentTask' => new FieldLookupValue($taskId),
3031
'PrimaryManager' => new FieldUserValue($me->getId()),
31-
'Managers' => new FieldMultiLookupValue([$me->getId()])
32+
'Managers' => new FieldMultiLookupValue([$me->getId()]),
33+
'TaskCategories' => new FieldMultiChoiceValue(["Event", "Reminder"])
3234
);
3335
$item = $list->addItem($taskProps)->executeQuery();

src/Outlook/OutlookItem.php

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,7 @@ class OutlookItem extends Entity
1414
*/
1515
public function getChangeKey()
1616
{
17-
if (!$this->isPropertyAvailable("ChangeKey")) {
18-
return null;
19-
}
20-
return $this->getProperty("ChangeKey");
17+
return $this->getProperty("ChangeKey", null);
2118
}
2219
/**
2320
* @var string
@@ -31,10 +28,7 @@ public function setChangeKey($value)
3128
*/
3229
public function getCategories()
3330
{
34-
if (!$this->isPropertyAvailable("Categories")) {
35-
return null;
36-
}
37-
return $this->getProperty("Categories");
31+
return $this->getProperty("Categories", null);
3832
}
3933
/**
4034
* @var array

src/Runtime/ClientObject.php

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -127,14 +127,6 @@ public function getParentCollection()
127127
return $this->parentCollection;
128128
}
129129

130-
/**
131-
* @return null
132-
*/
133-
protected function getServerTypeId()
134-
{
135-
return null;
136-
}
137-
138130

139131
/**
140132
* @return ClientRuntimeContext
@@ -211,11 +203,11 @@ public function select($value)
211203

212204
/**
213205
* Gets entity type name
214-
* @return string
206+
* @return ServerTypeInfo
215207
*/
216-
public function getServerTypeName()
208+
public function getServerTypeInfo()
217209
{
218-
return null;
210+
return ServerTypeInfo::resolve($this);
219211
}
220212

221213
/**

src/Runtime/ClientResult.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ public function executeQuery(){
5050

5151

5252
/**
53-
* @return bool|int|ClientObject|ClientValue|string|null
53+
* @return bool|int|string|ClientObject|ClientValue|null
5454
*/
5555
public function getValue(){
5656
return $this->value;

src/Runtime/ClientValue.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,11 +51,11 @@ public function getProperty($name, $defaultValue=null)
5151

5252

5353
/**
54-
* @return string
54+
* @return ServerTypeInfo
5555
*/
56-
public function getServerTypeName()
56+
public function getServerTypeInfo()
5757
{
58-
return null;
58+
return ServerTypeInfo::resolve($this);
5959
}
6060

6161
/**

src/Runtime/ClientValueCollection.php

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ public static function fromArray($itemTypeName,$values)
3030

3131
/**
3232
* Adds property to collection
33-
* @param ClientValue $value
33+
* @param ClientValue|string $value
3434
*/
3535
public function addChild($value)
3636
{
@@ -81,19 +81,12 @@ public function createType()
8181
*/
8282
function getItemTypeName()
8383
{
84-
if(isset($this->itemTypeName))
84+
if(isset($this->itemTypeName)) {
8585
return $this->itemTypeName;
86+
}
8687
return str_replace("Collection","",get_class($this));
8788
}
8889

89-
/**
90-
* @return string|null
91-
*/
92-
public function getServerTypeName()
93-
{
94-
return null;
95-
}
96-
9790
/**
9891
* @return array
9992
*/

src/Runtime/OData/ODataRequest.php

Lines changed: 6 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -75,30 +75,8 @@ public function buildRequest(){
7575
*/
7676
public function normalizeTypeName($type)
7777
{
78-
$collection = false;
79-
$typeName = $type->getServerTypeName();
80-
if (is_null($typeName)) {
81-
82-
if ($type instanceof ClientValueCollection) {
83-
$collection = true;
84-
$typeInfo = explode("\\", $type->getItemTypeName());
85-
}
86-
else{
87-
$typeInfo = explode("\\", get_class($type));
88-
}
89-
$typeName = end($typeInfo);
90-
91-
//if ($this->context instanceof OutlookClient)
92-
// $typeName = "#Microsoft.OutlookServices.$typeName";
93-
if ($this->context instanceof ClientContext)
94-
$typeName = "SP.$typeName";
95-
else if ($this->context instanceof GraphServiceClient) {
96-
$typeName = lcfirst($typeName);
97-
$typeName = "microsoft.graph.$typeName";
98-
}
99-
return $collection ? "Collection($typeName)" : $typeName;
100-
}
101-
return $typeName;
78+
$typeInfo = $type->getServerTypeInfo()->patch($this->context);
79+
return (string)$typeInfo;
10280
}
10381

10482

@@ -119,8 +97,8 @@ protected function normalizePayload($value,ODataFormat $format)
11997
return $this->normalizePayload($property,$format);
12098
}, $value->toJson(true));
12199

122-
if(!($value instanceof ClientValueCollection || $value instanceof ClientObjectCollection))
123-
$this->ensureAnnotation($value,$json,$format);
100+
101+
$this->ensureAnnotation($value,$json,$format);
124102
return $json;
125103
} else if (is_array($value)) {
126104
return array_map(function ($item) use($format){
@@ -147,7 +125,8 @@ protected function ensureAnnotation($type, &$json,$format)
147125
}
148126
}
149127
elseif ($format instanceof JsonFormat){
150-
$json[$format->TypeTag] = "$typeName";
128+
if(!($type instanceof ClientValueCollection))
129+
$json[$format->TypeTag] = "$typeName";
151130
}
152131
}
153132

src/Runtime/ServerTypeInfo.php

Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
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+
}

src/SharePoint/ContentTypeCreationInformation.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
namespace Office365\SharePoint;
55
use Office365\Runtime\ClientValue;
6+
use Office365\Runtime\ServerTypeInfo;
67

78
/**
89
* Specifies properties that are used as parameters to initialize a new content type.
@@ -15,9 +16,12 @@ public function __construct()
1516
}
1617

1718

18-
public function getServerTypeName()
19+
/**
20+
* @return ServerTypeInfo
21+
*/
22+
public function getServerTypeInfo()
1923
{
20-
return "SP.ContentType";
24+
return new ServerTypeInfo("SP", "ContentType");
2125
}
2226

2327

src/SharePoint/FieldCreationInformation.php

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
namespace Office365\SharePoint;
77

88
use Office365\Runtime\ClientValue;
9+
use Office365\Runtime\ServerTypeInfo;
10+
911
/**
1012
* Represents properties that can be set when creating a field.
1113
*/
@@ -16,9 +18,12 @@ public function __construct()
1618
parent::__construct();
1719
}
1820

19-
public function getServerTypeName()
21+
/**
22+
* @return ServerTypeInfo
23+
*/
24+
public function getServerTypeInfo()
2025
{
21-
return "SP.Field";
26+
return new ServerTypeInfo("SP", "Field");
2227
}
2328

2429
/**

0 commit comments

Comments
 (0)