Skip to content

Commit

Permalink
Count Method
Browse files Browse the repository at this point in the history
toArray & toJson Methods
Bugfixes
  • Loading branch information
marcreichel committed Jan 16, 2019
1 parent dc28306 commit df4063e
Show file tree
Hide file tree
Showing 2 changed files with 135 additions and 6 deletions.
47 changes: 41 additions & 6 deletions src/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -301,7 +301,7 @@ public function where(
} elseif ($operator === 'not ilike' && is_string($value)) {
$this->whereNotLike($key, $value, false, $boolean);
} else {
$value = json_encode($value);
$value = !is_numeric($value) ? json_encode($value) : $value;
$where->push(($where->count() ? $boolean . ' ' : '') . $key . ' ' . $operator . ' ' . $value);
$this->query->put('where', $where);
}
Expand Down Expand Up @@ -1402,7 +1402,6 @@ private function castDate($date)
*/
public function get()
{
dd($this->getQuery());
if ($this->endpoint) {

$cacheKey = 'igdb_cache.' . md5($this->endpoint . $this->getQuery());
Expand Down Expand Up @@ -1466,9 +1465,7 @@ private function handleRequestException($exception)
throw new ServiceUnavailableException($message);
}
if ($exception->getCode() === Response::HTTP_INTERNAL_SERVER_ERROR) {
$message = 'IGDB is not working properly right now. '
. 'You could report this at https://www.igdb.com/feedbacks/new';
throw new ServiceException($message);
throw new ServiceException($exception->getMessage());
}
}
}
Expand Down Expand Up @@ -1520,11 +1517,49 @@ public function findOrFail(int $id)
*/
public function first()
{
$data = $this->forPage(1, 1)->get();
$data = $this->get();

return collect($data)->first();
}

/**
* @return mixed
* @throws \MarcReichel\IGDBLaravel\Exceptions\MissingEndpointException
*/
public function count()
{
if ($this->endpoint) {

$this->endpoint = str_finish($this->endpoint, '/') . 'count';

$cacheKey = 'igdb_cache.' . md5($this->endpoint . $this->getQuery());

if (!$this->cacheLifetime) {
Cache::forget($cacheKey);
}

$data = Cache::remember($cacheKey, $this->cacheLifetime,
function () {
try {
return (int)json_decode($this->client->get($this->endpoint,
[
'body' => $this->getQuery()
])->getBody())['count'];
} catch (\Exception $exception) {
$this->handleRequestException($exception);
}

return null;
});

$this->initClient();

return $data;
}

throw new MissingEndpointException('Please provide an endpoint.');
}

/**
* @return mixed
* @throws \MarcReichel\IGDBLaravel\Exceptions\MissingEndpointException
Expand Down
94 changes: 94 additions & 0 deletions src/Models/Model.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,11 @@ class Model

private static $instance;

/**
* Model constructor.
*
* @param array $properties
*/
public function __construct($properties = [])
{
$this->builder = new Builder($this);
Expand All @@ -34,21 +39,41 @@ public function __construct($properties = [])
$this->setEndpoint();
}

/**
* @param $field
*
* @return mixed
*/
public function __get($field)
{
return $this->getAttribute($field);
}

/**
* @param $method
* @param $parameters
*
* @return mixed
*/
public function __call($method, $parameters)
{
return $this->forwardCallTo($this->newQuery(), $method, $parameters);
}

/**
* @param $method
* @param $parameters
*
* @return mixed
*/
public static function __callStatic($method, $parameters)
{
return (new static)->$method(...$parameters);
}

/**
* @return mixed
*/
public static function all()
{
return (new static)->limit(config('igdb.per_page_limit', 50))
Expand All @@ -60,6 +85,9 @@ protected function setIdentifier()
$this->identifier = collect($this->attributes)->get('id');
}

/**
* @param array $attributes
*/
protected function setAttributes(array $attributes)
{
$this->attributes = collect($attributes)->filter(function ($value) {
Expand All @@ -78,6 +106,9 @@ protected function setAttributes(array $attributes)
})->toArray();
}

/**
* @param array $attributes
*/
protected function setRelations(array $attributes)
{
$this->relations = collect($attributes)
Expand All @@ -91,6 +122,13 @@ protected function setRelations(array $attributes)
})->toArray();
}

/**
* @param $object
* @param $method
* @param $parameters
*
* @return mixed
*/
public function forwardCallTo($object, $method, $parameters)
{
try {
Expand All @@ -100,11 +138,19 @@ public function forwardCallTo($object, $method, $parameters)
}
}

/**
* @return \MarcReichel\IGDBLaravel\Builder
*/
public function newQuery()
{
return new Builder($this);
}

/**
* @param $fields
*
* @return \MarcReichel\IGDBLaravel\Models\Model
*/
public function getInstance($fields)
{
if (is_null(self::$instance)) {
Expand All @@ -115,11 +161,22 @@ public function getInstance($fields)
return self::$instance;
}

/**
* @param $field
*
* @return mixed
*/
public function getAttribute($field)
{
return collect($this->attributes)->merge($this->relations)->get($field);
}

/**
* @param $property
* @param $value
*
* @return array
*/
private function mapToModel($property, $value)
{
$class = $this->getClassNameForProperty($property);
Expand Down Expand Up @@ -150,6 +207,11 @@ private function mapToModel($property, $value)
return $value;
}

/**
* @param $property
*
* @return bool|mixed|string
*/
protected function getClassNameForProperty($property)
{
if (collect($this->casts)->has($property)) {
Expand All @@ -175,6 +237,11 @@ protected function getClassNameForProperty($property)
return false;
}

/**
* @param $value
*
* @return array
*/
protected function getProperties($value)
{
return collect($value)->toArray();
Expand All @@ -190,8 +257,35 @@ protected function setEndpoint()
}
}

/**
* @return mixed
*/
protected function getEndpoint()
{
return $this->endpoint;
}

/**
* @return \Illuminate\Support\Collection
*/
private function getAttributes(): \Illuminate\Support\Collection
{
return collect($this->attributes)->merge($this->relations);
}

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

/**
* @return string
*/
public function toJson(): string
{
return $this->getAttributes()->toJson();
}
}

0 comments on commit df4063e

Please sign in to comment.