Skip to content

Commit

Permalink
Merge pull request #10 from Restartz/develop
Browse files Browse the repository at this point in the history
Relations and ArrayAccess
  • Loading branch information
marcreichel committed Jul 10, 2019
2 parents 8a7e30f + 1e101d2 commit 8b69c54
Showing 1 changed file with 70 additions and 3 deletions.
73 changes: 70 additions & 3 deletions src/Models/Model.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace MarcReichel\IGDBLaravel\Models;

use Error;
use ArrayAccess;
use Carbon\Carbon;
use BadMethodCallException;
use Illuminate\Support\Str;
Expand All @@ -12,7 +13,7 @@
use MarcReichel\IGDBLaravel\Traits\HasAttributes;
use MarcReichel\IGDBLaravel\Traits\HasRelationships;

class Model implements Arrayable, Jsonable
class Model implements ArrayAccess, Arrayable, Jsonable
{
use HasAttributes,
HasRelationships;
Expand Down Expand Up @@ -51,6 +52,72 @@ public function __get($field)
return $this->getAttribute($field);
}

/**
* @param $field
*
* @param $value
*/
public function __set($field, $value)
{
$this->attributes[$field] = $value;
}

/**
* @param mixed $field
*
* @return bool
*/
public function offsetExists($field)
{
return isset($this->attributes[$field]) || isset($this->relations[$field]);
}

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

/**
* @param mixed $field
*
* @return mixed
*/
public function offsetSet($field, $value)
{
$this->attributes[$field] = $value;
}

/**
* @param mixed $field
*/
public function offsetUnset($field)
{
unset($this->attributes[$field], $this->relations[$field]);
}

/**
* @param $field
*
* @return bool
*/
public function __isset($field)
{
return $this->offsetExists($field);
}

/**
* @param $field
*/
public function __unset($field)
{
$this->offsetUnset($field);
}

/**
* @param $method
* @param $parameters
Expand Down Expand Up @@ -118,10 +185,10 @@ protected function setRelations(array $attributes)
if (is_array($value)) {
return collect($value)->map(function($value) use ($key) {
return $this->mapToModel($key, $value);
})->toArray();
});
}
return $this->mapToModel($key, $value);
})->toArray();
});
}

/**
Expand Down

0 comments on commit 8b69c54

Please sign in to comment.