diff --git a/src/Runtime/InvokePostMethodQuery.php b/src/Runtime/InvokePostMethodQuery.php index e20879c9..1ae26f4f 100644 --- a/src/Runtime/InvokePostMethodQuery.php +++ b/src/Runtime/InvokePostMethodQuery.php @@ -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; } \ No newline at end of file diff --git a/src/Runtime/OData/JsonLightSerializerContext.php b/src/Runtime/OData/JsonLightSerializerContext.php index 07e161a7..8fdfa877 100644 --- a/src/Runtime/OData/JsonLightSerializerContext.php +++ b/src/Runtime/OData/JsonLightSerializerContext.php @@ -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 { @@ -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 diff --git a/src/Runtime/OData/ODataRequest.php b/src/Runtime/OData/ODataRequest.php index f5dec007..d6f5bc60 100644 --- a/src/Runtime/OData/ODataRequest.php +++ b/src/Runtime/OData/ODataRequest.php @@ -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; @@ -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; /** @@ -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); @@ -166,13 +164,11 @@ 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); } } @@ -180,57 +176,6 @@ public function buildRequest() } - /** - * 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 */ @@ -239,6 +184,10 @@ protected function getSerializationContext() return $this->context->getSerializerContext(); } + + /** + * @return ClientAction|InvokePostMethodQuery + */ protected function getCurrentAction(){ return current($this->getActions()); } diff --git a/src/Runtime/OData/ODataSerializerContext.php b/src/Runtime/OData/ODataSerializerContext.php index bb753550..04346d68 100644 --- a/src/Runtime/OData/ODataSerializerContext.php +++ b/src/Runtime/OData/ODataSerializerContext.php @@ -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