1: <?php
2:
3: namespace Guzzle\Service\Command\LocationVisitor\Response;
4:
5: use Guzzle\Http\Message\Response;
6: use Guzzle\Service\Description\Parameter;
7: use Guzzle\Service\Command\CommandInterface;
8:
9: 10: 11:
12: class XmlVisitor extends AbstractResponseVisitor
13: {
14: 15: 16:
17: public function before(CommandInterface $command, array &$result)
18: {
19:
20: $result = json_decode(json_encode($command->getResponse()->xml()), true);
21: }
22:
23: 24: 25:
26: public function visit(CommandInterface $command, Response $response, Parameter $param, &$value, $context = null)
27: {
28: $sentAs = $param->getWireName();
29: $name = $param->getName();
30: if (isset($value[$sentAs])) {
31: $this->recursiveProcess($param, $value[$sentAs]);
32: if ($name != $sentAs) {
33: $value[$name] = $value[$sentAs];
34: unset($value[$sentAs]);
35: }
36: }
37: }
38:
39: 40: 41: 42: 43: 44:
45: protected function recursiveProcess(Parameter $param, &$value)
46: {
47: $type = $param->getType();
48:
49: if (!is_array($value)) {
50: if ($type == 'array') {
51:
52: $this->recursiveProcess($param->getItems(), $value);
53: $value = array($value);
54: }
55: } elseif ($type == 'object') {
56: $this->processObject($param, $value);
57: } elseif ($type == 'array') {
58: $this->processArray($param, $value);
59: }
60:
61: if ($value !== null) {
62: $value = $param->filter($value);
63: }
64: }
65:
66: 67: 68: 69: 70: 71:
72: protected function processArray(Parameter $param, &$value)
73: {
74:
75: if (!isset($value[0])) {
76:
77:
78:
79:
80:
81: if ($param->getItems() && isset($value[$param->getItems()->getWireName()])) {
82:
83: $value = $value[$param->getItems()->getWireName()];
84:
85: if (!isset($value[0]) || !is_array($value)) {
86: $value = array($value);
87: }
88: } elseif (!empty($value)) {
89:
90:
91: $value = array($value);
92: }
93: }
94:
95: foreach ($value as &$item) {
96: $this->recursiveProcess($param->getItems(), $item);
97: }
98: }
99:
100: 101: 102: 103: 104: 105:
106: protected function processObject(Parameter $param, &$value)
107: {
108:
109: if (!isset($value[0]) && ($properties = $param->getProperties())) {
110: foreach ($properties as $property) {
111: $name = $property->getName();
112: $sentAs = $property->getWireName();
113: if ($property->getData('xmlAttribute')) {
114: $this->processXmlAttribute($property, $value);
115: } elseif (isset($value[$sentAs])) {
116: $this->recursiveProcess($property, $value[$sentAs]);
117: if ($name != $sentAs) {
118: $value[$name] = $value[$sentAs];
119: unset($value[$sentAs]);
120: }
121: }
122: }
123: }
124: }
125:
126: 127: 128: 129: 130: 131:
132: protected function processXmlAttribute(Parameter $property, array &$value)
133: {
134: $sentAs = $property->getWireName();
135: if (isset($value['@attributes'][$sentAs])) {
136: $value[$property->getName()] = $value['@attributes'][$sentAs];
137: unset($value['@attributes'][$sentAs]);
138: if (empty($value['@attributes'])) {
139: unset($value['@attributes']);
140: }
141: }
142: }
143: }
144: