BeSimpleSoap/ServiceBinding/RpcLiteralResponseMessageBi...

99 lines
3.0 KiB
PHP
Raw Normal View History

<?php
2010-10-08 17:01:27 +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.
*/
namespace BeSimple\SoapBundle\ServiceBinding;
use BeSimple\SoapBundle\ServiceDefinition\Method;
use BeSimple\SoapBundle\ServiceDefinition\Strategy\PropertyComplexType;
use BeSimple\SoapBundle\ServiceDefinition\Strategy\MethodComplexType;
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>
*/
class RpcLiteralResponseMessageBinder implements MessageBinderInterface
{
private $messageRefs = array();
private $definitionComplexTypes;
public function processMessage(Method $messageDefinition, $message, array $definitionComplexTypes = array())
{
$this->definitionComplexTypes = $definitionComplexTypes;
return $this->processType($messageDefinition->getReturn()->getPhpType(), $message);
}
private function processType($phpType, $message)
{
$isArray = false;
if (preg_match('/^([^\[]+)\[\]$/', $phpType, $match)) {
$isArray = true;
$phpType = $match[1];
}
if (isset($this->definitionComplexTypes[$phpType])) {
if ($isArray) {
$array = array();
foreach ($message as $complexType) {
$array[] = $this->checkComplexType($phpType, $complexType);
}
$message = $array;
} else {
$message = $this->checkComplexType($phpType, $message);
}
}
return $message;
}
private function checkComplexType($phpType, $message)
{
$hash = spl_object_hash($message);
if (isset($this->messageRefs[$hash])) {
return $this->messageRefs[$hash];
}
$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));
}
$r = new \ReflectionClass($message);
2011-09-14 22:03:46 +02:00
foreach ($this->definitionComplexTypes[$phpType] as $type) {
$p = $r->getProperty($type->getName());
if ($p->isPublic()) {
$value = $message->{$type->getName()};
2011-09-08 20:42:09 +02:00
$message->{$type->getName()} = $this->processType($type->getValue(), $value);
} else {
$p->setAccessible(true);
$value = $p->getValue($message);
if ($value !== null) {
$value = $this->processType($type->getValue(), $value);
}
$p->setValue($message, $value);
2011-09-08 20:42:09 +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()));
}
}
return $message;
}
}