Skip to content

Commit 79f5cda

Browse files
committed
Refactorings for OData request/response processing
1 parent 10ddce4 commit 79f5cda

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

48 files changed

+650
-775
lines changed

src/Discovery/DiscoveryClient.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
use Office365\PHP\Client\Runtime\Auth\IAuthenticationContext;
66
use Office365\PHP\Client\Runtime\ClientAction;
77
use Office365\PHP\Client\Runtime\ClientRuntimeContext;
8-
use Office365\PHP\Client\Runtime\OData\JsonSerializerContext;
8+
use Office365\PHP\Client\Runtime\OData\JsonFormat;
99
use Office365\PHP\Client\Runtime\OData\ODataMetadataLevel;
1010
use Office365\PHP\Client\Runtime\Office365Version;
1111
use Office365\PHP\Client\Runtime\ResourcePathEntity;
@@ -16,7 +16,7 @@ class DiscoveryClient extends ClientRuntimeContext
1616
public function __construct(IAuthenticationContext $authContext, $version = Office365Version::V1)
1717
{
1818
$serviceRootUrl = "https://api.office.com/discovery/$version/";
19-
parent::__construct($serviceRootUrl, $authContext,new JsonSerializerContext(ODataMetadataLevel::NoMetadata),$version);
19+
parent::__construct($serviceRootUrl, $authContext,new JsonFormat(ODataMetadataLevel::NoMetadata),$version);
2020
}
2121

2222

@@ -32,4 +32,4 @@ public function getAllServices()
3232
$this->addQuery($qry,$allServices);
3333
return $allServices;
3434
}
35-
}
35+
}

src/GraphClient/GraphServiceClient.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
use Office365\PHP\Client\Runtime\Auth\IAuthenticationContext;
77
use Office365\PHP\Client\Runtime\ClientAction;
88
use Office365\PHP\Client\Runtime\ClientRuntimeContext;
9-
use Office365\PHP\Client\Runtime\OData\JsonSerializerContext;
9+
use Office365\PHP\Client\Runtime\OData\JsonFormat;
1010
use Office365\PHP\Client\Runtime\OData\ODataMetadataLevel;
1111
use Office365\PHP\Client\Runtime\Office365Version;
1212
use Office365\PHP\Client\Runtime\ResourcePathEntity;
@@ -17,7 +17,7 @@ class GraphServiceClient extends ClientRuntimeContext
1717
public function __construct(IAuthenticationContext $authContext)
1818
{
1919
$serviceRootUrl = "https://graph.microsoft.com/" . Office365Version::V1 . "/";
20-
parent::__construct($serviceRootUrl, $authContext,new JsonSerializerContext(ODataMetadataLevel::Verbose));
20+
parent::__construct($serviceRootUrl, $authContext,new JsonFormat(ODataMetadataLevel::Verbose));
2121
}
2222

2323

src/OneDrive/OneDriveClient.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
use Office365\PHP\Client\Runtime\Auth\IAuthenticationContext;
66
use Office365\PHP\Client\Runtime\ClientAction;
77
use Office365\PHP\Client\Runtime\ClientRuntimeContext;
8-
use Office365\PHP\Client\Runtime\OData\JsonSerializerContext;
8+
use Office365\PHP\Client\Runtime\OData\JsonFormat;
99
use Office365\PHP\Client\Runtime\OData\ODataMetadataLevel;
1010
use Office365\PHP\Client\Runtime\Office365Version;
1111
use Office365\PHP\Client\Runtime\ResourcePathEntity;
@@ -17,7 +17,7 @@ class OneDriveClient extends ClientRuntimeContext
1717
public function __construct($authorityUrl,IAuthenticationContext $authContext)
1818
{
1919
$serviceRootUrl = $authorityUrl . "/_api/" . Office365Version::V1 . "/";
20-
parent::__construct($serviceRootUrl, $authContext,new JsonSerializerContext(ODataMetadataLevel::Verbose));
20+
parent::__construct($serviceRootUrl, $authContext,new JsonFormat(ODataMetadataLevel::Verbose));
2121
}
2222

2323

src/OneNote/OneNoteClient.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
use Office365\PHP\Client\Runtime\UpdateEntityQuery;
1010
use Office365\PHP\Client\Runtime\ClientRuntimeContext;
1111
use Office365\PHP\Client\Runtime\HttpMethod;
12-
use Office365\PHP\Client\Runtime\OData\JsonSerializerContext;
12+
use Office365\PHP\Client\Runtime\OData\JsonFormat;
1313
use Office365\PHP\Client\Runtime\OData\ODataMetadataLevel;
1414
use Office365\PHP\Client\Runtime\Office365Version;
1515
use Office365\PHP\Client\Runtime\ResourcePathEntity;
@@ -22,7 +22,7 @@ public function __construct(IAuthenticationContext $authContext, $version = Offi
2222
{
2323
$this->version = $version;
2424
$this->serviceRootUrl = $this->serviceRootUrl . $version . "/";
25-
parent::__construct($this->serviceRootUrl, $authContext, new JsonSerializerContext(ODataMetadataLevel::NoMetadata), $version);
25+
parent::__construct($this->serviceRootUrl, $authContext, new JsonFormat(ODataMetadataLevel::NoMetadata), $version);
2626
}
2727

2828

@@ -95,4 +95,4 @@ public function getMyOrganization(){
9595
*/
9696
public $version;
9797

98-
}
98+
}

src/OutlookServices/OutlookClient.php

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
use Office365\PHP\Client\Runtime\HttpMethod;
1212
use Office365\PHP\Client\Runtime\Office365Version;
1313
use Office365\PHP\Client\Runtime\ResourcePathEntity;
14-
use Office365\PHP\Client\Runtime\OData\JsonSerializerContext;
14+
use Office365\PHP\Client\Runtime\OData\JsonFormat;
1515
use Office365\PHP\Client\Runtime\OData\ODataMetadataLevel;
1616
use Office365\PHP\Client\Runtime\Utilities\RequestOptions;
1717

@@ -25,7 +25,7 @@ public function __construct(IAuthenticationContext $authContext, $version = Offi
2525
{
2626
$this->version = $version;
2727
$this->serviceRootUrl = $this->serviceRootUrl . $version . "/";
28-
parent::__construct($this->serviceRootUrl, $authContext, new JsonSerializerContext(ODataMetadataLevel::Verbose), $version);
28+
parent::__construct($this->serviceRootUrl, $authContext, new JsonFormat(ODataMetadataLevel::Verbose), $version);
2929
}
3030

3131

@@ -42,9 +42,6 @@ public function executeQuery()
4242
}
4343

4444

45-
46-
47-
4845
private function prepareOutlookServicesRequest(RequestOptions $request,ClientAction $query)
4946
{
5047
//set data modification headers

src/OutlookServices/OutlookEntity.php

Lines changed: 16 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66

77
use Office365\PHP\Client\Runtime\DeleteEntityQuery;
8+
use Office365\PHP\Client\Runtime\OData\ODataFormat;
89
use Office365\PHP\Client\Runtime\UpdateEntityQuery;
910
use Office365\PHP\Client\Runtime\ClientObject;
1011
use ReflectionObject;
@@ -33,41 +34,32 @@ public function deleteObject()
3334
}
3435

3536

36-
public function addAnnotation($name, $value)
37+
protected function ensureTypeAnnotation(&$json)
3738
{
38-
$this->annotations["@odata.$name"] = $value;
39-
}
40-
41-
public function ensureTypeAnnotation()
42-
{
43-
$typeName = $this->getTypeName();
44-
$this->addAnnotation("type","#Microsoft.OutlookServices.$typeName");
39+
if($this->IncludeTypeAnnotation){
40+
$typeName = $this->getTypeName();
41+
$json["@odata.type"] = "#Microsoft.OutlookServices.$typeName";
42+
}
4543
}
4644

4745

4846

49-
50-
51-
function getProperties($flag=SCHEMA_ALL_PROPERTIES)
47+
function toJson(ODataFormat $format)
5248
{
53-
$result = parent::getProperties($flag);
54-
//include annotations
55-
foreach ($this->annotations as $key => $val) {
56-
$result[$key] = $val;
57-
}
58-
49+
$json = array();
50+
$this->ensureTypeAnnotation($json);
5951
$reflection = new ReflectionObject($this);
6052
foreach ($reflection->getProperties(ReflectionProperty::IS_PUBLIC) as $p) {
6153
$val = $p->getValue($this);
62-
if (!is_null($val)) {
63-
$result[$p->name] = $val;
54+
if (!is_null($val) && $p->getName() !== "IncludeTypeAnnotation") {
55+
$json[$p->name] = $val;
6456
}
6557
}
66-
return $result;
58+
return $json;
6759
}
6860

6961

70-
function setProperty($name, $value, $persistChanges = true)
62+
function setProperty($name, $value, $serializable = true)
7163
{
7264
$normalizedName = ucfirst($name);
7365
if($normalizedName == "Id"){
@@ -76,7 +68,7 @@ function setProperty($name, $value, $persistChanges = true)
7668
$this->{$normalizedName} = $value;
7769
}
7870
else
79-
parent::setProperty($normalizedName, $value, $persistChanges);
71+
parent::setProperty($normalizedName, $value, $serializable);
8072
}
8173

8274

@@ -85,10 +77,5 @@ function setProperty($name, $value, $persistChanges = true)
8577
*/
8678
public $Id;
8779

88-
89-
/**
90-
* @var array
91-
*/
92-
protected $annotations = array();
93-
94-
}
80+
public $IncludeTypeAnnotation;
81+
}

src/Runtime/CSOM/CSOMRequest.php

Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,24 +2,23 @@
22

33
namespace Office365\PHP\Client\Runtime\CSOM;
44

5-
use Office365\PHP\Client\Runtime\ClientAction;
5+
66
use Office365\PHP\Client\Runtime\ClientRequest;
77
use Office365\PHP\Client\Runtime\Utilities\RequestOptions;
8-
use SimpleXMLElement;
98

109
class CSOMRequest extends ClientRequest
1110
{
1211

1312
/**
1413
* @var string $AppName
1514
*/
16-
private static $AppName = "phpSPO Client";
15+
//private static $AppName = "phpSPO Client";
1716

1817

1918
/**
2019
* @var string $SchemaVersion
2120
*/
22-
private static $SchemaVersion = "16.0.0.0";
21+
//private static $SchemaVersion = "16.0.0.0";
2322

2423
/**
2524
* Submit client request(s) to Office 365 API OData/SOAP service
@@ -55,7 +54,6 @@ protected function setRequestHeaders(RequestOptions $request)
5554

5655
/**
5756
* @param $response
58-
* @param $resultObject
5957
*/
6058
public function processResponse($response)
6159
{
@@ -64,18 +62,11 @@ public function processResponse($response)
6462

6563
/**
6664
* Build Client Request
67-
* @return RequestOptions
65+
* @return void
6866
*/
6967
protected function buildRequest()
7068
{
7169
// TODO: Implement buildRequest() method.
7270
}
7371

74-
/**
75-
* @return ClientRequest
76-
*/
77-
public function getNextRequest()
78-
{
79-
// TODO: Implement getNextRequest() method.
80-
}
81-
}
72+
}

src/Runtime/ClientObject.php

Lines changed: 20 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@
22

33
namespace Office365\PHP\Client\Runtime;
44

5-
5+
use Office365\PHP\Client\Runtime\OData\JsonLightFormat;
6+
use Office365\PHP\Client\Runtime\OData\ODataFormat;
7+
use Office365\PHP\Client\Runtime\OData\ODataMetadataLevel;
68
use Office365\PHP\Client\Runtime\OData\ODataPathBuilder;
79

810
/**
@@ -28,7 +30,7 @@ class ClientObject implements IEntityType
2830
/**
2931
* @var array
3032
*/
31-
private $properties = array();
33+
private $properties = null;
3234

3335

3436
/**
@@ -42,8 +44,6 @@ class ClientObject implements IEntityType
4244
protected $parentCollection;
4345

4446

45-
46-
4747
/**
4848
* ClientObject constructor.
4949
* @param ClientRuntimeContext $ctx
@@ -53,6 +53,7 @@ public function __construct(ClientRuntimeContext $ctx, ResourcePath $resourcePat
5353
{
5454
$this->context = $ctx;
5555
$this->resourcePath = $resourcePath;
56+
$this->properties = array();
5657
}
5758

5859

@@ -134,21 +135,22 @@ public function getTypeName()
134135
}
135136

136137
/**
137-
* @param int $flag
138+
*
139+
* @param ODataFormat $format
138140
* @return array
139141
*/
140-
function getProperties($flag=SCHEMA_ALL_PROPERTIES)
142+
function toJson(ODataFormat $format)
141143
{
142-
if($flag === SCHEMA_ALL_PROPERTIES)
143-
return $this->properties;
144-
//exclude non serializable properties
145-
$result = array();
144+
$payload = array();
146145
foreach( $this->properties as $key=>$value ) {
147146
$metadata = $this->propertiesMetadata[$key];
148-
if(($metadata !== null && $metadata["Serializable"] == true))
149-
$result[$key] = $value;
147+
if(($metadata !== null && $metadata["Serializable"] === true))
148+
$payload[$key] = $value;
150149
}
151-
return $result;
150+
if ($format instanceof JsonLightFormat && $format->MetadataLevel == ODataMetadataLevel::Verbose) {
151+
$format->ensureMetadataAnnotation($this, $payload);
152+
}
153+
return $payload;
152154
}
153155

154156

@@ -159,7 +161,7 @@ function getProperties($flag=SCHEMA_ALL_PROPERTIES)
159161
*/
160162
public function isPropertyAvailable($name)
161163
{
162-
return isset($this->properties[$name]) && !isset($this->properties[$name]->__deferred);
164+
return isset($this->properties[$name]);
163165
}
164166

165167

@@ -168,9 +170,7 @@ public function isPropertyAvailable($name)
168170
* @return bool
169171
*/
170172
public function getServerObjectIsNull(){
171-
if(!is_null($this->resourcePath))
172-
return $this->resourcePath->ServerObjectIsNull;
173-
return true;
173+
return is_null($this->properties);
174174
}
175175

176176

@@ -188,12 +188,11 @@ public function getProperty($name)
188188
* A preferred way of setting the client object property
189189
* @param string $name
190190
* @param mixed $value
191-
* @param bool $persistChanges
191+
* @param bool $serializable
192192
*/
193-
public function setProperty($name, $value, $persistChanges = true)
193+
public function setProperty($name, $value, $serializable = true)
194194
{
195-
$this->propertiesMetadata[$name] = array("Serializable" => $persistChanges);
196-
195+
$this->propertiesMetadata[$name] = array("Serializable" => $serializable);
197196
//save property
198197
$this->{$name} = $value;
199198

0 commit comments

Comments
 (0)