Skip to content

Commit

Permalink
Normalize request payload changes
Browse files Browse the repository at this point in the history
  • Loading branch information
vgrem committed Aug 4, 2018
1 parent 35710e4 commit c1548be
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 66 deletions.
10 changes: 5 additions & 5 deletions src/Runtime/InvokePostMethodQuery.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,20 +12,20 @@ class InvokePostMethodQuery extends InvokeMethodQuery
* @param ResourcePath $resourcePath
* @param string $methodName
* @param array $methodParameters
* @param string|IEntityType $methodPayload
* @param string|IEntityType $methodBody
*/
public function __construct(ResourcePath $resourcePath, $methodName = null,$methodParameters=null,$methodPayload=null)
public function __construct(ResourcePath $resourcePath, $methodName = null, $methodParameters=null, $methodBody=null)
{
$this->MethodPayload = $methodPayload;
$this->MethodBody = $methodBody;
parent::__construct($resourcePath,$methodName, $methodParameters);
}



/**
* @var string|IEntityType $MethodPayload
* @var string|IEntityType $MethodBody
*/
public $MethodPayload;
public $MethodBody;


}
45 changes: 45 additions & 0 deletions src/Runtime/OData/JsonLightSerializerContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@



use Office365\PHP\Client\Runtime\IEntityType;
use Office365\PHP\Client\SharePoint\CamlQuery;
use Office365\PHP\Client\SharePoint\ChangeLogItemQuery;
use Office365\PHP\Client\SharePoint\ChangeQuery;
use Office365\PHP\Client\SharePoint\WebCreationInformation;

class JsonLightSerializerContext extends ODataSerializerContext
{

Expand Down Expand Up @@ -74,6 +80,45 @@ public function getMediaType()
}



public function normalize($value)
{
$payload = parent::normalize($value);
if ($value instanceof IEntityType) {
$this->ensureMetadata($value, $payload); //ensure metadata
$this->ensureContainer($value, $payload); //ensure parent container
}
return $payload;
}


/**
* @param IEntityType $value
* @param $payload
*/
private function ensureContainer(IEntityType $value, &$payload)
{
if ($value instanceof CamlQuery || $value instanceof ChangeQuery || $value instanceof ChangeLogItemQuery)
$payload = array("query" => $payload);
else if ($value instanceof WebCreationInformation)
$payload = array("parameters" => $payload);
}

/**
* @param IEntityType $value
* @param $payload
*/
private function ensureMetadata(IEntityType $value, &$payload)
{
if ($this->MetadataLevel == ODataMetadataLevel::Verbose) {
$metadataTypeName = $value->getTypeName();
if (substr($metadataTypeName, 0, 3) !== "SP.")
$metadataTypeName = "SP." . $metadataTypeName;
$payload["__metadata"] = array("type" => $metadataTypeName);
}
}


/**
* @param $value
* @return bool
Expand Down
71 changes: 10 additions & 61 deletions src/Runtime/OData/ODataRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@


use Exception;
use Office365\PHP\Client\Runtime\ClientAction;
use Office365\PHP\Client\Runtime\ClientResult;
use Office365\PHP\Client\Runtime\IEntityType;
use Office365\PHP\Client\Runtime\InvokeMethodQuery;
Expand All @@ -13,10 +14,7 @@
use Office365\PHP\Client\Runtime\ClientRuntimeContext;
use Office365\PHP\Client\Runtime\HttpMethod;
use Office365\PHP\Client\Runtime\Utilities\RequestOptions;
use Office365\PHP\Client\SharePoint\CamlQuery;
use Office365\PHP\Client\SharePoint\ChangeLogItemQuery;
use Office365\PHP\Client\SharePoint\ChangeQuery;
use Office365\PHP\Client\SharePoint\WebCreationInformation;


/**
Expand Down Expand Up @@ -64,7 +62,7 @@ public function processResponse($response)
}

$resultObject = $this->resultObjects[$this->getCurrentAction()->getId()];
if ($this->getCurrentAction() instanceof InvokePostMethodQuery && $this->getCurrentAction()->MethodPayload instanceof ChangeLogItemQuery) {
if ($this->getCurrentAction() instanceof InvokePostMethodQuery && $this->getCurrentAction()->MethodBody instanceof ChangeLogItemQuery) {
$payload = $this->parseXmlResponse($response);
} else {
$payload = $this->parseJsonResponse($response);
Expand Down Expand Up @@ -166,71 +164,18 @@ public function buildRequest()
$request = new RequestOptions($resourceUrl);
if ($this->getCurrentAction() instanceof InvokePostMethodQuery) {
$request->Method = HttpMethod::Post;
if (is_string($this->getCurrentAction()->MethodPayload))
$request->Data = $this->getCurrentAction()->MethodPayload;
if (is_array($this->getCurrentAction()->MethodPayload))
$request->Data = json_encode($this->getCurrentAction()->MethodPayload);
else if ($this->getCurrentAction()->MethodPayload instanceof IEntityType) {
if (is_string($this->getCurrentAction()->MethodBody))
$request->Data = $this->getCurrentAction()->MethodBody;
else if ($this->getCurrentAction()->MethodBody instanceof IEntityType) {
//build request payload
$payload = $this->normalizePayload($this->getCurrentAction()->MethodPayload);
$payload = $this->getSerializationContext()->normalize($this->getCurrentAction()->MethodBody);
$request->Data = json_encode($payload);
}
}
return $request;
}


/**
* Normalize request payload
* @param IEntityType|array $value
* @return array
*/
protected function normalizePayload($value)
{
if ($value instanceof IEntityType) {
$payload = array_map(function ($property) {
return $this->normalizePayload($property);
}, $value->getProperties(SCHEMA_SERIALIZABLE_PROPERTIES));

$this->ensureMetadata($value, $payload); //ensure metadata
$this->ensureContainer($value, $payload); //ensure parent container
return $payload;
} else if (is_array($value)) {
return array_map(function ($item) {
return $this->normalizePayload($item);
}, $value);
}
return $value;
}


/**
* @param IEntityType $value
* @param $payload
*/
private function ensureContainer(IEntityType $value, &$payload)
{
if ($value instanceof CamlQuery || $value instanceof ChangeQuery || $value instanceof ChangeLogItemQuery)
$payload = array("query" => $payload);
else if ($value instanceof WebCreationInformation)
$payload = array("parameters" => $payload);
}

/**
* @param IEntityType $value
* @param $payload
*/
private function ensureMetadata(IEntityType $value, &$payload)
{
if ($this->getSerializationContext() instanceof JsonLightSerializerContext && $this->getSerializationContext()->MetadataLevel == ODataMetadataLevel::Verbose) {
$metadataTypeName = $value->getTypeName();
if (substr($metadataTypeName, 0, 3) !== "SP.")
$metadataTypeName = "SP." . $metadataTypeName;
$payload["__metadata"] = array("type" => $metadataTypeName);
}
}


/**
* @return ODataSerializerContext
*/
Expand All @@ -239,6 +184,10 @@ protected function getSerializationContext()
return $this->context->getSerializerContext();
}


/**
* @return ClientAction|InvokePostMethodQuery
*/
protected function getCurrentAction(){
return current($this->getActions());
}
Expand Down
21 changes: 21 additions & 0 deletions src/Runtime/OData/ODataSerializerContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,27 @@ protected function mapTypeCollection($json, IEntityTypeCollection $resultTypeCol
}


/**
* Normalize request payload
* @param IEntityType|array $value
* @return array
*/
public function normalize($value)
{
if ($value instanceof IEntityType) {
$payload = array_map(function ($property) {
return $this->normalize($property);
}, $value->getProperties(SCHEMA_SERIALIZABLE_PROPERTIES));
return $payload;
} else if (is_array($value)) {
return array_map(function ($item) {
return $this->normalize($item);
}, $value);
}
return $value;
}



/**
* @return string
Expand Down

0 comments on commit c1548be

Please sign in to comment.