[Ivan Diaz] - Update DataStore model and User model

This commit is contained in:
Ivan Diaz 2015-11-27 19:57:13 -03:00
parent 6973d0519b
commit c1b1ce20df
3 changed files with 16 additions and 15 deletions

View File

@ -27,7 +27,6 @@ $app->group('/user', function () use ($app) {
$pass = $userInstance->password;
}
else {
return;
Response::respondError(ERRORS::INVALID_CREDENTIALS);
}

View File

@ -2,11 +2,19 @@
use RedBeanPHP\Facade as RedBean;
abstract class DataStore {
protected $_id;
protected $_bean;
abstract protected function getDefaultProperties();
public static function getDataStore($value, $property = 'id') {
$bean = RedBean::findOne(static::TABLE, ':property=:value', array(
':property' => $property,
':value' => $value
));
return ($bean) ? new static($bean) : null;
}
public function __construct($beanInstance = null) {
if ($beanInstance) {
@ -15,13 +23,14 @@ abstract class DataStore {
else {
$this->_bean = RedBean::dispense(static::TABLE);
$defaultProperties = $this->getDefaultProperties();
foreach ($defaultProperties as $PROP => $VALUE) {
$this->_bean[$PROP] = $VALUE;
}
}
}
protected function deleteDataStore($dataStore) {
protected static function deleteDataStore($dataStore) {
if ($dataStore instanceof DataStore) {
RedBean::trash($dataStore->getBeanInstance());
unset($dataStore);
@ -32,13 +41,15 @@ abstract class DataStore {
}
}
protected function getBeanInstance() {
public function getBeanInstance() {
return $this->_bean;
}
public function setProperties($properties) {
foreach (static::PROPERTIES as $PROP) {
$this->_bean[$PROP] = $properties[$PROP];
if(array_key_exists($PROP, $properties)) {
$this->_bean[$PROP] = $properties[$PROP];
}
}
}

View File

@ -10,16 +10,7 @@ class User extends DataStore {
);
public static function getUser($value, $property = 'id') {
if ($property === 'id') {
$mapValue = 'id=:value';
}
else if ($property === 'user') {
$mapValue = 'user=:value';
}
$user = RedBean::findOne('users', $mapValue, array(':value' => $value));
return ($user) ? new User($user) : null;
return parent::getDataStore($value, $property);
}
public static function deleteUser($user) {