1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31: 32: 33: 34: 35: 36: 37: 38: 39: 40: 41: 42: 43: 44: 45: 46: 47: 48: 49: 50: 51: 52: 53: 54: 55: 56: 57: 58: 59: 60: 61: 62: 63: 64: 65: 66: 67: 68: 69: 70: 71: 72: 73: 74: 75: 76: 77: 78: 79: 80: 81: 82: 83: 84: 85: 86: 87: 88: 89: 90: 91: 92: 93: 94:
<?php
namespace Guzzle\Service\Command\LocationVisitor\Response;
use Guzzle\Http\Message\Response;
use Guzzle\Service\Description\Parameter;
use Guzzle\Service\Command\CommandInterface;
class JsonVisitor extends AbstractResponseVisitor
{
public function before(CommandInterface $command, array &$result)
{
$result = $command->getResponse()->json();
}
public function visit(
CommandInterface $command,
Response $response,
Parameter $param,
&$value,
$context = null
) {
$name = $param->getName();
$key = $param->getWireName();
if (isset($value[$key])) {
$this->recursiveProcess($param, $value[$key]);
if ($key != $name) {
$value[$name] = $value[$key];
unset($value[$key]);
}
}
}
protected function recursiveProcess(Parameter $param, &$value)
{
if ($value === null) {
return;
}
if (is_array($value)) {
$type = $param->getType();
if ($type == 'array') {
foreach ($value as &$item) {
$this->recursiveProcess($param->getItems(), $item);
}
} elseif ($type == 'object' && !isset($value[0])) {
$knownProperties = array();
if ($properties = $param->getProperties()) {
foreach ($properties as $property) {
$name = $property->getName();
$key = $property->getWireName();
$knownProperties[$name] = 1;
if (isset($value[$key])) {
$this->recursiveProcess($property, $value[$key]);
if ($key != $name) {
$value[$name] = $value[$key];
unset($value[$key]);
}
}
}
}
if ($param->getAdditionalProperties() === false) {
$value = array_intersect_key($value, $knownProperties);
} elseif (($additional = $param->getAdditionalProperties()) !== true) {
foreach ($value as &$v) {
$this->recursiveProcess($additional, $v);
}
}
}
}
$value = $param->filter($value);
}
}