2010-10-08 14:24:42 +02:00
|
|
|
<?php
|
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>
|
|
|
|
*
|
|
|
|
* 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;
|
2010-10-08 14:24:42 +02:00
|
|
|
|
2011-08-24 23:36:49 +02:00
|
|
|
use Zend\Soap\Wsdl;
|
|
|
|
|
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();
|
|
|
|
|
|
|
|
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-07-28 00:39:19 +02:00
|
|
|
$class = $phpType;
|
|
|
|
if ($class[0] == '\\') {
|
|
|
|
$class = substr($class, 1);
|
|
|
|
}
|
|
|
|
|
2011-08-24 23:36:49 +02:00
|
|
|
if (!$message instanceof $class) {
|
|
|
|
throw new \InvalidArgumentException(sprintf('The instance class must be "%s", "%s" given.', get_class($message), $class));
|
2011-07-28 00:39:19 +02:00
|
|
|
}
|
|
|
|
|
2011-08-24 23:36:49 +02:00
|
|
|
$r = new \ReflectionClass($message);
|
|
|
|
foreach ($this->definitionComplexTypes[$class] as $type) {
|
|
|
|
$p = $r->getProperty($type->getName());
|
|
|
|
if ($p->isPublic()) {
|
|
|
|
$value = $message->{$type->getName()};
|
2011-07-23 20:06:42 +02:00
|
|
|
} else {
|
2011-08-24 23:36:49 +02:00
|
|
|
$p->setAccessible(true);
|
|
|
|
$value = $p->getValue($message);
|
2011-07-23 20:06:42 +02:00
|
|
|
}
|
2011-07-28 00:39:19 +02:00
|
|
|
|
2011-08-24 23:36:49 +02:00
|
|
|
$value = $this->processType($type->getValue(), $value);
|
|
|
|
|
|
|
|
if (!$type->isNillable() && null === $value) {
|
|
|
|
throw new \InvalidArgumentException(sprintf('"%s::%s" cannot be null.', $class, $type->getName()));
|
|
|
|
}
|
2011-07-23 20:06:42 +02:00
|
|
|
}
|
|
|
|
|
2011-08-24 23:36:49 +02:00
|
|
|
return $this->messageRefs[$hash] = $message;
|
2011-07-23 20:06:42 +02:00
|
|
|
}
|
2010-10-08 14:24:42 +02:00
|
|
|
}
|