Skip to content

Commit

Permalink
Merge pull request #22 from enisz/#21-fixing-multiquery-and-prepare-q…
Browse files Browse the repository at this point in the history
…uery-builder-to-build-multiqueries

#21 fixing multiquery and prepare query builder to build multiqueries
  • Loading branch information
enisz authored Aug 19, 2021
2 parents a18a3e9 + ba0cd26 commit 1524564
Show file tree
Hide file tree
Showing 4 changed files with 173 additions and 22 deletions.
3 changes: 0 additions & 3 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
# npm packages
node_modules

# generated database file
database.json

Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ The wrapper's main purpose is to provide a simple solution to fetch data from IG

To have access to IGDB's database you have to register a Twitch Account and have your own `client_id` and `access_token`. Refer to the [Account Creation](https://api-docs.igdb.com/#account-creation) and [Authentication](https://api-docs.igdb.com/#authentication) sections of the [IGDB API Documentation](https://api-docs.igdb.com/) for details.

> Now there is a utility class called `IGDBUtils` which contains an `authenticate` method to help you generate your access token! Refer to the [documentation](#documentation) for details!
> Now there is a utility class called `IGDBUtils` which contains an `authenticate` method to help you generate your access token! Refer to the [documentation](https://enisz.github.io/igdb/documentation#authenticate) for details!
For a better understanding of the wrapper, there are several example scripts in the [documentation](#documentation).
For a better understanding of the wrapper, there are several [example scripts](https://enisz.github.io/igdb/documentation#examples) in the documentation.

## Documentation

Expand Down
30 changes: 16 additions & 14 deletions src/IGDB.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
* Fethching data from IGDB's database.
* Compatible with IGDB API v4
*
* @version 4.2.0
* @version 4.3.0
* @author Enisz Abdalla <[email protected]>
* @link https://github.com/enisz/igdb
*/
Expand Down Expand Up @@ -181,7 +181,6 @@ private function _exec_query($url, $query) {

/**
* Closes the CURL handler.
* After this method is called, the class cannot run any queries against IGDB unless you reinitialize it manually.
*/
public function curl_close() {
curl_close($this->curl_handler);
Expand Down Expand Up @@ -852,20 +851,23 @@ public function website($query, $count = false) {
*
* @link https://api-docs.igdb.com/#multi-query
*
* @param $endpoint ( string ) The endpoint to send your query to
* @param $result_name ( string ) A name for the result given by you
* @param $query ( string ) An apicalypse query string or null, if no filtering required
* @return $result ( mixed ) The result of the query
* @param $queries ( array of strings ) The queries to send to the multiquery endpoint as an array of multiquery formatted apicalypse strings.
* @return $result ( mixed ) The result of the query.
* @throws IGDBEndpointException if the response code is non successful (successful range is from 200 to 299)
* @throws IGDBInvalidParameterException If not array of strings is passed as a parameter
*/
public function mutliquery($endpoint, $result_name, $query = null) {
return $this->_exec_query(
$this->construct_url(
"multiquery",
false
),
"query $endpoint \"$result_name\" {\n" . (is_null($query) ? "" : $query) . "\n};"
);
public function multiquery($queries) {
if(gettype($queries) == "array") {
foreach($queries as $index => $query) {
if(gettype($query) != "string") {
throw new IGDBInvalidParameterException("Invalid type of parameter for multiquery! An array of strings is expected, " . gettype($query) . " passed at index " . $index . "!");
}
}
} else {
throw new IGDBInvalidParameterException("Invalid type of parameter for multiquery! An array is expected, " . gettype($query) . " passed!");
}

return $this->_exec_query($this->construct_url("multiquery", false), implode("\n\n", $queries));
}

}
Expand Down
158 changes: 155 additions & 3 deletions src/IGDBQueryBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
*
* Building apicalypse query strings
*
* @version 1.1.0
* @version 1.2.0
* @author Enisz Abdalla <[email protected]>
* @link https://github.com/enisz/igdb
*/
Expand Down Expand Up @@ -48,6 +48,21 @@ class IGDBQueryBuilder {
*/
private $_sort;

/**
* A name for the query for multiquery (multiquery only)
*/
private $_name;

/**
* The endpoint for the multiquery (multiquery only)
*/
private $_endpoint;

/**
* Count switch for the endpoint (multiquery only)
*/
private $_count;

/**
* Default value of the limit parameter
*/
Expand Down Expand Up @@ -76,6 +91,9 @@ public function reset() {
$this->_offset = $this->offset_default;
$this->_where = array();
$this->_sort = array();
$this->_name = null;
$this->_endpoint = null;
$this->_count = false;

return $this;
}
Expand Down Expand Up @@ -334,9 +352,118 @@ public function sort($sort) {
}

/**
* Building the Apicalypse query string from the configured object
* Setting the name of the query. Will be processed in case of multiquery only.
* @param $name - The name of the query.
* @return IGDBQueryBuilder
* @throws IGDBInvalidParameterException if a non-string value is passed to the method.
*/
public function name($name) {
if(gettype($name) == "string") {
$this->_name = $name;
} else {
throw new IGDBInvalidParameterException("Invalid type of parameter for name! A string is expected, " . gettype($name) . " passed!");
}

return $this;
}

/**
* Setting the endpoint for the query. Will be processed in case of multiquery only.
* @param $endpoint - The endpont to send the query against.
* @return IGDBQueryBuilder
* @throws IGDBInvalidParameterException if a non-string value is passed to the method.
*/
public function endpoint($endpoint) {
if(gettype($endpoint) == "string") {
$available_endpoints = array(
"age_rating_content_description" => "age_rating_content_descriptions",
"age_rating" => "age_ratings",
"alternative_name" => "alternative_names",
"artwork" => "artworks",
"character_mug_shot" => "character_mug_shots",
"character" => "characters",
"collection" => "collections",
"company_logo" => "company_logos",
"company_website" => "company_websites",
"company" => "companies",
"cover" => "covers",
"external_game" => "external_games",
"franchise" => "franchises",
"game_engine_logo" => "game_engine_logos",
"game_engine" => "game_engines",
"game_mode" => "game_modes",
"game_version_feature_value" => "game_version_feature_values",
"game_version_feature" => "game_version_features",
"game_version" => "game_versions",
"game_video" => "game_videos",
"game" => "games",
"genre" => "genres",
"involved_company" => "involved_companies",
"keyword" => "keywords",
"multiplayer_mode" => "multiplayer_modes",
"multiquery" => "multiquery",
"platform_family" => "platform_families",
"platform_logo" => "platform_logos",
"platform_version_company" => "platform_version_companies",
"platform_version_release_date" => "platform_version_release_dates",
"platform_version" => "platform_versions",
"platform_website" => "platform_websites",
"platform" => "platforms",
"player_perspective" => "player_perspectives",
"release_date" => "release_dates",
"screenshot" => "screenshots",
"search" => "search",
"theme" => "themes",
"website" => "websites"
);

if(array_key_exists($endpoint, $available_endpoints)) {
$this->_endpoint = $available_endpoints[$endpoint];
} else {
throw new IGDBInvalidParameterException("The passed endpoint \"$endpoint\" is invalid. Make sure to use the name of the endpoint, not it's path!");
}
} else {
throw new IGDBInvalidParameterException("Invalid type of parameter for endpoint! A string is expected, " . gettype($endpoint) . " passed!");
}

return $this;
}

/**
* Setting the query to use the count functionality. Will be processed in case of multiquery only.
* @param $count - A boolean value whether the count of records is needed.
* @return IGDBQueryBuilder
* @throws IGDBInvalidParameterException if a non-boolean value is passed to the method.
*/
public function count($count = true) {
if(gettype($count) == "boolean") {
$this->_count = $count;
} else {
throw new IGDBInvalidParameterException("Invalid type of parameter for endpoint! A boolean is expected, " . gettype($count) . " passed!");
}

return $this;
}

/**
* Building the apicalypse query from the configured object
* @param $multiquery - whether a multiquery string is required or a simple endpoint query
* @throws IGDBInavlidParameterException if a non-boolean parameter is passed to the method
* @return $query - the apicalpyse query string
*/
public function build($multiquery = false) {
if(gettype($multiquery) != "boolean") {
throw new IGDBInvalidParameterException("Invalid type of parameter for build! A boolean is expected, " . gettype($multiquery) . "passed!");
}

return $multiquery ? $this->multiquery() : $this->query();
}

/**
* Building the Apicalypse query string from the configured object for endpoint query requests
* @return $query - Apicalypse formatted query string
*/
public function build() {
private function query() {
$segments = array();

foreach(array("fields", "search", "exclude", "limit", "offset", "where", "sort") as $parameter) {
Expand Down Expand Up @@ -406,6 +533,31 @@ public function build() {
return implode(";\n", $segments) . ";";
}

/**
* Building the apicalpyse query string for multiquery requests.
* @throws IGBDInvalidParameterException if the name or endpoint properties are not set in the builder configuration
* @return $query - Apicalypse formatted multiquery query string
*/
private function multiquery() {
if($this->_name == null) {
throw new IGDBInvalidParameterException("The name parameter for the multiquery is not set!");
}

if($this->_endpoint == null) {
throw new IGDBInvalidParameterException("The endpoint parameter for the multiquery is not set!");
}

$query = "query " . $this->_endpoint . ($this->_count ? "/count" : "") . " \"" . addslashes($this->_name) . "\" {\n";

foreach(explode("\n", $this->query()) as $line) {
$query .= " $line\n";
}

$query .= "};";

return $query;
}

}

?>

0 comments on commit 1524564

Please sign in to comment.