Last active
August 29, 2015 13:59
-
-
Save amcsi/10989673 to your computer and use it in GitHub Desktop.
Immutable value object
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 MyProject; | |
class Person | |
{ | |
private $data; | |
/** | |
* | |
* [ | |
* 'id' => <id>, | |
* 'name' => 'name', | |
* 'insertDate' => '2014-04-17 15:53:00' | |
* ] | |
* | |
* @param array $data | |
**/ | |
public function __construct(array $data) | |
{ | |
$this->data = $data; | |
} | |
/** | |
* @return string | |
**/ | |
public function getInsertTime() | |
{ | |
return strtotime($this->data['insertDate']); | |
} | |
/** | |
* __get | |
* | |
* @param mixed string | |
* @access public | |
* @return mixed | |
*/ | |
public function __get($name) | |
{ | |
static $forbidden = array('insertDate'); | |
if (array_key_exists($name, $this->data) && !in_array($name, $forbidden)) { | |
switch ($name) { | |
case 'insertTime': | |
$ret = $this->getInsertTime(); | |
break; | |
default: | |
$ret = $this->data[$name]; | |
break; | |
} | |
} else { | |
throw new \DomainException("No such key: `$name`"); | |
} | |
return $ret; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment