2010-10-08 14:24:42 +02:00
|
|
|
<?php
|
2013-02-21 08:22:55 +01:00
|
|
|
|
2010-10-08 17:01:27 +02:00
|
|
|
/*
|
2011-07-18 22:43:12 +02:00
|
|
|
* This file is part of the BeSimpleSoapBundle.
|
2010-10-08 17:01:27 +02:00
|
|
|
*
|
|
|
|
* (c) Christian Kerl <christian-kerl@web.de>
|
2013-02-21 08:22:55 +01:00
|
|
|
* (c) Francis Besset <francis.besset@gmail.com>
|
2010-10-08 17:01:27 +02:00
|
|
|
*
|
|
|
|
* This source file is subject to the MIT license that is bundled
|
|
|
|
* with this source code in the file LICENSE.
|
|
|
|
*/
|
2010-10-08 14:24:42 +02:00
|
|
|
|
2011-07-18 22:43:12 +02:00
|
|
|
namespace BeSimple\SoapBundle\ServiceBinding;
|
2010-10-08 14:24:42 +02:00
|
|
|
|
2011-07-18 22:43:12 +02:00
|
|
|
use BeSimple\SoapBundle\ServiceDefinition\Method;
|
2011-07-23 20:06:42 +02:00
|
|
|
use BeSimple\SoapBundle\ServiceDefinition\Strategy\PropertyComplexType;
|
|
|
|
use BeSimple\SoapBundle\ServiceDefinition\Strategy\MethodComplexType;
|
2013-02-21 08:22:55 +01:00
|
|
|
use BeSimple\SoapCommon\Util\MessageBinder;
|
2011-08-24 23:36:49 +02:00
|
|
|
|
2011-07-24 11:31:15 +02:00
|
|
|
/**
|
|
|
|
* @author Christian Kerl <christian-kerl@web.de>
|
|
|
|
* @author Francis Besset <francis.besset@gmail.com>
|
|
|
|
*/
|
2010-10-08 14:24:42 +02:00
|
|
|
class RpcLiteralResponseMessageBinder implements MessageBinderInterface
|
|
|
|
{
|
2011-07-27 23:15:47 +02:00
|
|
|
private $messageRefs = array();
|
2011-08-24 23:36:49 +02:00
|
|
|
private $definitionComplexTypes;
|
2011-07-27 23:15:47 +02:00
|
|
|
|
2011-07-23 20:06:42 +02:00
|
|
|
public function processMessage(Method $messageDefinition, $message, array $definitionComplexTypes = array())
|
2010-10-08 14:24:42 +02:00
|
|
|
{
|
2011-08-24 23:36:49 +02:00
|
|
|
$this->definitionComplexTypes = $definitionComplexTypes;
|
2011-07-28 00:39:19 +02:00
|
|
|
|
2011-08-24 23:36:49 +02:00
|
|
|
return $this->processType($messageDefinition->getReturn()->getPhpType(), $message);
|
2011-07-28 00:39:19 +02:00
|
|
|
}
|
|
|
|
|
2011-08-24 23:36:49 +02:00
|
|
|
private function processType($phpType, $message)
|
2011-07-28 00:39:19 +02:00
|
|
|
{
|
2011-08-24 23:36:49 +02:00
|
|
|
$isArray = false;
|
|
|
|
|
2011-07-28 00:39:19 +02:00
|
|
|
if (preg_match('/^([^\[]+)\[\]$/', $phpType, $match)) {
|
2011-07-23 20:06:42 +02:00
|
|
|
$isArray = true;
|
2011-08-24 23:36:49 +02:00
|
|
|
$phpType = $match[1];
|
2011-07-23 20:06:42 +02:00
|
|
|
}
|
|
|
|
|
2011-08-24 23:36:49 +02:00
|
|
|
if (isset($this->definitionComplexTypes[$phpType])) {
|
2011-07-23 20:06:42 +02:00
|
|
|
if ($isArray) {
|
|
|
|
$array = array();
|
|
|
|
|
2013-02-27 17:06:00 +01:00
|
|
|
// See https://github.com/BeSimple/BeSimpleSoapBundle/issues/29
|
|
|
|
if (is_array($message) && in_array('BeSimple\SoapCommon\Type\AbstractKeyValue', class_parents($phpType))) {
|
|
|
|
$keyValue = array();
|
|
|
|
foreach ($message as $key => $value) {
|
|
|
|
$keyValue[] = new $phpType($key, $value);
|
|
|
|
}
|
|
|
|
|
|
|
|
$message = $keyValue;
|
|
|
|
}
|
|
|
|
|
2011-07-23 20:06:42 +02:00
|
|
|
foreach ($message as $complexType) {
|
2011-08-24 23:36:49 +02:00
|
|
|
$array[] = $this->checkComplexType($phpType, $complexType);
|
2011-07-23 20:06:42 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
$message = $array;
|
|
|
|
} else {
|
2011-08-24 23:36:49 +02:00
|
|
|
$message = $this->checkComplexType($phpType, $message);
|
2011-07-23 20:06:42 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-10-08 14:24:42 +02:00
|
|
|
return $message;
|
|
|
|
}
|
2011-07-23 20:06:42 +02:00
|
|
|
|
2011-08-24 23:36:49 +02:00
|
|
|
private function checkComplexType($phpType, $message)
|
2011-07-23 20:06:42 +02:00
|
|
|
{
|
2011-07-27 23:15:47 +02:00
|
|
|
$hash = spl_object_hash($message);
|
|
|
|
if (isset($this->messageRefs[$hash])) {
|
|
|
|
return $this->messageRefs[$hash];
|
|
|
|
}
|
|
|
|
|
2011-09-08 20:44:06 +02:00
|
|
|
$this->messageRefs[$hash] = $message;
|
|
|
|
|
2011-09-14 22:03:46 +02:00
|
|
|
if (!$message instanceof $phpType) {
|
|
|
|
throw new \InvalidArgumentException(sprintf('The instance class must be "%s", "%s" given.', get_class($message), $phpType));
|
2011-07-28 00:39:19 +02:00
|
|
|
}
|
|
|
|
|
2013-02-21 08:22:55 +01:00
|
|
|
$messageBinder = new MessageBinder($message);
|
2011-09-14 22:03:46 +02:00
|
|
|
foreach ($this->definitionComplexTypes[$phpType] as $type) {
|
2013-02-21 08:22:55 +01:00
|
|
|
$property = $type->getName();
|
|
|
|
$value = $messageBinder->readProperty($property);
|
2012-01-12 11:45:15 +01:00
|
|
|
|
2012-01-12 11:53:58 +01:00
|
|
|
if (null !== $value) {
|
|
|
|
$value = $this->processType($type->getValue(), $value);
|
2011-07-28 00:39:19 +02:00
|
|
|
|
2013-02-21 08:22:55 +01:00
|
|
|
$messageBinder->writeProperty($property, $value);
|
2011-09-08 20:42:09 +02:00
|
|
|
}
|
2011-08-24 23:36:49 +02:00
|
|
|
|
|
|
|
if (!$type->isNillable() && null === $value) {
|
2011-09-14 22:03:46 +02:00
|
|
|
throw new \InvalidArgumentException(sprintf('"%s::%s" cannot be null.', $phpType, $type->getName()));
|
2011-08-24 23:36:49 +02:00
|
|
|
}
|
2011-07-23 20:06:42 +02:00
|
|
|
}
|
|
|
|
|
2011-09-08 20:44:06 +02:00
|
|
|
return $message;
|
2011-07-23 20:06:42 +02:00
|
|
|
}
|
2012-01-12 11:45:15 +01:00
|
|
|
}
|