Open Journal Systems  3.3.0
Response/XmlVisitor.php
1 <?php
2 
4 
8 
13 {
14  public function before(CommandInterface $command, array &$result)
15  {
16  // Set the result of the command to the array conversion of the XML body
17  $result = json_decode(json_encode($command->getResponse()->xml()), true);
18  }
19 
20  public function visit(
21  CommandInterface $command,
22  Response $response,
23  Parameter $param,
24  &$value,
25  $context = null
26  ) {
27  $sentAs = $param->getWireName();
28  $name = $param->getName();
29  if (isset($value[$sentAs])) {
30  $this->recursiveProcess($param, $value[$sentAs]);
31  if ($name != $sentAs) {
32  $value[$name] = $value[$sentAs];
33  unset($value[$sentAs]);
34  }
35  }
36  }
37 
44  protected function recursiveProcess(Parameter $param, &$value)
45  {
46  $type = $param->getType();
47 
48  if (!is_array($value)) {
49  if ($type == 'array') {
50  // Cast to an array if the value was a string, but should be an array
51  $this->recursiveProcess($param->getItems(), $value);
52  $value = array($value);
53  }
54  } elseif ($type == 'object') {
55  $this->processObject($param, $value);
56  } elseif ($type == 'array') {
57  $this->processArray($param, $value);
58  } elseif ($type == 'string' && gettype($value) == 'array') {
59  $value = '';
60  }
61 
62  if ($value !== null) {
63  $value = $param->filter($value);
64  }
65  }
66 
73  protected function processArray(Parameter $param, &$value)
74  {
75  // Convert the node if it was meant to be an array
76  if (!isset($value[0])) {
77  // Collections fo nodes are sometimes wrapped in an additional array. For example:
78  // <Items><Item><a>1</a></Item><Item><a>2</a></Item></Items> should become:
79  // array('Items' => array(array('a' => 1), array('a' => 2))
80  // Some nodes are not wrapped. For example: <Foo><a>1</a></Foo><Foo><a>2</a></Foo>
81  // should become array('Foo' => array(array('a' => 1), array('a' => 2))
82  if ($param->getItems() && isset($value[$param->getItems()->getWireName()])) {
83  // Account for the case of a collection wrapping wrapped nodes: Items => Item[]
84  $value = $value[$param->getItems()->getWireName()];
85  // If the wrapped node only had one value, then make it an array of nodes
86  if (!isset($value[0]) || !is_array($value)) {
87  $value = array($value);
88  }
89  } elseif (!empty($value)) {
90  // Account for repeated nodes that must be an array: Foo => Baz, Foo => Baz, but only if the
91  // value is set and not empty
92  $value = array($value);
93  }
94  }
95 
96  foreach ($value as &$item) {
97  $this->recursiveProcess($param->getItems(), $item);
98  }
99  }
100 
107  protected function processObject(Parameter $param, &$value)
108  {
109  // Ensure that the array is associative and not numerically indexed
110  if (!isset($value[0]) && ($properties = $param->getProperties())) {
111  $knownProperties = array();
112  foreach ($properties as $property) {
113  $name = $property->getName();
114  $sentAs = $property->getWireName();
115  $knownProperties[$name] = 1;
116  if ($property->getData('xmlAttribute')) {
117  $this->processXmlAttribute($property, $value);
118  } elseif (isset($value[$sentAs])) {
119  $this->recursiveProcess($property, $value[$sentAs]);
120  if ($name != $sentAs) {
121  $value[$name] = $value[$sentAs];
122  unset($value[$sentAs]);
123  }
124  }
125  }
126 
127  // Remove any unknown and potentially unsafe properties
128  if ($param->getAdditionalProperties() === false) {
129  $value = array_intersect_key($value, $knownProperties);
130  }
131  }
132  }
133 
140  protected function processXmlAttribute(Parameter $property, array &$value)
141  {
142  $sentAs = $property->getWireName();
143  if (isset($value['@attributes'][$sentAs])) {
144  $value[$property->getName()] = $value['@attributes'][$sentAs];
145  unset($value['@attributes'][$sentAs]);
146  if (empty($value['@attributes'])) {
147  unset($value['@attributes']);
148  }
149  }
150  }
151 }
Guzzle\Service\Description\Parameter\getItems
getItems()
Definition: Parameter.php:820
Guzzle\Service\Command\LocationVisitor\Response
Definition: AbstractResponseVisitor.php:3
Guzzle\Service\Description\Parameter\filter
filter($value)
Definition: Parameter.php:201
Guzzle\Service\Command\LocationVisitor\Response\XmlVisitor
Definition: Response/XmlVisitor.php:12
Guzzle\Service\Description\Parameter
Definition: Parameter.php:10
Guzzle\Http\Message\Response
Definition: lib/vendor/guzzle/guzzle/src/Guzzle/Http/Message/Response.php:17
Guzzle\Service\Command\LocationVisitor\Response\XmlVisitor\before
before(CommandInterface $command, array &$result)
Definition: Response/XmlVisitor.php:14
Guzzle\Service\Command\CommandInterface
Definition: CommandInterface.php:17
Guzzle\Service\Command\CommandInterface\getResponse
getResponse()
Guzzle\Service\Command\LocationVisitor\Response\XmlVisitor\recursiveProcess
recursiveProcess(Parameter $param, &$value)
Definition: Response/XmlVisitor.php:44
Guzzle\Service\Description\Parameter\getWireName
getWireName()
Definition: Parameter.php:250
Guzzle\Service\Description\Parameter\getProperties
getProperties()
Definition: Parameter.php:705
Guzzle\Service\Description\Parameter\getName
getName()
Definition: Parameter.php:240
Guzzle\Service\Description\Parameter\getAdditionalProperties
getAdditionalProperties()
Definition: Parameter.php:775
Guzzle\Service\Command\LocationVisitor\Response\XmlVisitor\visit
visit(CommandInterface $command, Response $response, Parameter $param, &$value, $context=null)
Definition: Response/XmlVisitor.php:20
Guzzle\Service\Command\LocationVisitor\Response\XmlVisitor\processXmlAttribute
processXmlAttribute(Parameter $property, array &$value)
Definition: Response/XmlVisitor.php:140
Guzzle\Service\Description\Parameter\getType
getType()
Definition: Parameter.php:274
Guzzle\Service\Command\LocationVisitor\Response\AbstractResponseVisitor
Definition: AbstractResponseVisitor.php:13
Guzzle\Service\Command\LocationVisitor\Response\XmlVisitor\processArray
processArray(Parameter $param, &$value)
Definition: Response/XmlVisitor.php:73
Guzzle\Service\Command\LocationVisitor\Response\XmlVisitor\processObject
processObject(Parameter $param, &$value)
Definition: Response/XmlVisitor.php:107