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: 11: 12: 13: 14: 15: 16:
17: class JsonVisitor extends AbstractResponseVisitor
18: {
19: 20: 21:
22: public function before(CommandInterface $command, array &$result)
23: {
24:
25: $result = $command->getResponse()->json();
26: }
27:
28: 29: 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: 46: 47: 48: 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:
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: