I have a basic service description schema to get a user model that looks like this:
{
"name": "API",
"baseUrl": "http://localhost/",
"operations": {
"GetUser": {
"httpMethod": "GET",
"uri": "users/{user_id}",
"summary": "Show user details",
"responseClass": "GetUserOutput",
"parameters": {
"user_id": {
"location": "uri",
"description": "ID of the user to be returned"
}
}
}
},
"models": {
"User" : {
"type": "object",
"properties": {
"id": {
"location": "json",
"type": "integer",
"sentAs": "user_id"
},
"username": {
"location": "json",
"type": "string"
},
"email": {
"location": "json",
"type": "string"
}
}
},
"GetUserOutput": {
"$ref": "User"
}
}
}
my client does the following:
require_once('../../vendor/autoload.php');
$client = new \Guzzle\Service\Client();
$client->setDescription(\Guzzle\Service\Description\ServiceDescription::factory(__DIR__ . '/client.json'));
$authPlugin = new \Guzzle\Plugin\CurlAuth\CurlAuthPlugin('23', '9bd2cb3f1bccc01c0c1091d7e88e51b208b3792b');
$client->addSubscriber($authPlugin);
$command = $client->getCommand('getUser', array('user_id' => 23));
$request = $command->prepare();
$request->addHeader('Accept', 'application/json');
try {
$result = $command->execute();
echo '<pre>' . print_r($result, true) . '</pre>';
}
which returns a Guzzle\Service\Resource\Model Object, which at the bottom contains the user data i wanted:
[data:protected] => Array
(
[user] => Array
(
[id] => 23
[username] => gardni
[email] => email@email.com
how do i map this to the schema object? or more importantly my own application object? apparently the solution here doesn't work:
class User implements ResponseClassInterface
{
public static function fromCommand(OperationCommand $command)
{
$parser = OperationResponseParser::getInstance();
$parsedModel = $parser->parse($command);
return new self($parsedModel);
}
public function __construct(Model $parsedModel)
{
// Do something with the parsed model object
}
}