Skip to content

Instantly share code, notes, and snippets.

@johnnietheblack
Created March 17, 2012 00:00
Show Gist options
  • Select an option

  • Save johnnietheblack/2053760 to your computer and use it in GitHub Desktop.

Select an option

Save johnnietheblack/2053760 to your computer and use it in GitHub Desktop.
Possible Entity / Mapper architecture
<?php
class AccountData
{
private
$_data = array(),
$_fields = array(
'active' => array(
'default' => false
),
'email' => array(),
'password' => array(),
'salt' => array(),
'validated' => array(
'default' => false
),
);
public function __construct(array $data=array())
{
foreach($this->_fields as $field => $meta) {
if(isset($data[$field])) {
$this->set($field,$data[$field]);
}elseif(isset($meta['default'])) {
$this->set($field,$meta['default']);
}
}
}
public function __get($field)
{
return $this->get($field);
}
public function __set($field,$val)
{
$this->set($field,$val);
}
public function get($field)
{
if(!isset($this->_fields[$field])) {
// DO EXCEPTION
return false;
}
return isset($this->_data[$field]) ? $this->_data[$field] : null;
}
public function set($field,$val)
{
if(!isset($this->_fields[$field])) {
// DO EXCEPTION
return false;
}
$this->_data[$field] = $val;
return $this;
}
}
class AccountEntity
{
private
$_data,
$_id;
public function __construct($id,AccountData $data)
{
$this->_id = $id;
$this->_data = $data;
}
public function active()
{
return $this->_data->get('active');
}
public function data()
{
return $this->_data;
}
public function email()
{
return $this->_data->get('email');
}
public function hash($str) {
return hash('sha256',$this->salt().$str);
}
public function id()
{
return $this->_id;
}
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment