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 JSON response data into a formatted array.
11:  *
12:  * Allows top level JSON parameters to be inserted into the result of a command. The top level attributes are grabbed
13:  * from the response's JSON data using the name value by default. Filters can be applied to parameters as they are
14:  * traversed. This allows data to be normalized before returning it to users (for example converting timestamps to
15:  * DateTime objects).
16:  */
17: class JsonVisitor extends AbstractResponseVisitor
18: {
19:     /**
20:      * {@inheritdoc}
21:      */
22:     public function before(CommandInterface $command, array &$result)
23:     {
24:         // Ensure that the result of the command is always rooted with the parsed JSON data
25:         $result = $command->getResponse()->json();
26:     }
27: 
28:     /**
29:      * {@inheritdoc}
30:      */
31:     public function visit(CommandInterface $command, Response $response, Parameter $param, &$value, $context =  null)
32:     {
33:         $name = $param->getName();
34:         $key = $param->getWireName();
35:         if (isset($value[$key])) {
36:             $this->recursiveProcess($param, $value[$key]);
37:             if ($key != $name) {
38:                 $value[$name] = $value[$key];
39:                 unset($value[$key]);
40:             }
41:         }
42:     }
43: 
44:     /**
45:      * Recursively process a parameter while applying filters
46:      *
47:      * @param Parameter $param API parameter being validated
48:      * @param mixed     $value Value to validate and process. The value may change during this process.
49:      */
50:     protected function recursiveProcess(Parameter $param, &$value)
51:     {
52:         if ($value === null) {
53:             return;
54:         }
55: 
56:         if (is_array($value)) {
57:             $type = $param->getType();
58:             if ($type == 'array') {
59:                 foreach ($value as &$item) {
60:                     $this->recursiveProcess($param->getItems(), $item);
61:                 }
62:             } elseif ($type == 'object' && !isset($value[0])) {
63:                 // On the above line, we ensure that the array is associative and not numerically indexed
64:                 if ($properties = $param->getProperties()) {
65:                     foreach ($properties as $property) {
66:                         $name = $property->getName();
67:                         $key = $property->getWireName();
68:                         if (isset($value[$key])) {
69:                             $this->recursiveProcess($property, $value[$key]);
70:                         }
71:                         if ($key != $name) {
72:                             $value[$name] = $value[$key];
73:                             unset($value[$key]);
74:                         }
75:                     }
76:                 }
77:             }
78:         }
79: 
80:         $value = $param->filter($value);
81:     }
82: }
83: 
php-coveralls API documentation generated by ApiGen 2.8.0