diff --git a/.php_cs.cache b/.php_cs.cache deleted file mode 100644 index 46b4106..0000000 --- a/.php_cs.cache +++ /dev/null @@ -1 +0,0 @@ -{"php":"7.2.4","version":"2.15.3:v2.15.3#705490b0f282f21017d73561e9498d2b622ee34c","indent":" ","lineEnding":"\n","rules":{"blank_line_after_namespace":true,"braces":true,"class_definition":true,"elseif":true,"function_declaration":true,"indentation_type":true,"line_ending":true,"lowercase_constants":true,"lowercase_keywords":true,"method_argument_space":{"ensure_fully_multiline":false},"no_closing_tag":true,"no_spaces_after_function_name":true,"no_spaces_inside_parenthesis":true,"no_trailing_whitespace":true,"no_trailing_whitespace_in_comment":true,"single_blank_line_at_eof":true,"single_class_element_per_statement":{"elements":["property"]},"single_import_per_statement":true,"single_line_after_imports":true,"switch_case_semicolon_to_colon":true,"switch_case_space":true,"visibility_required":true,"encoding":true,"full_opening_tag":true,"array_syntax":{"syntax":"short"},"binary_operator_spaces":{"align_equals":false,"align_double_arrow":false},"cast_spaces":true,"combine_consecutive_unsets":true,"concat_space":{"spacing":"one"},"linebreak_after_opening_tag":true,"no_blank_lines_after_class_opening":true,"no_blank_lines_after_phpdoc":true,"no_extra_consecutive_blank_lines":true,"no_trailing_comma_in_singleline_array":true,"no_whitespace_in_blank_line":true,"no_spaces_around_offset":true,"no_unused_imports":true,"no_useless_else":true,"no_useless_return":true,"no_whitespace_before_comma_in_array":true,"normalize_index_brace":true,"phpdoc_indent":true,"phpdoc_to_comment":true,"phpdoc_trim":true,"single_quote":true,"ternary_to_null_coalescing":true,"trailing_comma_in_multiline_array":true,"trim_array_spaces":true,"blank_line_before_statement":true},"hashes":{"src\\Api\\AbstractApi.php":347679094,"src\\Api\\Character.php":2121936801,"src\\Api\\Location.php":3638972731,"src\\Model\\AbstractModel.php":2471819396,"src\\Model\\Character.php":1124497613,"src\\Model\\Location.php":2697623879,"src\\RickAndMortyApiWrapper.php":1955676743,"tests\\ExampleTest.php":4233137363,"src\\Api\\Episode.php":4087943247,"src\\Model\\Episode.php":186598213,"tests\\RickAndMortyApiWrapperTest.php":3270942840,"tests\\Unit\\Api\\CharacterTest.php":2196974728,"tests\\Unit\\RickAndMortyApiWrapperTest.php":1429713073,"tests\\Unit\\Api\\EpisodeTest.php":3864833709,"tests\\Unit\\Api\\LocationTest.php":2759447948}} \ No newline at end of file diff --git a/.travis.yml b/.travis.yml index d4da099..24c2d6c 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,7 +1,6 @@ language: php php: - - 7.0 - 7.1 - 7.2 - 7.3 diff --git a/CHANGELOG.md b/CHANGELOG.md index 56a0f74..ddf769e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/README.md b/README.md index 1d867fb..0cc401d 100644 --- a/README.md +++ b/README.md @@ -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 @@ -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 @@ -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). \ No newline at end of file +This package was generated using the [PHP Package Boilerplate](https://laravelpackageboilerplate.com). diff --git a/src/Model/Character.php b/src/Model/Character.php index d98a9d6..4084794 100644 --- a/src/Model/Character.php +++ b/src/Model/Character.php @@ -76,9 +76,8 @@ 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) @@ -86,11 +85,11 @@ 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) {