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: 11:
12: class JsonVisitor extends AbstractRequestVisitor
13: {
14: 15: 16:
17: protected $jsonContentType = 'application/json';
18:
19: 20: 21:
22: protected $data;
23:
24: 25: 26:
27: public function __construct()
28: {
29: $this->data = new \SplObjectStorage();
30: }
31:
32: 33: 34: 35: 36: 37: 38: 39:
40: public function ($header = 'application/json')
41: {
42: $this->jsonContentType = $header;
43:
44: return $this;
45: }
46:
47: 48: 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: 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:
70: if ($this->jsonContentType && !$request->hasHeader('Content-Type')) {
71: $request->setHeader('Content-Type', $this->jsonContentType);
72: }
73: }
74: }
75: }
76: