zmaglica / rick-and-morty-api-wrapper
带有查询构建器的Rick and Morty API包装器
Requires
- php: ^7.0
- ext-json: *
- guzzlehttp/guzzle: ^6.3
Requires (Dev)
- friendsofphp/php-cs-fixer: ^2.15
- phpunit/phpunit: ^7.0
This package is auto-updated.
Last update: 2024-09-17 20:22:53 UTC
README
这是一个PHP包,它是https://rickandmortyapi.com/的包装器,带有类似Doctrine和Laravel数据库查询构建器的查询构建器。它包含诸如where子句、简单分页、结果预加载等流行功能。
安装
您可以通过composer安装此包
composer require zmaglica/rick-and-morty-api-wrapper
用法
默认查询函数,可用于所有API端点
where() // Add custom filtering. where(['name' => 'Rick']) clear() // Clear all filters (where clauses) whereId() // Add character, location or episode ID to where clause whereId([1,2,3]) setPage() // Specify request page. Example: setPage(2) nextPage() // Set next page previousPage() // Set previous page raw() // Execute raw URI to the Rick and Morty API without any filters and pages
默认函数,可以在执行请求后使用
toArray() // Get results as array toJson() // Get results as json isFirstPage() // Check if request result page is first page isLastPage() //Check if request result page is last page count() // Get total number of records pages() // Get total number of pages prev() // Send request to fetch data from previous page next() // Send request to fetch data from next page first() // Send request to fetch data from first page goToPage(int $page) // Send request to fetch data from desired page last() // Send request to fetch data from last page
创建API包装器的实例,如下所示
$api = new RickAndMortyApiWrapper()
您可以通过调用setClient()
方法设置自己的GuzzleHTTP客户端。您还可以将数组传递给类构造函数以添加自定义Guzzle HTTP配置选项
角色
角色API文档可在此处找到:https://rickandmortyapi.com/documentation/#character
首先,创建API包装器的实例
$api = new RickAndMortyApiWrapper()
然后调用方法character()
,其中可以执行角色模式API调用
$characterApi = $api->character(); // Or you can directly call new RickAndMortyApiWrapper()->character()
然后您可以执行Rick and Morty API调用。
示例
获取所有角色
$characterApi->all();
获取单个角色
$characterApi->get(1);
获取多个角色
$characterApi->get([1,2,3]);
获取角色起源位置
$characterApi->getOrigin(1);
获取角色最后已知位置
$characterApi->getLocation(1);
向请求添加“状态”过滤器
$characterApi->isAlive() // fetch alive characters $characterApi->isDead() // fetch dead characters $characterApi->isStatusUnknown() // fetch characters with unknown status
向请求添加“性别”过滤器
$characterApi->isFemale() // fetch female characters $characterApi->isMale() // fetch male characters $characterApi->isGenderless() // fetch genderless characters $characterApi->isGenderUnknown() // fetch unknown gender characters
使用内置的where功能运行自定义查询参数
$characterApi->where(['status' => 'alive', 'gender' => 'female']) //Get all female characters that are alive.
可以使用此方法实现相同的查询
$characterApi->isAlive()->isFemale()
可以通过使用where子句中的可用过滤器(whereFilterName)来实现自定义过滤
$characterApi->whereName('Rick') // filter by the given name. $characterApi->whereStatus('alive') // filter by the given status (alive, dead or unknown). $characterApi->whereType('Korblock') // filter by the given type. $characterApi->whereGender('female') // filter by the given gender (female, male, genderless or unknown).
执行API调用后,您可以使用这些方法从找到的角色中获取位置和剧集
$characterApi->all()->locations() // Get instace of Location API from founded characters. Pass false to constructor if you want to remove duplicates $characterApi->all()->getLocations() // Get all location from founded characters. Pass false to constructor if you want to remove duplicates $characterApi->all()->origins() // Get instace of origin Location API from founded characters. Pass false to constructor if you want to remove duplicates $characterApi->all()->getOrigins() // Get all origin location from founded characters. Pass false to constructor if you want to remove duplicates $characterApi->all()->episodes() // Get instace of Episode API from founded characters. Pass false to constructor if you want to remove duplicates $characterApi->all()->getEpisodes() // Get all episode from founded characters. Pass false to constructor if you want to remove duplicates
以下是一个获取所有女性角色剧集的示例。
$characterApi->isAlive()->isFemale->get()->getEpisodes(); // Same thing can be achieved by using following code $characterApi->whereStatus('alive')->whereGender('female')->get()->getEpisodes(); // and using this code $characterApi->where(['status' => 'alive', 'gender' => 'female'])->get()->getEpisodes();
待办事项
- 添加更多示例并更新位置和剧集API的文档
测试
vendor/bin/phpunit
变更日志
请参阅CHANGELOG以获取有关最近更改的更多信息。
贡献
请参阅CONTRIBUTING以获取详细信息。
安全
如果您发现任何与安全相关的问题,请通过电子邮件zvonimirmaglica4@gmail.com与我们联系,而不是使用问题跟踪器。
鸣谢
许可协议
MIT许可协议(MIT)。有关更多信息,请参阅许可文件。
PHP包模板
此包是使用PHP包模板生成的。