Skip to content

Commit

Permalink
Merged with latest master
Browse files Browse the repository at this point in the history
  • Loading branch information
zmaglica committed Oct 15, 2019
2 parents 58863a3 + 6d83e21 commit 5d5550d
Show file tree
Hide file tree
Showing 5 changed files with 142 additions and 14 deletions.
1 change: 0 additions & 1 deletion .php_cs.cache

This file was deleted.

1 change: 0 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
language: php

php:
- 7.0
- 7.1
- 7.2
- 7.3
Expand Down
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@

All notable changes to `rick-and-morty-api-wrapper` will be documented in this file

## 1.0.0 - 201X-XX-XX
## 1.0.0

- initial release
141 changes: 136 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
# Very short description of the package
# The Rick and Morty API wrapper with query builder

[![Latest Version on Packagist](https://img.shields.io/packagist/v/zmaglica/rick-and-morty-api-wrapper.svg?style=flat-square)](https://packagist.org/packages/zmaglica/rick-and-morty-api-wrapper)
[![Build Status](https://img.shields.io/travis/zmaglica/rick-and-morty-api-wrapper/master.svg?style=flat-square)](https://travis-ci.org/zmaglica/rick-and-morty-api-wrapper)
[![Quality Score](https://img.shields.io/scrutinizer/g/zmaglica/rick-and-morty-api-wrapper.svg?style=flat-square)](https://scrutinizer-ci.com/g/zmaglica/rick-and-morty-api-wrapper)
[![Total Downloads](https://img.shields.io/packagist/dt/zmaglica/rick-and-morty-api-wrapper.svg?style=flat-square)](https://packagist.org/packages/zmaglica/rick-and-morty-api-wrapper)

This is where your description should go. Try and limit it to a paragraph or two, and maybe throw in a mention of what PSRs you support to avoid any confusion with users and contributors.
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

Expand All @@ -17,14 +17,145 @@ composer require zmaglica/rick-and-morty-api-wrapper

## Usage

Default query function that can be use on all API endpoints

``` php
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

``` php
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

``` php
$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

``` php
$api = new RickAndMortyApiWrapper()
```

After that call method `character()` where you can execute API calls for character schema

``` php
$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

``` php
$characterApi->all();
```
Get single character

``` php
// Usage description here
$characterApi->get(1);
```
Get multiple characters

``` php
$characterApi->get([1,2,3]);
```
Get character origin location

``` php
$characterApi->getOrigin(1);
```
Get character last known location

``` php
$characterApi->getLocation(1);
```

Add "Status" filter to request
``` php
$characterApi->isAlive() // fetch alive characters
$characterApi->isDead() // fetch dead characters
$characterApi->isStatusUnknown() // fetch characters with unknown status
```

Add "Gender" filter to request
``` php
$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
``` php
$characterApi->where(['status' => 'alive', 'gender' => 'female']) //Get all female characters that are alive.
```
Same query can be achieved by using this:
``` php
$characterApi->isAlive()->isFemale()
```
Custom filtering can be achieved by using available filter with where clause (whereFilterName)
``` php
$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:

``` php
$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.

``` php
$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

``` bash
composer test
vendor/bin/phpunit
```

### Changelog
Expand All @@ -50,4 +181,4 @@ The MIT License (MIT). Please see [License File](LICENSE.md) for more informatio

## PHP Package Boilerplate

This package was generated using the [PHP Package Boilerplate](https://laravelpackageboilerplate.com).
This package was generated using the [PHP Package Boilerplate](https://laravelpackageboilerplate.com).
11 changes: 5 additions & 6 deletions src/Model/Character.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,21 +76,20 @@ public function getEpisodes($removeDuplicates = false)
}

/**
* This helper function will extract all URL ids by key. Key can be location, residence or episode
* @param $key
* @param $removeDuplicates
* @param string $key
* @param boolean $removeDuplicates
* @return array
*/
private function extractIdsFromResponseBasedOnKey($key, $removeDuplicates)
{
$ids = [];
$urls = [];
if ($this->info) {
$urls = array_column($this->data['results'], 'location');
$urls = array_column($this->data['results'], $key);
} elseif (isset($this->data[0])) {
$urls = array_column($this->data, 'location');
$urls = array_column($this->data, $key);
} else {
$urls[] = $this->data['location'];
$urls[] = $this->data[$key];
}
$urls = array_column($urls, 'url');
if ($removeDuplicates) {
Expand Down

0 comments on commit 5d5550d

Please sign in to comment.