Skip to content

Commit

Permalink
Merge pull request #8 from Andrew-Shook/master
Browse files Browse the repository at this point in the history
Add Arrayable and Jsonable interface to model.
  • Loading branch information
marcreichel committed Jul 1, 2019
2 parents a144abd + 69972d9 commit 8bad64d
Showing 1 changed file with 14 additions and 18 deletions.
32 changes: 14 additions & 18 deletions src/Models/Model.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,17 @@

namespace MarcReichel\IGDBLaravel\Models;

use Carbon\Carbon;
use Error;
use Carbon\Carbon;
use BadMethodCallException;
use Illuminate\Support\Str;
use MarcReichel\IGDBLaravel\Builder;
use Illuminate\Contracts\Support\Jsonable;
use Illuminate\Contracts\Support\Arrayable;
use MarcReichel\IGDBLaravel\Traits\HasAttributes;
use MarcReichel\IGDBLaravel\Traits\HasRelationships;

class Model
class Model implements Arrayable, Jsonable
{
use HasAttributes,
HasRelationships;
Expand Down Expand Up @@ -266,36 +268,30 @@ protected function getEndpoint()
}

/**
* Get the instance as an array.
*
* @return array
*/
public function toArray(): array
{
$attributes = collect($this->attributes);
$relations = collect($this->relations)->map(function($relation) {
if (is_array($relation)) {
return collect($relation)->map(function($single) {
return $single->toArray();
})->toArray();
if($relation instanceof Arrayable){
return $relation->toArray();
}
return $relation->toArray();
return $relation;
});
return $attributes->merge($relations)->sortKeys()->toArray();
}

/**
* Convert the object to its JSON representation.
*
* @param int $options
* @return string
*/
public function toJson(): string
public function toJson($options = 0): string
{
$attributes = collect($this->attributes);
$relations = collect($this->relations)->map(function($relation) {
if (is_array($relation)) {
return collect($relation)->map(function($single) {
return $single->toJson();
})->toJson();
}
return $relation->toJson();
});
return $attributes->merge($relations)->sortKeys()->toJson();
return collect($this->toArray())->toJson($options);
}
}

0 comments on commit 8bad64d

Please sign in to comment.