zmaglica / rick-and-morty-api-wrapper
Wrapper for The Rick and Morty API with query builder
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: 2025-05-17 21:52:22 UTC
README
This PHP package is wrapper for https://rickandmortyapi.com/ with query builder that is similar to Doctrine and Laravel database query builder. It contains popular function like where clauses, easy pagination, eager loading of results and many other things.
Installation
You can install the package via composer:
composer require zmaglica/rick-and-morty-api-wrapper
Usage
Default query function that can be use on all API endpoints
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
Default functions that can be used AFTER request is performed
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
Create instance of API wrapper like this
$api = new RickAndMortyApiWrapper()
You can set up your own GuzzleHTTP client by calling setClient()
method . Also you can pass array to class constructor to add custom Guzzle HTTP configuration options
Character
Character API documentation can be found here: https://rickandmortyapi.com/documentation/#character
First, create instance of API wrapper
$api = new RickAndMortyApiWrapper()
After that call method character()
where you can execute API calls for character schema
$characterApi = $api->character(); // Or you can directly call new RickAndMortyApiWrapper()->character()
After that you can execute The Rick and Morty API calls.
Examples:
Get all characters
$characterApi->all();
Get single character
$characterApi->get(1);
Get multiple characters
$characterApi->get([1,2,3]);
Get character origin location
$characterApi->getOrigin(1);
Get character last known location
$characterApi->getLocation(1);
Add "Status" filter to request
$characterApi->isAlive() // fetch alive characters $characterApi->isDead() // fetch dead characters $characterApi->isStatusUnknown() // fetch characters with unknown status
Add "Gender" filter to request
$characterApi->isFemale() // fetch female characters $characterApi->isMale() // fetch male characters $characterApi->isGenderless() // fetch genderless characters $characterApi->isGenderUnknown() // fetch unknown gender characters
Run custom query parameter using built-in where functionality
$characterApi->where(['status' => 'alive', 'gender' => 'female']) //Get all female characters that are alive.
Same query can be achieved by using this:
$characterApi->isAlive()->isFemale()
Custom filtering can be achieved by using available filter with where clause (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).
After you execute API call you can fetch location and episodes from founded characters using these methods:
$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
Here is the example of getting all episodes of female characters that are alive.
$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();
Todo
- Add more examples and update documentation for Location and Episode API
Testing
vendor/bin/phpunit
Changelog
Please see CHANGELOG for more information what has changed recently.
Contributing
Please see CONTRIBUTING for details.
Security
If you discover any security related issues, please email zvonimirmaglica4@gmail.com instead of using the issue tracker.
Credits
License
The MIT License (MIT). Please see License File for more information.
PHP Package Boilerplate
This package was generated using the PHP Package Boilerplate.