1
我有一個基本的服務描述的模式來獲得,看起來像這樣一個用戶模型:如何在狂飲服務描述架構映射到應用模式
{
"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"
}
}
}
我的客戶將執行以下操作:
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>';
}
它返回一個狂飲\服務\資源\模型對象,它在底部包含我想要的用戶數據:
[data:protected] => Array
(
[user] => Array
(
[id] => 23
[username] => gardni
[email] => [email protected]
如何將此映射到模式對象?或者更重要的是我自己的應用程序對象?顯然這裏的解決方案不起作用:
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
}
}