Skip to content

Commit

Permalink
Optimize odata request payload serializing, fixes for saving multi-ch…
Browse files Browse the repository at this point in the history
…oice field value (#261)
  • Loading branch information
vgrem committed Feb 5, 2022
1 parent 04a2b2c commit 62fb425
Show file tree
Hide file tree
Showing 27 changed files with 290 additions and 95 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Office365\Runtime\Auth\ClientCredential;
use Office365\SharePoint\ClientContext;
use Office365\SharePoint\FieldLookupValue;
use Office365\SharePoint\FieldMultiChoiceValue;
use Office365\SharePoint\FieldMultiLookupValue;
use Office365\SharePoint\FieldUserValue;
use Office365\SharePoint\ListItem;
Expand All @@ -28,6 +29,7 @@
'Title' => "New task N#" . rand(1, 100000),
'ParentTask' => new FieldLookupValue($taskId),
'PrimaryManager' => new FieldUserValue($me->getId()),
'Managers' => new FieldMultiLookupValue([$me->getId()])
'Managers' => new FieldMultiLookupValue([$me->getId()]),
'TaskCategories' => new FieldMultiChoiceValue(["Event", "Reminder"])
);
$item = $list->addItem($taskProps)->executeQuery();
10 changes: 2 additions & 8 deletions src/Outlook/OutlookItem.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,7 @@ class OutlookItem extends Entity
*/
public function getChangeKey()
{
if (!$this->isPropertyAvailable("ChangeKey")) {
return null;
}
return $this->getProperty("ChangeKey");
return $this->getProperty("ChangeKey", null);
}
/**
* @var string
Expand All @@ -31,10 +28,7 @@ public function setChangeKey($value)
*/
public function getCategories()
{
if (!$this->isPropertyAvailable("Categories")) {
return null;
}
return $this->getProperty("Categories");
return $this->getProperty("Categories", null);
}
/**
* @var array
Expand Down
14 changes: 3 additions & 11 deletions src/Runtime/ClientObject.php
Original file line number Diff line number Diff line change
Expand Up @@ -127,14 +127,6 @@ public function getParentCollection()
return $this->parentCollection;
}

/**
* @return null
*/
protected function getServerTypeId()
{
return null;
}


/**
* @return ClientRuntimeContext
Expand Down Expand Up @@ -211,11 +203,11 @@ public function select($value)

/**
* Gets entity type name
* @return string
* @return ServerTypeInfo
*/
public function getServerTypeName()
public function getServerTypeInfo()
{
return null;
return ServerTypeInfo::resolve($this);
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/Runtime/ClientResult.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public function executeQuery(){


/**
* @return bool|int|ClientObject|ClientValue|string|null
* @return bool|int|string|ClientObject|ClientValue|null
*/
public function getValue(){
return $this->value;
Expand Down
6 changes: 3 additions & 3 deletions src/Runtime/ClientValue.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,11 @@ public function getProperty($name, $defaultValue=null)


/**
* @return string
* @return ServerTypeInfo
*/
public function getServerTypeName()
public function getServerTypeInfo()
{
return null;
return ServerTypeInfo::resolve($this);
}

/**
Expand Down
13 changes: 3 additions & 10 deletions src/Runtime/ClientValueCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public static function fromArray($itemTypeName,$values)

/**
* Adds property to collection
* @param ClientValue $value
* @param ClientValue|string $value
*/
public function addChild($value)
{
Expand Down Expand Up @@ -81,19 +81,12 @@ public function createType()
*/
function getItemTypeName()
{
if(isset($this->itemTypeName))
if(isset($this->itemTypeName)) {
return $this->itemTypeName;
}
return str_replace("Collection","",get_class($this));
}

/**
* @return string|null
*/
public function getServerTypeName()
{
return null;
}

/**
* @return array
*/
Expand Down
33 changes: 6 additions & 27 deletions src/Runtime/OData/ODataRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,30 +75,8 @@ public function buildRequest(){
*/
public function normalizeTypeName($type)
{
$collection = false;
$typeName = $type->getServerTypeName();
if (is_null($typeName)) {

if ($type instanceof ClientValueCollection) {
$collection = true;
$typeInfo = explode("\\", $type->getItemTypeName());
}
else{
$typeInfo = explode("\\", get_class($type));
}
$typeName = end($typeInfo);

//if ($this->context instanceof OutlookClient)
// $typeName = "#Microsoft.OutlookServices.$typeName";
if ($this->context instanceof ClientContext)
$typeName = "SP.$typeName";
else if ($this->context instanceof GraphServiceClient) {
$typeName = lcfirst($typeName);
$typeName = "microsoft.graph.$typeName";
}
return $collection ? "Collection($typeName)" : $typeName;
}
return $typeName;
$typeInfo = $type->getServerTypeInfo()->patch($this->context);
return (string)$typeInfo;
}


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

if(!($value instanceof ClientValueCollection || $value instanceof ClientObjectCollection))
$this->ensureAnnotation($value,$json,$format);

$this->ensureAnnotation($value,$json,$format);
return $json;
} else if (is_array($value)) {
return array_map(function ($item) use($format){
Expand All @@ -147,7 +125,8 @@ protected function ensureAnnotation($type, &$json,$format)
}
}
elseif ($format instanceof JsonFormat){
$json[$format->TypeTag] = "$typeName";
if(!($type instanceof ClientValueCollection))
$json[$format->TypeTag] = "$typeName";
}
}

Expand Down
121 changes: 121 additions & 0 deletions src/Runtime/ServerTypeInfo.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
<?php

namespace Office365\Runtime;

use Exception;
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 = implode(".", array_slice($parts, 1, count($parts) - 2));
return new ServerTypeInfo(null, $typeName, $collection);
}

/**
* @param ClientRuntimeContext $context
*/
public function patch($context){
if ($context instanceof ClientContext) {
if(is_null($this->Namespace)) $this->Namespace = "SP";
}
else if ($context instanceof GraphServiceClient) {
if(is_null($this->Namespace)) $this->Namespace = "microsoft.graph";
$this->Name = lcfirst($this->Name);
}
return $this;
}

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"
);

}
8 changes: 6 additions & 2 deletions src/SharePoint/ContentTypeCreationInformation.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

namespace Office365\SharePoint;
use Office365\Runtime\ClientValue;
use Office365\Runtime\ServerTypeInfo;

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


public function getServerTypeName()
/**
* @return ServerTypeInfo
*/
public function getServerTypeInfo()
{
return "SP.ContentType";
return new ServerTypeInfo("SP", "ContentType");
}


Expand Down
9 changes: 7 additions & 2 deletions src/SharePoint/FieldCreationInformation.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
namespace Office365\SharePoint;

use Office365\Runtime\ClientValue;
use Office365\Runtime\ServerTypeInfo;

/**
* Represents properties that can be set when creating a field.
*/
Expand All @@ -16,9 +18,12 @@ public function __construct()
parent::__construct();
}

public function getServerTypeName()
/**
* @return ServerTypeInfo
*/
public function getServerTypeInfo()
{
return "SP.Field";
return new ServerTypeInfo("SP", "Field");
}

/**
Expand Down
32 changes: 32 additions & 0 deletions src/SharePoint/FieldMultiChoiceValue.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

namespace Office365\SharePoint;

use Office365\Runtime\ClientValueCollection;
use Office365\Runtime\ServerTypeInfo;

class FieldMultiChoiceValue extends ClientValueCollection
{

/**
* @param string[] $choices
*/
public function __construct($choices)
{
parent::__construct("string");
foreach ($choices as $choice) {
$this->addChild($choice);
}
}

public function toJson()
{
return array('results' => $this->getData());
}

public function getServerTypeInfo()
{
return ServerTypeInfo::primitive("string",true);
}

}
Loading

0 comments on commit 62fb425

Please sign in to comment.