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

  • AbstractRequestVisitor
  • BodyVisitor
  • HeaderVisitor
  • JsonVisitor
  • PostFieldVisitor
  • PostFileVisitor
  • QueryVisitor
  • ResponseBodyVisitor
  • XmlVisitor

Interfaces

  • RequestVisitorInterface
  • Overview
  • Namespace
  • Class
  • Tree
  • Todo
 1: <?php
 2: 
 3: namespace Guzzle\Service\Command\LocationVisitor\Request;
 4: 
 5: use Guzzle\Http\Message\RequestInterface;
 6: use Guzzle\Service\Command\CommandInterface;
 7: use Guzzle\Service\Description\Parameter;
 8: 
 9: /**
10:  * Visitor used to apply a parameter to an array that will be serialized as a top level key-value pair in a JSON body
11:  */
12: class JsonVisitor extends AbstractRequestVisitor
13: {
14:     /**
15:      * @var bool Whether or not to add a Content-Type header when JSON is found
16:      */
17:     protected $jsonContentType = 'application/json';
18: 
19:     /**
20:      * @var \SplObjectStorage Data object for persisting JSON data
21:      */
22:     protected $data;
23: 
24:     /**
25:      * This visitor uses an {@see \SplObjectStorage} to associate JSON data with commands
26:      */
27:     public function __construct()
28:     {
29:         $this->data = new \SplObjectStorage();
30:     }
31: 
32:     /**
33:      * Set the Content-Type header to add to the request if JSON is added to the body. This visitor does not add a
34:      * Content-Type header unless you specify one here.
35:      *
36:      * @param string $header Header to set when JSON is added (e.g. application/json)
37:      *
38:      * @return self
39:      */
40:     public function setContentTypeHeader($header = 'application/json')
41:     {
42:         $this->jsonContentType = $header;
43: 
44:         return $this;
45:     }
46: 
47:     /**
48:      * {@inheritdoc}
49:      */
50:     public function visit(CommandInterface $command, RequestInterface $request, Parameter $param, $value)
51:     {
52:         if (isset($this->data[$command])) {
53:             $json = $this->data[$command];
54:         } else {
55:             $json = array();
56:         }
57:         $json[$param->getWireName()] = $this->prepareValue($value, $param);
58:         $this->data[$command] = $json;
59:     }
60: 
61:     /**
62:      * {@inheritdoc}
63:      */
64:     public function after(CommandInterface $command, RequestInterface $request)
65:     {
66:         if (isset($this->data[$command])) {
67:             $request->setBody(json_encode($this->data[$command]));
68:             unset($this->data[$command]);
69:             // Don't overwrite the Content-Type if one is set
70:             if ($this->jsonContentType && !$request->hasHeader('Content-Type')) {
71:                 $request->setHeader('Content-Type', $this->jsonContentType);
72:             }
73:         }
74:     }
75: }
76: 
php-coveralls API documentation generated by ApiGen 2.8.0