1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31: 32: 33: 34: 35: 36: 37: 38: 39: 40: 41: 42: 43: 44: 45: 46: 47: 48: 49: 50: 51: 52: 53: 54: 55: 56: 57: 58: 59: 60: 61: 62: 63: 64: 65: 66: 67: 68: 69: 70: 71: 72: 73: 74: 75: 76: 77: 78: 79: 80: 81: 82: 83: 84: 85: 86: 87: 88: 89: 90: 91: 92: 93: 94: 95: 96: 97: 98: 99: 100: 101: 102: 103: 104: 105: 106: 107: 108: 109: 110: 111: 112: 113: 114: 115: 116: 117: 118: 119: 120: 121: 122: 123: 124: 125: 126: 127: 128: 129: 130: 131: 132: 133: 134: 135: 136: 137: 138: 139: 140: 141: 142: 143: 144: 145: 146: 147: 148: 149: 150: 151: 152: 153: 154: 155: 156: 157: 158: 159: 160: 161: 162: 163: 164: 165: 166: 167: 168: 169: 170: 171: 172: 173: 174: 175: 176: 177: 178: 179: 180: 181: 182: 183: 184: 185: 186: 187: 188: 189: 190: 191: 192: 193: 194: 195: 196: 197: 198: 199: 200: 201: 202: 203: 204: 205: 206: 207: 208: 209: 210: 211: 212: 213: 214: 215: 216: 217: 218: 219: 220: 221: 222: 223: 224: 225: 226: 227: 228: 229: 230: 231: 232: 233: 234: 235: 236: 237: 238: 239: 240: 241: 242: 243: 244: 245: 246: 247: 248: 249: 250: 251: 252: 253:
<?php
namespace Guzzle\Service\Command\LocationVisitor\Request;
use Guzzle\Http\Message\RequestInterface;
use Guzzle\Service\Command\CommandInterface;
use Guzzle\Service\Description\Operation;
use Guzzle\Service\Description\Parameter;
class XmlVisitor extends AbstractRequestVisitor
{
protected $data;
protected $contentType = 'application/xml';
public function __construct()
{
$this->data = new \SplObjectStorage();
}
public function setContentTypeHeader($header)
{
$this->contentType = $header;
return $this;
}
public function visit(CommandInterface $command, RequestInterface $request, Parameter $param, $value)
{
$xml = isset($this->data[$command])
? $this->data[$command]
: $this->createRootElement($param->getParent());
$this->addXml($xml, $param, $value);
$this->data[$command] = $xml;
}
public function after(CommandInterface $command, RequestInterface $request)
{
$xml = null;
if (isset($this->data[$command])) {
$xml = $this->finishDocument($this->data[$command]);
unset($this->data[$command]);
} else {
$operation = $command->getOperation();
if ($operation->getData('xmlAllowEmpty')) {
$xmlWriter = $this->createRootElement($operation);
$xml = $this->finishDocument($xmlWriter);
}
}
if ($xml) {
if ($this->contentType && !$request->hasHeader('Content-Type')) {
$request->setHeader('Content-Type', $this->contentType);
}
$request->setBody($xml);
}
}
protected function createRootElement(Operation $operation)
{
static $defaultRoot = array('name' => 'Request');
$root = $operation->getData('xmlRoot') ?: $defaultRoot;
$encoding = $operation->getData('xmlEncoding');
$xmlWriter = $this->startDocument($encoding);
$xmlWriter->startElement($root['name']);
if (!empty($root['namespaces'])) {
foreach ((array) $root['namespaces'] as $prefix => $uri) {
$nsLabel = 'xmlns';
if (!is_numeric($prefix)) {
$nsLabel .= ':'.$prefix;
}
$xmlWriter->writeAttribute($nsLabel, $uri);
}
}
return $xmlWriter;
}
protected function addXml(\XMLWriter $xmlWriter, Parameter $param, $value)
{
if ($value === null) {
return;
}
$value = $param->filter($value);
$type = $param->getType();
$name = $param->getWireName();
$prefix = null;
$namespace = $param->getData('xmlNamespace');
if (false !== strpos($name, ':')) {
list($prefix, $name) = explode(':', $name, 2);
}
if ($type == 'object' || $type == 'array') {
if (!$param->getData('xmlFlattened')) {
$xmlWriter->startElementNS(null, $name, $namespace);
}
if ($param->getType() == 'array') {
$this->addXmlArray($xmlWriter, $param, $value);
} elseif ($param->getType() == 'object') {
$this->addXmlObject($xmlWriter, $param, $value);
}
if (!$param->getData('xmlFlattened')) {
$xmlWriter->endElement();
}
return;
}
if ($param->getData('xmlAttribute')) {
$this->writeAttribute($xmlWriter, $prefix, $name, $namespace, $value);
} else {
$this->writeElement($xmlWriter, $prefix, $name, $namespace, $value);
}
}
protected function writeAttribute($xmlWriter, $prefix, $name, $namespace, $value)
{
if (empty($namespace)) {
$xmlWriter->writeAttribute($name, $value);
} else {
$xmlWriter->writeAttributeNS($prefix, $name, $namespace, $value);
}
}
protected function writeElement(\XMLWriter $xmlWriter, $prefix, $name, $namespace, $value)
{
$xmlWriter->startElementNS($prefix, $name, $namespace);
if (strpbrk($value, '<>&')) {
$xmlWriter->writeCData($value);
} else {
$xmlWriter->writeRaw($value);
}
$xmlWriter->endElement();
}
protected function startDocument($encoding)
{
$xmlWriter = new \XMLWriter();
$xmlWriter->openMemory();
$xmlWriter->startDocument('1.0', $encoding);
return $xmlWriter;
}
protected function finishDocument($xmlWriter)
{
$xmlWriter->endDocument();
return $xmlWriter->outputMemory();
}
protected function addXmlArray(\XMLWriter $xmlWriter, Parameter $param, &$value)
{
if ($items = $param->getItems()) {
foreach ($value as $v) {
$this->addXml($xmlWriter, $items, $v);
}
}
}
protected function addXmlObject(\XMLWriter $xmlWriter, Parameter $param, &$value)
{
$noAttributes = array();
foreach ($value as $name => $v) {
if ($property = $param->getProperty($name)) {
if ($property->getData('xmlAttribute')) {
$this->addXml($xmlWriter, $property, $v);
} else {
$noAttributes[] = array('value' => $v, 'property' => $property);
}
}
}
foreach ($noAttributes as $element) {
$this->addXml($xmlWriter, $element['property'], $element['value']);
}
}
}