Overview

Namespaces

  • Contrib
    • Bundle
      • CoverallsBundle
        • Console
        • Entity
      • CoverallsV1Bundle
        • Api
        • Collector
        • Command
        • Config
        • Entity
          • Git
    • Component
      • File
      • Log
      • System
        • Git
  • Guzzle
    • Batch
      • Exception
    • Cache
    • Common
      • Exception
    • Http
      • Curl
      • Exception
      • Message
      • QueryAggregator
    • Inflection
    • Iterator
    • Log
    • Parser
      • Cookie
      • Message
      • UriTemplate
      • Url
    • Plugin
      • Async
      • Backoff
      • Cache
      • Cookie
        • CookieJar
        • Exception
      • CurlAuth
      • ErrorResponse
        • Exception
      • History
      • Log
      • Md5
      • Mock
      • Oauth
    • Service
      • Builder
      • Command
        • Factory
        • LocationVisitor
          • Request
          • Response
      • Description
      • Exception
      • Resource
    • Stream
  • PHP
  • Psr
    • Log
  • Symfony
    • Component
      • Config
        • Definition
          • Builder
          • Exception
        • Exception
        • Loader
        • Resource
        • Util
      • Console
        • Command
        • Formatter
        • Helper
        • Input
        • Output
        • Tester
      • EventDispatcher
        • Debug
      • Finder
        • Adapter
        • Comparator
        • Exception
        • Expression
        • Iterator
        • Shell
      • Stopwatch
      • Yaml
        • Exception

Classes

  • AbstractResponseVisitor
  • BodyVisitor
  • HeaderVisitor
  • JsonVisitor
  • ReasonPhraseVisitor
  • StatusCodeVisitor
  • XmlVisitor

Interfaces

  • ResponseVisitorInterface
  • Overview
  • Namespace
  • Class
  • Tree
  • Todo
  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:  * Location visitor used to marshal XML response data into a formatted array
 11:  */
 12: class XmlVisitor extends AbstractResponseVisitor
 13: {
 14:     /**
 15:      * {@inheritdoc}
 16:      */
 17:     public function before(CommandInterface $command, array &$result)
 18:     {
 19:         // Set the result of the command to the array conversion of the XML body
 20:         $result = json_decode(json_encode($command->getResponse()->xml()), true);
 21:     }
 22: 
 23:     /**
 24:      * {@inheritdoc}
 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:      * Recursively process a parameter while applying filters
 41:      *
 42:      * @param Parameter $param API parameter being processed
 43:      * @param mixed     $value Value to validate and process. The value may change during this process.
 44:      */
 45:     protected function recursiveProcess(Parameter $param, &$value)
 46:     {
 47:         $type = $param->getType();
 48: 
 49:         if (!is_array($value)) {
 50:             if ($type == 'array') {
 51:                 // Cast to an array if the value was a string, but should be an array
 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:      * Process an array
 68:      *
 69:      * @param Parameter $param API parameter being parsed
 70:      * @param mixed     $value Value to process
 71:      */
 72:     protected function processArray(Parameter $param, &$value)
 73:     {
 74:         // Convert the node if it was meant to be an array
 75:         if (!isset($value[0])) {
 76:             // Collections fo nodes are sometimes wrapped in an additional array. For example:
 77:             // <Items><Item><a>1</a></Item><Item><a>2</a></Item></Items> should become:
 78:             // array('Items' => array(array('a' => 1), array('a' => 2))
 79:             // Some nodes are not wrapped. For example: <Foo><a>1</a></Foo><Foo><a>2</a></Foo>
 80:             // should become array('Foo' => array(array('a' => 1), array('a' => 2))
 81:             if ($param->getItems() && isset($value[$param->getItems()->getWireName()])) {
 82:                 // Account for the case of a collection wrapping wrapped nodes: Items => Item[]
 83:                 $value = $value[$param->getItems()->getWireName()];
 84:                 // If the wrapped node only had one value, then make it an array of nodes
 85:                 if (!isset($value[0]) || !is_array($value)) {
 86:                     $value = array($value);
 87:                 }
 88:             } elseif (!empty($value)) {
 89:                 // Account for repeated nodes that must be an array: Foo => Baz, Foo => Baz, but only if the
 90:                 // value is set and not empty
 91:                 $value = array($value);
 92:             }
 93:         }
 94: 
 95:         foreach ($value as &$item) {
 96:             $this->recursiveProcess($param->getItems(), $item);
 97:         }
 98:     }
 99: 
100:     /**
101:      * Process an object
102:      *
103:      * @param Parameter $param API parameter being parsed
104:      * @param mixed     $value Value to process
105:      */
106:     protected function processObject(Parameter $param, &$value)
107:     {
108:         // Ensure that the array is associative and not numerically indexed
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:      * Process an XML attribute property
128:      *
129:      * @param Parameter $property Property to process
130:      * @param array     $value    Value to process and update
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: 
php-coveralls API documentation generated by ApiGen 2.8.0