Open Journal Systems  3.3.0
Request/JsonVisitor.php
1 <?php
2 
4 
8 
13 {
15  protected $jsonContentType = 'application/json';
16 
18  protected $data;
19 
20  public function __construct()
21  {
22  $this->data = new \SplObjectStorage();
23  }
24 
33  public function setContentTypeHeader($header = 'application/json')
34  {
35  $this->jsonContentType = $header;
36 
37  return $this;
38  }
39 
40  public function visit(CommandInterface $command, RequestInterface $request, Parameter $param, $value)
41  {
42  if (isset($this->data[$command])) {
43  $json = $this->data[$command];
44  } else {
45  $json = array();
46  }
47  $json[$param->getWireName()] = $this->prepareValue($value, $param);
48  $this->data[$command] = $json;
49  }
50 
51  public function after(CommandInterface $command, RequestInterface $request)
52  {
53  if (isset($this->data[$command])) {
54  // Don't overwrite the Content-Type if one is set
55  if ($this->jsonContentType && !$request->hasHeader('Content-Type')) {
56  $request->setHeader('Content-Type', $this->jsonContentType);
57  }
58 
59  $request->setBody(json_encode($this->data[$command]));
60  unset($this->data[$command]);
61  }
62  }
63 }
Guzzle\Service\Command\LocationVisitor\Request
Definition: AbstractRequestVisitor.php:3
Guzzle\Service\Command\LocationVisitor\Request\JsonVisitor\setContentTypeHeader
setContentTypeHeader($header='application/json')
Definition: Request/JsonVisitor.php:39
Guzzle\Http\Message\RequestInterface
Definition: lib/vendor/guzzle/guzzle/src/Guzzle/Http/Message/RequestInterface.php:16
Guzzle\Service\Command\LocationVisitor\Request\JsonVisitor
Definition: Request/JsonVisitor.php:12
Guzzle\Http\Message\MessageInterface\setHeader
setHeader($header, $value)
Guzzle\Service\Command\LocationVisitor\Request\JsonVisitor\__construct
__construct()
Definition: Request/JsonVisitor.php:26
Guzzle\Service\Command\LocationVisitor\Request\AbstractRequestVisitor
Definition: AbstractRequestVisitor.php:9
Guzzle\Service\Description\Parameter
Definition: Parameter.php:10
Guzzle\Service\Command\LocationVisitor\Request\JsonVisitor\after
after(CommandInterface $command, RequestInterface $request)
Definition: Request/JsonVisitor.php:57
Guzzle\Service\Command\CommandInterface
Definition: CommandInterface.php:17
Guzzle\Service\Command\LocationVisitor\Request\JsonVisitor\$data
$data
Definition: Request/JsonVisitor.php:24
Guzzle\Service\Description\Parameter\getWireName
getWireName()
Definition: Parameter.php:250
Guzzle\Service\Command\LocationVisitor\Request\AbstractRequestVisitor\prepareValue
prepareValue($value, Parameter $param)
Definition: AbstractRequestVisitor.php:29
Guzzle\Service\Command\LocationVisitor\Request\JsonVisitor\$jsonContentType
$jsonContentType
Definition: Request/JsonVisitor.php:18
Guzzle\Service\Command\LocationVisitor\Request\JsonVisitor\visit
visit(CommandInterface $command, RequestInterface $request, Parameter $param, $value)
Definition: Request/JsonVisitor.php:46
Guzzle\Http\Message\MessageInterface\hasHeader
hasHeader($header)