jeyroik / extas-expands
此包最新版本(4.0.1)没有可用的许可证信息。
Extas 扩展包
4.0.1
2020-08-25 16:05 UTC
Requires
Requires (Dev)
README
描述
该包允许创建可展开的对象,即大约是这样的
GET /index Accept: application/json 响应
{
"app": {
"name": "example app",
"expand": ["app.version", "app.player"]
}
}
GET /index?expand=app.version,app.player Accept: application/json 响应
{
"app": {
"name": "example app",
"version": "1.0",
"player": {
"name": "root",
"title": "...",
"description": "...",
"expand": ["player.aliases", "player.identities", "player.settings"]
},
"expand": ["app.version", "app.player"]
}
}
GET /index?expand=app.version,app.player,player.aliases Accept: application/json 响应
{
"app": {
"name": "example app",
"version": "1.0",
"player": {
"name": "root",
"title": "...",
"description": "...",
"aliases": ["root", "admin", "authorized"],
"expand": ["player.aliases", "player.identities", "player.settings"]
},
"expand": ["app.version", "app.player"]
}
}
应用扩展
use extas\components\Item; use extas\components\expands\Expand; /** * @var Psr\Http\Message\RequestInterface $request * @var Psr\Http\Message\ResponseInterface $response */ $app = new class([ 'name' => 'example app' ]) extends Item { protected function getSubjectForExtension() : string{ return 'app'; } }; $expand = new Expand([ Expand::FIELD__PSR_REQUEST => $request, Expand::FIELD__PSR_RESPONSE => $response, Expand::FIELD__ARGUMENTS => [ Expand::ARG__EXPAND => 'app.version' ] ]); $app = $expand->expand($app);
在调用 expand 方法时,将执行两个阶段
extas.expand.parse: 在这个阶段解析expand参数字符串(Expand::FIELD__ARGUMENTS)。extas.expand.app.version: 在这个阶段进行实际的展开。
在 extas 兼容的配置中
{
"plugins": [
{
"class": "\\PluginAppExpandVersion",
"stage": "extas.expand.app.version"
}
]
}
应用结果
use extas\components\Item; use extas\components\expands\Expand; /** * @var Psr\Http\Message\RequestInterface $request * @var Psr\Http\Message\ResponseInterface $response */ $app = new class([ 'name' => 'example app' ]) extends Item { protected function getSubjectForExtension() : string{ return 'app'; } }; $expand = new Expand([ Expand::FIELD__PSR_REQUEST => $request, Expand::FIELD__PSR_RESPONSE => $response, Expand::FIELD__ARGUMENTS => [ Expand::ARG__EXPAND => 'app.version' ] ]); $app = $expand->expand($app); print_r($app->__toArray());
结果大致如下
Array ( "name" => "example app" "version" => "1.0" "expand" => ["app.version", "app.player"] )
内置插件
此包提供了两个用于解析的内置插件
- 支持通配符:允许使用
app.*这样的扩展,它会展开实体中的所有内容。 - 空值检查:删除空的扩展元素
要连接这些插件,需要导入它们
extas.json
{
"import": {
"from": {
"extas/expands": {
"plugins": "*"
}
},
"parameters": {
"on_miss_package": {
"name": "on_miss_package",
"value": "continue"
},
"on_miss_section": {
"name": "on_miss_section",
"value": "throw"
}
}
}
}