[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; $pass = $userInstance->password;
} }
else { else {
return;
Response::respondError(ERRORS::INVALID_CREDENTIALS); Response::respondError(ERRORS::INVALID_CREDENTIALS);
} }

View File

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