BeSimpleSoap/ServiceBinding/RpcLiteralRequestMessageBinder.php

119 lines
3.7 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>
* (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.
*/
namespace BeSimple\SoapBundle\ServiceBinding;
use BeSimple\SoapBundle\ServiceDefinition\Method;
use BeSimple\SoapBundle\ServiceDefinition\Strategy\MethodComplexType;
use BeSimple\SoapBundle\ServiceDefinition\Strategy\PropertyComplexType;
use BeSimple\SoapCommon\Util\MessageBinder;
2011-07-24 11:31:15 +02:00
/**
* @author Christian Kerl <christian-kerl@web.de>
* @author Francis Besset <francis.besset@gmail.com>
*/
class RpcLiteralRequestMessageBinder implements MessageBinderInterface
{
private $messageRefs = array();
private $definitionComplexTypes;
2011-07-24 10:56:07 +02:00
public function processMessage(Method $messageDefinition, $message, array $definitionComplexTypes = array())
{
$this->definitionComplexTypes = $definitionComplexTypes;
$result = array();
$i = 0;
foreach ($messageDefinition->getArguments() as $argument) {
if (isset($message[$i])) {
$result[$argument->getName()] = $this->processType($argument->getType()->getPhpType(), $message[$i]);
2011-07-24 10:56:07 +02:00
}
$i++;
}
return $result;
}
protected function processType($phpType, $message)
2011-07-24 10:56:07 +02:00
{
$isArray = false;
2011-07-24 10:56:07 +02:00
if (preg_match('/^([^\[]+)\[\]$/', $phpType, $match)) {
$isArray = true;
$array = array();
$phpType = $match[1];
2011-07-24 10:56:07 +02:00
}
// @TODO Fix array reference
if (isset($this->definitionComplexTypes[$phpType])) {
2011-07-24 10:56:07 +02:00
if ($isArray) {
if (isset($message->item)) {
foreach ($message->item as $complexType) {
$array[] = $this->checkComplexType($phpType, $complexType);
}
2013-02-27 17:06:00 +01:00
// See https://github.com/BeSimple/BeSimpleSoapBundle/issues/29
if (in_array('BeSimple\SoapCommon\Type\AbstractKeyValue', class_parents($phpType))) {
$assocArray = array();
foreach ($array as $keyValue) {
$assocArray[$keyValue->getKey()] = $keyValue->getValue();
}
$array = $assocArray;
}
}
2011-07-24 10:56:07 +02:00
$message = $array;
} else {
$message = $this->checkComplexType($phpType, $message);
}
2011-07-24 10:56:07 +02:00
} elseif ($isArray) {
if (isset($message->item)) {
$message = $message->item;
} else {
$message = $array;
}
}
2011-07-24 10:56:07 +02:00
return $message;
}
protected function checkComplexType($phpType, $message)
{
$hash = spl_object_hash($message);
if (isset($this->messageRefs[$hash])) {
return $this->messageRefs[$hash];
}
$this->messageRefs[$hash] = $message;
$messageBinder = new MessageBinder($message);
foreach ($this->definitionComplexTypes[$phpType] as $type) {
$property = $type->getName();
$value = $messageBinder->readProperty($property);
if (null !== $value) {
$value = $this->processType($type->getValue(), $value);
$messageBinder->writeProperty($property, $value);
}
if (!$type->isNillable() && null === $value) {
throw new \SoapFault('SOAP_ERROR_COMPLEX_TYPE', sprintf('"%s:%s" cannot be null.', ucfirst($phpType), $type->getName()));
}
}
return $message;
}
}