Skip to content

Commit

Permalink
Merge pull request #2 from zmaglica/1.0.2
Browse files Browse the repository at this point in the history
1.0.2
  • Loading branch information
zmaglica authored Oct 16, 2019
2 parents 6d83e21 + 7c55290 commit 6edf912
Show file tree
Hide file tree
Showing 11 changed files with 134 additions and 82 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@ composer.lock
docs
vendor
coverage
.idea
.idea
.php_cs.cache
1 change: 0 additions & 1 deletion .php_cs.cache

This file was deleted.

31 changes: 17 additions & 14 deletions src/Api/AbstractApi.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,10 @@ public function sendRequest($uri = null, $arguments = null, $async = false)
return $promise;
}

/**
* @param null $ids
* @return mixed
*/
public function get($ids = null)
{
$uri = $this->getUri($ids ?? $this->ids);
Expand All @@ -60,7 +64,7 @@ public function all()
* @param array $params
* @return $this
*/
public function where(array $params)
public function where(array $params) : self
{
if (!isset($this->arguments['query'])) {
$this->arguments['query'] = [];
Expand All @@ -78,12 +82,11 @@ public function where(array $params)
}

/**
* You can set ids and add additional filters and then execute API request
*
* You can set ids and add additional filters and then execute API requesr
* @param $ids
* @return AbstractApi
*/
public function whereId($ids)
public function whereId($ids) : self
{
return $this->where(['id' => $ids]);
}
Expand All @@ -102,7 +105,7 @@ public function raw(string $uri)
*
* @return $this
*/
public function clear()
public function clear() : self
{
$this->ids = null;
$this->arguments['query'] = [];
Expand All @@ -114,7 +117,7 @@ public function clear()
* Get arguments/filters for API
* @return array
*/
public function getArguments()
public function getArguments() : array
{
return $this->arguments;
}
Expand All @@ -123,9 +126,9 @@ public function getArguments()
* Set page for request
*
* @param int $page
* @return $this
* @return AbstractApi
*/
public function setPage(int $page)
public function setPage(int $page) : self
{
$this->page = $page;
$this->where(['page' => $page]);
Expand All @@ -135,9 +138,9 @@ public function setPage(int $page)

/**
* Increment page to simulate pagination
* @return $this
* @return AbstractApi
*/
public function nextPage()
public function nextPage() : self
{
$this->page++;
$this->where(['page' => $this->page]);
Expand All @@ -147,9 +150,9 @@ public function nextPage()

/**
* Decrement page to simulate pagination
* @return $this
* @return AbstractApi
*/
public function previousPage()
public function previousPage() : self
{
/*
* Minimum page number is 1
Expand Down Expand Up @@ -189,9 +192,9 @@ public function getUri($ids = null)
* Example: whereName('Rick') will internally call where function with argument ['name' => 'Rick']
* @param $name
* @param $arguments
* @return $this
* @return AbstractApi
*/
public function __call($name, $arguments)
public function __call($name, $arguments) : self
{
if (strpos($name, 'where') !== 0) {
throw new \BadFunctionCallException();
Expand Down
36 changes: 20 additions & 16 deletions src/Api/Character.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,9 @@ public function __construct(Client $client)
*/
public function getOrigin($id = null)
{
return $this->get($id)->getOrigins();
$origin = $this->get($id);

return $origin->hasErrors() ? $origin : $origin->getOrigins();
}

/**
Expand All @@ -41,15 +43,17 @@ public function getOrigin($id = null)
*/
public function getLocation($id = null)
{
return $this->get($id)->getLocations();
$locations = $this->get($id);

return $locations->hasErrors() ? $locations : $locations->getLocations();
}

/**
* Query to filter dead characters
*
* @return $this
* @return Character
*/
public function isDead()
public function isDead() : self
{
$this->where(['status' => 'dead']);

Expand All @@ -59,9 +63,9 @@ public function isDead()
/**
* Query to filter alive characters
*
* @return $this
* @return Character
*/
public function isAlive()
public function isAlive() : self
{
$this->where(['status' => 'alive']);

Expand All @@ -71,9 +75,9 @@ public function isAlive()
/**
* Query to filter characters with unknown status
*
* @return $this
* @return Character
*/
public function isStatusUnknown()
public function isStatusUnknown() : self
{
$this->where(['status' => 'unknown']);

Expand All @@ -83,9 +87,9 @@ public function isStatusUnknown()
/**
* Query to filter female characters
*
* @return $this
* @return Character
*/
public function isFemale()
public function isFemale() : self
{
$this->where(['gender' => 'female']);

Expand All @@ -95,9 +99,9 @@ public function isFemale()
/**
* Query to filter male characters
*
* @return $this
* @return Character
*/
public function isMale()
public function isMale() : self
{
$this->where(['gender' => 'male']);

Expand All @@ -107,9 +111,9 @@ public function isMale()
/**
* Query to filter genderless characters
*
* @return $this
* @return Character
*/
public function isGenderless()
public function isGenderless() : self
{
$this->where(['gender' => 'genderless']);

Expand All @@ -119,9 +123,9 @@ public function isGenderless()
/**
* Query to filter characters with unknown gender
*
* @return $this
* @return Character
*/
public function isGenderUnknown()
public function isGenderUnknown() : self
{
$this->where(['gender' => 'unknown']);

Expand Down
4 changes: 3 additions & 1 deletion src/Api/Episode.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ public function __construct(Client $client)
*/
public function getCharacters($id = null)
{
return $this->get($id)->getCharacters();
$episodes = $this->get($id);

return $episodes->hasErrors() ? $episodes : $episodes->getCharacters();
}
}
4 changes: 3 additions & 1 deletion src/Api/Location.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ public function __construct(Client $client)
*/
public function getResidents($id = null)
{
return $this->get($id)->getResidents();
$locations = $this->get($id);

return $locations->hasErrors() ? $locations : $locations->getResidents();
}
}
67 changes: 61 additions & 6 deletions src/Model/AbstractModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,39 @@ public function __construct($response, $parent)
$this->info = $this->data['info'] ?? null;
}

public function toArray()
/**
* Check if there was errors on request
* @return bool
*/
public function hasErrors(): bool
{
return $this->response->getStatusCode() >= 400;
}

/**
* Get response status code
*
* @return int
*/
public function getResponseStatusCode(): int
{
return $this->response->getStatusCode();
}

/**
* Get response data
* @return array
*/
public function toArray(): array
{
return $this->data;
}

public function toJson()
/**
* Get data as json
* @return string
*/
public function toJson(): string
{
return json_encode($this->data);
}
Expand All @@ -44,7 +71,7 @@ public function toJson()
* Check if is first page of response
* @return bool
*/
public function isFirstPage()
public function isFirstPage(): bool
{
if ($this->info && $this->info['prev']) {
return false;
Expand All @@ -58,7 +85,7 @@ public function isFirstPage()
*
* @return bool
*/
public function isLastPage()
public function isLastPage(): bool
{
if ($this->info && $this->info['next']) {
return false;
Expand All @@ -71,7 +98,7 @@ public function isLastPage()
* Get total number of records
* @return int
*/
public function count()
public function count(): int
{
if ($this->info) {
return $this->info['count'];
Expand All @@ -91,7 +118,7 @@ public function count()
/** Get total number of pages
* @return int
*/
public function pages()
public function pages(): int
{
return $this->info['pages'] ?? 1;
}
Expand Down Expand Up @@ -122,6 +149,10 @@ public function next()
return $this->parent->nextPage()->sendRequest();
}

/**
* Go to first page
* @return mixed
*/
public function firstPage()
{
if ($this->info) {
Expand All @@ -131,13 +162,22 @@ public function firstPage()
return null;
}

/**
* Go to desired page
* @param int $page
* @return mixed
*/
public function goToPage(int $page)
{
if ($this->info) {
return $this->parent->setPage($page)->sendRequest();
}
}

/**
* Go to last page
* @return mixed
*/
public function lastPage()
{
if ($this->info) {
Expand All @@ -146,4 +186,19 @@ public function lastPage()

return null;
}

/**
* Helper function to get ids from urls
* @param array $urls
* @return array
*/
public function parseIdsFromUrl(array $urls) : array
{
$ids = [];
foreach ($urls as $url) {
$ids[] = substr(strrchr($url, '/'), 1);
}

return $ids;
}
}
Loading

0 comments on commit 6edf912

Please sign in to comment.