Created
April 25, 2014 20:46
-
-
Save dongilbert/11302725 to your computer and use it in GitHub Desktop.
AbstractBaseEntity - the solution to the problem was to wrap the constructor `$this->entityData = $data` in an `if(empty($data))` block.
This file contains hidden or 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 Entities; | |
abstract class AbstractBaseEntity | |
{ | |
protected $entityData = []; | |
final public function __construct($data = []) | |
{ | |
if (is_object($data)) | |
{ | |
$data = get_object_vars($data); | |
} | |
$this->entityData = $data; | |
} | |
public function __get($name) | |
{ | |
$method = $this->getMethodName($name); | |
if (method_exists($this, $method)) | |
{ | |
return $this->$method(); | |
} | |
if (array_key_exists($name, $this->entityData)) | |
{ | |
return $this->entityData[$name]; | |
} | |
return null; | |
} | |
public function __set($name, $value) | |
{ | |
$this->entityData[$name] = $value; | |
} | |
/** | |
* Transforms a property name into a method | |
* name. Swaps snake_case to camelCase | |
* | |
* @param $name | |
*/ | |
private function getMethodName($name) | |
{ | |
return 'get' . str_replace(' ', '', ucwords(str_replace('_', ' ', $name))); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment