Last active
August 29, 2015 14:13
-
-
Save tlode/c2f0635c03f2650d1eb3 to your computer and use it in GitHub Desktop.
CustomFlexibleEntity
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace SimpleEntities; | |
use PommProject\ModelManager\Model\FlexibleEntity\FlexibleEntity; | |
use PommProject\ModelManager\Model\FlexibleEntity\FlexibleContainer; | |
use PommProject\ModelManager\Model\FlexibleEntity\StatefullEntityTrait; | |
class CustomFlexibleEntity extends FlexibleContainer | |
{ | |
use StatefullEntityTrait; | |
/** | |
* Instantiate the entity and hydrate it with the given values. | |
* | |
* @param array $values Optional starting values. | |
*/ | |
public function __construct(array $values = null) | |
{ | |
if (!is_null($values)) { | |
$this->hydrate($values); | |
} | |
} | |
/** | |
* Set a value in the varholder. | |
* | |
* @param String $var Attribute name. | |
* @param Mixed $value Attribute value. | |
* @return FlexibleEntity $this | |
*/ | |
final public function set($var, $value) | |
{ | |
$this->container[$var] = $value; | |
$this->touch(); | |
return $this; | |
} | |
/** | |
* Returns the $var value | |
* | |
* @param string $var Key you want to retrieve value from. | |
* @return mixed | |
*/ | |
final public function get($var) | |
{ | |
if (!$this->has($var)) | |
return null; | |
return $this->container[$var]; | |
} | |
/** | |
* Returns true if the given key exists. | |
* | |
* @param string $var | |
* @return boolean | |
*/ | |
final public function has($var) | |
{ | |
return array_key_exists($var, $this->container); | |
} | |
/** | |
* Drop an attribute from the varholder. | |
* | |
* @param String $offset Attribute name. | |
* @return FlexibleEntity $this | |
*/ | |
final public function clear($offset) | |
{ | |
if ($this->has($offset)) { | |
unset($this->container[$offset]); | |
$this->touch(); | |
} | |
return $this; | |
} | |
/** | |
* PHP magic to get attributes. | |
* | |
* @param String $var Attribute name. | |
* @return Mixed Attribute value. | |
*/ | |
public function __get($var) | |
{ | |
return $this->get($var); | |
} | |
/** | |
* PHP magic to set attributes. | |
* | |
* @param String $var Attribute name. | |
* @param Mixed $value Attribute value. | |
* @return FlexibleEntity $this | |
*/ | |
public function __set($var, $value) | |
{ | |
return $this->set($var, $value); | |
} | |
/** | |
* PHP magic to test for attributes. | |
* | |
* @param String $var Attribute name. | |
* @return Mixed Attribute value. | |
*/ | |
public function __isset($var) | |
{ | |
return $this->has($var); | |
} | |
/** | |
* PHP magic to unset an attribute | |
* | |
* @param String $var Attribute name. | |
* @return Mixed Attribute value. | |
*/ | |
public function __unset($var) | |
{ | |
return $this->clear($var); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment