therselman 7 years ago
parent
commit
ba8f3b8014
  1. 47
      src/ORM/Entity.php

47
src/ORM/Entity.php

@ -1,22 +1,16 @@ @@ -1,22 +1,16 @@
<?php
/**
* Entity class with dynamic properties
* Properties can include callable functions, which can `lazy load` properties, or return dynamic/complex/calculated properties
*/
namespace Twister\ORM;
class Entity
abstract class Entity
{
protected $container = null;
protected $properties = null;
public $id = 0;
protected $properties;
public $id;
public function __construct(Container &$c, array $properties = null)
function __construct(array $properties = null)
{
$this->properties =& $properties;
$this->id = $properties['id'] ?? null;
}
/**
@ -25,10 +19,12 @@ class Entity @@ -25,10 +19,12 @@ class Entity
* @param string $name
* @return mixed
*/
public function __get($name)
function __get($name)
{
$value = $this->properties[$name];
return is_callable($value) ? $value($this) : $value;
if ( ! isset($this->properties[$name]))
$this->load($name);
return $this->properties[$name];
}
/**
@ -38,34 +34,19 @@ class Entity @@ -38,34 +34,19 @@ class Entity
* @param mixed $value
* @return void
*/
public function __set($name, $value)
function __set($name, $value)
{
$this->properties[$name] = $value;
}
public function __isset($name)
function __isset($name)
{
return isset($this->properties[$name]);
}
public function __unset($name)
function __unset($name)
{
unset($this->properties[$name]);
}
public function __call($method, ...$args)
{
array_unshift($args, $this);
return call_user_func($this->properties[$method], ...$args);
}
public function __invoke()
{
return $this->properties['__invoke']($this);
}
public function setMethod($method, callable $callable)
{
$this->properties[$method] = $callable;
return $this;
}
abstract function load(...$args);
}

Loading…
Cancel
Save