BeSimpleSoap/ServiceBinding/RpcLiteralRequestMessageBinder.php

92 lines
2.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>
*
* 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;
class RpcLiteralRequestMessageBinder implements MessageBinderInterface
{
2011-07-24 10:56:07 +02:00
private $definitionComplexTypes;
public function processMessage(Method $messageDefinition, $message, array $definitionComplexTypes = array())
{
2011-07-24 10:56:07 +02:00
$this->definitionComplexTypes = $definitionComplexTypes;
$result = array();
$i = 0;
2011-07-14 17:45:03 +02:00
foreach($messageDefinition->getArguments() as $argument) {
if (isset($message[$i])) {
2011-07-24 10:56:07 +02:00
$result[$argument->getName()] = $this->processType($argument->getType()->getPhpType(), $message[$i]);
}
$i++;
}
$this->definitionComplexTypes = array();
return $result;
}
2011-07-24 10:56:07 +02:00
private function processType($phpType, $message)
{
if (preg_match('/^([^\[]+)\[\]$/', $phpType, $match)) {
$isArray = true;
$type = $match[1];
} else {
$isArray = false;
$type = $phpType;
}
2011-07-24 10:56:07 +02:00
if (isset($this->definitionComplexTypes[$type])) {
if ($isArray) {
$array = array();
2011-07-24 10:56:07 +02:00
foreach ($message->item as $complexType) {
$array[] = $this->getInstanceOfType($type, $complexType);
}
2011-07-24 10:56:07 +02:00
$message = $array;
} else {
$message = $this->getInstanceOfType($type, $message);
}
2011-07-24 10:56:07 +02:00
} elseif ($isArray) {
$message = $message->item;
}
2011-07-24 10:56:07 +02:00
return $message;
}
2011-07-24 10:56:07 +02:00
private function getInstanceOfType($phpType, $message)
{
2011-07-24 10:56:07 +02:00
$instanceType = new $phpType();
2011-07-24 10:56:07 +02:00
foreach ($this->definitionComplexTypes[$phpType] as $type) {
$value = $this->processType($type->getValue(), $message->{$type->getName()});
2011-07-24 10:56:07 +02:00
if ($type instanceof PropertyComplexType) {
$instanceType->{$type->getOriginalName()} = $value;
} elseif ($type instanceof MethodComplexType) {
if (!$type->getSetter()) {
throw new \LogicException();
}
$instanceType->{$type->getSetter()}($value);
} else {
throw new \InvalidArgumentException();
}
}
return $instanceType;
}
}