Skip to content

Commit

Permalink
Add support for loading properties in Object class
Browse files Browse the repository at this point in the history
  • Loading branch information
mahagr committed May 16, 2017
1 parent 41c130c commit c7bcbea
Showing 1 changed file with 30 additions and 1 deletion.
31 changes: 30 additions & 1 deletion system/src/Grav/Framework/Object/Object.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,10 @@
*/
class Object implements ObjectInterface
{
use ObjectTrait, ArrayAccessWithGetters, Export;
use ObjectTrait, ArrayAccessWithGetters, Export {
ArrayAccessWithGetters::offsetExists as private parentOffsetExists;
ArrayAccessWithGetters::offsetGet as private parentOffsetGet;
}

/**
* Properties of the object.
Expand Down Expand Up @@ -46,6 +49,32 @@ public function __construct(array $elements = [], $key = null)
}
}

/**
* Checks whether or not an offset exists with a possibility to load the field by $this->loadField{offset}().
*
* @param mixed $offset An offset to check for.
* @return bool Returns TRUE on success or FALSE on failure.
*/
public function offsetExists($offset)
{
return $this->parentOffsetExists($offset) || method_exists($this, "loadOffset_{$offset}");
}

/**
* Returns the value at specified offset with a possibility to load the field by $this->loadField{offset}().
*
* @param mixed $offset The offset to retrieve.
* @return mixed Can return all value types.
*/
public function offsetGet($offset)
{
if (!$this->parentOffsetExists($offset) && method_exists($this, "loadOffset_{$offset}")) {
$this->offsetSet($offset, call_user_func([$this, "loadOffset_{$offset}"]));
}

return $this->parentOffsetGet($offset);
}

/**
* Implements JsonSerializable interface.
*
Expand Down

0 comments on commit c7bcbea

Please sign in to comment.