diff options
Diffstat (limited to 'vendor/guzzle/guzzle/src/Guzzle/Service/Command/LocationVisitor/Response')
8 files changed, 435 insertions, 0 deletions
diff --git a/vendor/guzzle/guzzle/src/Guzzle/Service/Command/LocationVisitor/Response/AbstractResponseVisitor.php b/vendor/guzzle/guzzle/src/Guzzle/Service/Command/LocationVisitor/Response/AbstractResponseVisitor.php new file mode 100644 index 0000000..d87eeb9 --- /dev/null +++ b/vendor/guzzle/guzzle/src/Guzzle/Service/Command/LocationVisitor/Response/AbstractResponseVisitor.php @@ -0,0 +1,26 @@ +<?php + +namespace Guzzle\Service\Command\LocationVisitor\Response; + +use Guzzle\Service\Command\CommandInterface; +use Guzzle\Http\Message\Response; +use Guzzle\Service\Description\Parameter; + +/** + * {@inheritdoc} + * @codeCoverageIgnore + */ +abstract class AbstractResponseVisitor implements ResponseVisitorInterface +{ + public function before(CommandInterface $command, array &$result) {} + + public function after(CommandInterface $command) {} + + public function visit( + CommandInterface $command, + Response $response, + Parameter $param, + &$value, + $context = null + ) {} +} diff --git a/vendor/guzzle/guzzle/src/Guzzle/Service/Command/LocationVisitor/Response/BodyVisitor.php b/vendor/guzzle/guzzle/src/Guzzle/Service/Command/LocationVisitor/Response/BodyVisitor.php new file mode 100644 index 0000000..f70b727 --- /dev/null +++ b/vendor/guzzle/guzzle/src/Guzzle/Service/Command/LocationVisitor/Response/BodyVisitor.php @@ -0,0 +1,23 @@ +<?php + +namespace Guzzle\Service\Command\LocationVisitor\Response; + +use Guzzle\Service\Command\CommandInterface; +use Guzzle\Http\Message\Response; +use Guzzle\Service\Description\Parameter; + +/** + * Visitor used to add the body of a response to a particular key + */ +class BodyVisitor extends AbstractResponseVisitor +{ + public function visit( + CommandInterface $command, + Response $response, + Parameter $param, + &$value, + $context = null + ) { + $value[$param->getName()] = $param->filter($response->getBody()); + } +} diff --git a/vendor/guzzle/guzzle/src/Guzzle/Service/Command/LocationVisitor/Response/HeaderVisitor.php b/vendor/guzzle/guzzle/src/Guzzle/Service/Command/LocationVisitor/Response/HeaderVisitor.php new file mode 100644 index 0000000..0f8737c --- /dev/null +++ b/vendor/guzzle/guzzle/src/Guzzle/Service/Command/LocationVisitor/Response/HeaderVisitor.php @@ -0,0 +1,50 @@ +<?php + +namespace Guzzle\Service\Command\LocationVisitor\Response; + +use Guzzle\Http\Message\Response; +use Guzzle\Service\Description\Parameter; +use Guzzle\Service\Command\CommandInterface; + +/** + * Location visitor used to add a particular header of a response to a key in the result + */ +class HeaderVisitor extends AbstractResponseVisitor +{ + public function visit( + CommandInterface $command, + Response $response, + Parameter $param, + &$value, + $context = null + ) { + if ($param->getType() == 'object' && $param->getAdditionalProperties() instanceof Parameter) { + $this->processPrefixedHeaders($response, $param, $value); + } else { + $value[$param->getName()] = $param->filter((string) $response->getHeader($param->getWireName())); + } + } + + /** + * Process a prefixed header array + * + * @param Response $response Response that contains the headers + * @param Parameter $param Parameter object + * @param array $value Value response array to modify + */ + protected function processPrefixedHeaders(Response $response, Parameter $param, &$value) + { + // Grab prefixed headers that should be placed into an array with the prefix stripped + if ($prefix = $param->getSentAs()) { + $container = $param->getName(); + $len = strlen($prefix); + // Find all matching headers and place them into the containing element + foreach ($response->getHeaders()->toArray() as $key => $header) { + if (stripos($key, $prefix) === 0) { + // Account for multi-value headers + $value[$container][substr($key, $len)] = count($header) == 1 ? end($header) : $header; + } + } + } + } +} diff --git a/vendor/guzzle/guzzle/src/Guzzle/Service/Command/LocationVisitor/Response/JsonVisitor.php b/vendor/guzzle/guzzle/src/Guzzle/Service/Command/LocationVisitor/Response/JsonVisitor.php new file mode 100644 index 0000000..a609ebd --- /dev/null +++ b/vendor/guzzle/guzzle/src/Guzzle/Service/Command/LocationVisitor/Response/JsonVisitor.php @@ -0,0 +1,93 @@ +<?php + +namespace Guzzle\Service\Command\LocationVisitor\Response; + +use Guzzle\Http\Message\Response; +use Guzzle\Service\Description\Parameter; +use Guzzle\Service\Command\CommandInterface; + +/** + * Location visitor used to marshal JSON response data into a formatted array. + * + * Allows top level JSON parameters to be inserted into the result of a command. The top level attributes are grabbed + * from the response's JSON data using the name value by default. Filters can be applied to parameters as they are + * traversed. This allows data to be normalized before returning it to users (for example converting timestamps to + * DateTime objects). + */ +class JsonVisitor extends AbstractResponseVisitor +{ + public function before(CommandInterface $command, array &$result) + { + // Ensure that the result of the command is always rooted with the parsed JSON data + $result = $command->getResponse()->json(); + } + + public function visit( + CommandInterface $command, + Response $response, + Parameter $param, + &$value, + $context = null + ) { + $name = $param->getName(); + $key = $param->getWireName(); + if (isset($value[$key])) { + $this->recursiveProcess($param, $value[$key]); + if ($key != $name) { + $value[$name] = $value[$key]; + unset($value[$key]); + } + } + } + + /** + * Recursively process a parameter while applying filters + * + * @param Parameter $param API parameter being validated + * @param mixed $value Value to validate and process. The value may change during this process. + */ + protected function recursiveProcess(Parameter $param, &$value) + { + if ($value === null) { + return; + } + + if (is_array($value)) { + $type = $param->getType(); + if ($type == 'array') { + foreach ($value as &$item) { + $this->recursiveProcess($param->getItems(), $item); + } + } elseif ($type == 'object' && !isset($value[0])) { + // On the above line, we ensure that the array is associative and not numerically indexed + $knownProperties = array(); + if ($properties = $param->getProperties()) { + foreach ($properties as $property) { + $name = $property->getName(); + $key = $property->getWireName(); + $knownProperties[$name] = 1; + if (isset($value[$key])) { + $this->recursiveProcess($property, $value[$key]); + if ($key != $name) { + $value[$name] = $value[$key]; + unset($value[$key]); + } + } + } + } + + // Remove any unknown and potentially unsafe properties + if ($param->getAdditionalProperties() === false) { + $value = array_intersect_key($value, $knownProperties); + } elseif (($additional = $param->getAdditionalProperties()) !== true) { + // Validate and filter additional properties + foreach ($value as &$v) { + $this->recursiveProcess($additional, $v); + } + } + } + } + + $value = $param->filter($value); + } +} diff --git a/vendor/guzzle/guzzle/src/Guzzle/Service/Command/LocationVisitor/Response/ReasonPhraseVisitor.php b/vendor/guzzle/guzzle/src/Guzzle/Service/Command/LocationVisitor/Response/ReasonPhraseVisitor.php new file mode 100644 index 0000000..1b10ebc --- /dev/null +++ b/vendor/guzzle/guzzle/src/Guzzle/Service/Command/LocationVisitor/Response/ReasonPhraseVisitor.php @@ -0,0 +1,23 @@ +<?php + +namespace Guzzle\Service\Command\LocationVisitor\Response; + +use Guzzle\Http\Message\Response; +use Guzzle\Service\Description\Parameter; +use Guzzle\Service\Command\CommandInterface; + +/** + * Location visitor used to add the reason phrase of a response to a key in the result + */ +class ReasonPhraseVisitor extends AbstractResponseVisitor +{ + public function visit( + CommandInterface $command, + Response $response, + Parameter $param, + &$value, + $context = null + ) { + $value[$param->getName()] = $response->getReasonPhrase(); + } +} diff --git a/vendor/guzzle/guzzle/src/Guzzle/Service/Command/LocationVisitor/Response/ResponseVisitorInterface.php b/vendor/guzzle/guzzle/src/Guzzle/Service/Command/LocationVisitor/Response/ResponseVisitorInterface.php new file mode 100644 index 0000000..033f40c --- /dev/null +++ b/vendor/guzzle/guzzle/src/Guzzle/Service/Command/LocationVisitor/Response/ResponseVisitorInterface.php @@ -0,0 +1,46 @@ +<?php + +namespace Guzzle\Service\Command\LocationVisitor\Response; + +use Guzzle\Http\Message\Response; +use Guzzle\Service\Description\Parameter; +use Guzzle\Service\Command\CommandInterface; + +/** + * Location visitor used to parse values out of a response into an associative array + */ +interface ResponseVisitorInterface +{ + /** + * Called before visiting all parameters. This can be used for seeding the result of a command with default + * data (e.g. populating with JSON data in the response then adding to the parsed data). + * + * @param CommandInterface $command Command being visited + * @param array $result Result value to update if needed (e.g. parsing XML or JSON) + */ + public function before(CommandInterface $command, array &$result); + + /** + * Called after visiting all parameters + * + * @param CommandInterface $command Command being visited + */ + public function after(CommandInterface $command); + + /** + * Called once for each parameter being visited that matches the location type + * + * @param CommandInterface $command Command being visited + * @param Response $response Response being visited + * @param Parameter $param Parameter being visited + * @param mixed $value Result associative array value being updated by reference + * @param mixed $context Parsing context + */ + public function visit( + CommandInterface $command, + Response $response, + Parameter $param, + &$value, + $context = null + ); +} diff --git a/vendor/guzzle/guzzle/src/Guzzle/Service/Command/LocationVisitor/Response/StatusCodeVisitor.php b/vendor/guzzle/guzzle/src/Guzzle/Service/Command/LocationVisitor/Response/StatusCodeVisitor.php new file mode 100644 index 0000000..00c5ce0 --- /dev/null +++ b/vendor/guzzle/guzzle/src/Guzzle/Service/Command/LocationVisitor/Response/StatusCodeVisitor.php @@ -0,0 +1,23 @@ +<?php + +namespace Guzzle\Service\Command\LocationVisitor\Response; + +use Guzzle\Http\Message\Response; +use Guzzle\Service\Description\Parameter; +use Guzzle\Service\Command\CommandInterface; + +/** + * Location visitor used to add the status code of a response to a key in the result + */ +class StatusCodeVisitor extends AbstractResponseVisitor +{ + public function visit( + CommandInterface $command, + Response $response, + Parameter $param, + &$value, + $context = null + ) { + $value[$param->getName()] = $response->getStatusCode(); + } +} diff --git a/vendor/guzzle/guzzle/src/Guzzle/Service/Command/LocationVisitor/Response/XmlVisitor.php b/vendor/guzzle/guzzle/src/Guzzle/Service/Command/LocationVisitor/Response/XmlVisitor.php new file mode 100644 index 0000000..bb7124b --- /dev/null +++ b/vendor/guzzle/guzzle/src/Guzzle/Service/Command/LocationVisitor/Response/XmlVisitor.php @@ -0,0 +1,151 @@ +<?php + +namespace Guzzle\Service\Command\LocationVisitor\Response; + +use Guzzle\Http\Message\Response; +use Guzzle\Service\Description\Parameter; +use Guzzle\Service\Command\CommandInterface; + +/** + * Location visitor used to marshal XML response data into a formatted array + */ +class XmlVisitor extends AbstractResponseVisitor +{ + public function before(CommandInterface $command, array &$result) + { + // Set the result of the command to the array conversion of the XML body + $result = json_decode(json_encode($command->getResponse()->xml()), true); + } + + public function visit( + CommandInterface $command, + Response $response, + Parameter $param, + &$value, + $context = null + ) { + $sentAs = $param->getWireName(); + $name = $param->getName(); + if (isset($value[$sentAs])) { + $this->recursiveProcess($param, $value[$sentAs]); + if ($name != $sentAs) { + $value[$name] = $value[$sentAs]; + unset($value[$sentAs]); + } + } + } + + /** + * Recursively process a parameter while applying filters + * + * @param Parameter $param API parameter being processed + * @param mixed $value Value to validate and process. The value may change during this process. + */ + protected function recursiveProcess(Parameter $param, &$value) + { + $type = $param->getType(); + + if (!is_array($value)) { + if ($type == 'array') { + // Cast to an array if the value was a string, but should be an array + $this->recursiveProcess($param->getItems(), $value); + $value = array($value); + } + } elseif ($type == 'object') { + $this->processObject($param, $value); + } elseif ($type == 'array') { + $this->processArray($param, $value); + } elseif ($type == 'string' && gettype($value) == 'array') { + $value = ''; + } + + if ($value !== null) { + $value = $param->filter($value); + } + } + + /** + * Process an array + * + * @param Parameter $param API parameter being parsed + * @param mixed $value Value to process + */ + protected function processArray(Parameter $param, &$value) + { + // Convert the node if it was meant to be an array + if (!isset($value[0])) { + // Collections fo nodes are sometimes wrapped in an additional array. For example: + // <Items><Item><a>1</a></Item><Item><a>2</a></Item></Items> should become: + // array('Items' => array(array('a' => 1), array('a' => 2)) + // Some nodes are not wrapped. For example: <Foo><a>1</a></Foo><Foo><a>2</a></Foo> + // should become array('Foo' => array(array('a' => 1), array('a' => 2)) + if ($param->getItems() && isset($value[$param->getItems()->getWireName()])) { + // Account for the case of a collection wrapping wrapped nodes: Items => Item[] + $value = $value[$param->getItems()->getWireName()]; + // If the wrapped node only had one value, then make it an array of nodes + if (!isset($value[0]) || !is_array($value)) { + $value = array($value); + } + } elseif (!empty($value)) { + // Account for repeated nodes that must be an array: Foo => Baz, Foo => Baz, but only if the + // value is set and not empty + $value = array($value); + } + } + + foreach ($value as &$item) { + $this->recursiveProcess($param->getItems(), $item); + } + } + + /** + * Process an object + * + * @param Parameter $param API parameter being parsed + * @param mixed $value Value to process + */ + protected function processObject(Parameter $param, &$value) + { + // Ensure that the array is associative and not numerically indexed + if (!isset($value[0]) && ($properties = $param->getProperties())) { + $knownProperties = array(); + foreach ($properties as $property) { + $name = $property->getName(); + $sentAs = $property->getWireName(); + $knownProperties[$name] = 1; + if ($property->getData('xmlAttribute')) { + $this->processXmlAttribute($property, $value); + } elseif (isset($value[$sentAs])) { + $this->recursiveProcess($property, $value[$sentAs]); + if ($name != $sentAs) { + $value[$name] = $value[$sentAs]; + unset($value[$sentAs]); + } + } + } + + // Remove any unknown and potentially unsafe properties + if ($param->getAdditionalProperties() === false) { + $value = array_intersect_key($value, $knownProperties); + } + } + } + + /** + * Process an XML attribute property + * + * @param Parameter $property Property to process + * @param array $value Value to process and update + */ + protected function processXmlAttribute(Parameter $property, array &$value) + { + $sentAs = $property->getWireName(); + if (isset($value['@attributes'][$sentAs])) { + $value[$property->getName()] = $value['@attributes'][$sentAs]; + unset($value['@attributes'][$sentAs]); + if (empty($value['@attributes'])) { + unset($value['@attributes']); + } + } + } +} |