[Ivan Diaz] - Create datastore abstract class

This commit is contained in:
Ivan Diaz 2015-11-02 21:55:59 -03:00
parent 36fd8a4585
commit 9b6ad74e6a
3 changed files with 57 additions and 65 deletions

View File

@ -1,10 +1,57 @@
<?php
/*abstract class DataStore {
use RedBeanPHP\Facade as RedBean;
abstract class DataStore {
protected $_id;
protected $_bean;
protected function __construct($table) {
abstract protected function getDefaultProperties();
public function __construct($beanInstance = null) {
if ($beanInstance) {
$this->_bean = $beanInstance;
}
else {
$this->_bean = RedBean::dispense(static::TABLE);
$defaultProperties = $this->getDefaultProperties();
foreach ($defaultProperties as $PROP => $VALUE) {
$this->_bean[$PROP] = $VALUE;
}
}
}
protected function deleteDataStore($dataStore) {
if ($dataStore instanceof DataStore) {
RedBean::trash($dataStore->getBeanInstance());
unset($dataStore);
return true;
}
else {
return false;
}
}
protected function getBeanInstance() {
return $this->_bean;
}
public function setProperties($properties) {
foreach (static::PROPERTIES as $PROP) {
$this->_bean[$PROP] = $properties[$PROP];
}
}
public function __get($name) {
if ($this->_bean[$name]) {
return $this->_bean[$name];
}
else {
return null;
}
}
public function store() {
return RedBean::store($this->_bean);
}
}
*/

View File

@ -1,17 +0,0 @@
<?php
class Log {
public static function getLog($id) {
return RedBean::load('log', $id);
}
public static function log($properties) {
$this->log = RedBean::dispense('LOGS');
$log->date = RedBean::isoDateTime();
$log->type = $properties['type'];
$log->content = $properties['content'];
}
public function showUserDetails() {
return $this->_user;
}
}

View File

@ -1,7 +1,8 @@
<?php
use RedBeanPHP\Facade as RedBean;
class User {
class User extends DataStore {
const TABLE = 'users';
const PROPERTIES = array(
'user',
'password',
@ -11,9 +12,6 @@ class User {
'UNDEFINED_CREDENTIALS' => 'User or password is not defined'
);
private $_id;
private $_user;
public static function getUser($value, $property = 'id') {
if ($property === 'id') {
$mapValue = 'id=:value';
@ -28,49 +26,13 @@ class User {
}
public static function deleteUser($user) {
if ($user instanceof User) {
RedBean::trash($user);
unset($user);
return true;
}
else {
return false;
}
parent::deleteDataStore($user);
}
public function __construct($user = null) {
if ($user) {
$this->_user = $user;
}
else {
$this->_user = RedBean::dispense('users');
$this->setDefaultProperties();
}
}
public function setDefaultProperties() {
}
public function setProperties($properties) {
foreach (self::PROPERTIES as $PROP) {
$this->_user[$PROP] = $properties[$PROP];
}
}
public function __get($name) {
if ($this->_user[$name]) {
return $this->_user[$name];
}
else {
return null;
}
}
public function store() {
return RedBean::store($this->_user);
public function getDefaultProperties() {
return [
'admin' => 0
];
}
public function showUserDetails() {