phuongtt / architect
1.0.2
2020-04-07 07:34 UTC
Requires
- laravel/framework: >=5.1
Requires (Dev)
- mockery/mockery: 0.9.*
- orchestra/testbench: ~3.1
- phpunit/phpunit: ~4.7
- satooshi/php-coveralls: dev-master@dev
README
简介
Architect 用于动态创建 API 资源关系的结构。听起来有点复杂和炫耀?
想象一下,你有一个资源 Book
和一个相关资源 Author
。
Book 1-----n Author
正常嵌入式模式
这是默认情况下使用嵌入式模式加载相关资源的方式。
{ "books":[ { "id":1, "author_id":1, "title":"How to save the world from evil", "pages":100, "author":{ "id":1, "name":"Optimus Prime" } }, { "id":2, "author_id":2, "title":"How to take over the world", "pages":100, "author":{ "id":2, "name":"Megatron" } } ] }
现在,使用 Architect,你可以通过 ids
模式和 sideloading
模式来加载相关资源
Ids 模式
仅加载相关资源的 IDs。使用 Eloquent 模型的主键属性将在路线图上。
{ "books":[ { "id":1, "author_id":1, "title":"How to save the world from evil", "pages":100, "author":1 }, { "id":2, "author_id":2, "title":"How to take over the world", "pages":100, "author":2 } ] }
Sideloading 模式
将相关资源提升到全局作用域,并使用 ID 模式解析器留下 IDs。
{ "author":[ { "id":1, "name":"Optimus Prime" }, { "id":2, "name":"Megatron" } ], "books":[ { "id":1, "author_id":1, "title":"How to save the world from evil", "pages":100, "author":1 }, { "id":2, "author_id":2, "title":"How to take over the world", "pages":100, "author":2 } ] }
用法
Architect 与普通数组(集合和资源)、Illuminate\Support\Collection
和 Illuminate\Database\Eloquent\Model
一起工作。
<?php $books = Book::with('Author')->get(); $architect = new \Optimus\Architect\Architect; $parsed = $architect->parseData($books, [ 'author' => 'sideload' // can also be embed or ids (embed is default) ], 'books');
Optimus\LaravelController 提供了一些方便的方法来定义查询参数中的 Architect 关系。
安装
composer require optimus/architect ~1.0
标准
此包符合 PSR-1、PSR-2 和 PSR-4。如果您发现不符合规范的地方,请通过拉取请求发送补丁。
测试
$ phpunit
贡献
请参阅 CONTRIBUTING 获取详细信息。
许可
MIT 许可证 (MIT)。有关更多信息,请参阅 许可证文件。