2015-11-01 22:46:13 +01:00
|
|
|
<?php
|
2015-11-03 01:55:59 +01:00
|
|
|
use RedBeanPHP\Facade as RedBean;
|
|
|
|
|
|
|
|
abstract class DataStore {
|
2015-11-01 22:46:13 +01:00
|
|
|
protected $_bean;
|
|
|
|
|
2015-11-03 01:55:59 +01:00
|
|
|
abstract protected function getDefaultProperties();
|
|
|
|
|
2015-11-27 23:57:13 +01:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2015-11-03 01:55:59 +01:00
|
|
|
public function __construct($beanInstance = null) {
|
|
|
|
|
|
|
|
if ($beanInstance) {
|
|
|
|
$this->_bean = $beanInstance;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
$this->_bean = RedBean::dispense(static::TABLE);
|
|
|
|
$defaultProperties = $this->getDefaultProperties();
|
2015-11-27 23:57:13 +01:00
|
|
|
|
2015-11-03 01:55:59 +01:00
|
|
|
foreach ($defaultProperties as $PROP => $VALUE) {
|
|
|
|
$this->_bean[$PROP] = $VALUE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-11-27 23:57:13 +01:00
|
|
|
protected static function deleteDataStore($dataStore) {
|
2015-11-03 01:55:59 +01:00
|
|
|
if ($dataStore instanceof DataStore) {
|
|
|
|
RedBean::trash($dataStore->getBeanInstance());
|
|
|
|
unset($dataStore);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-11-27 23:57:13 +01:00
|
|
|
public function getBeanInstance() {
|
2015-11-03 01:55:59 +01:00
|
|
|
return $this->_bean;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function setProperties($properties) {
|
|
|
|
foreach (static::PROPERTIES as $PROP) {
|
2015-11-27 23:57:13 +01:00
|
|
|
if(array_key_exists($PROP, $properties)) {
|
|
|
|
$this->_bean[$PROP] = $properties[$PROP];
|
|
|
|
}
|
2015-11-03 01:55:59 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public function __get($name) {
|
|
|
|
if ($this->_bean[$name]) {
|
|
|
|
return $this->_bean[$name];
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
2015-11-01 22:46:13 +01:00
|
|
|
|
2015-11-03 01:55:59 +01:00
|
|
|
public function store() {
|
|
|
|
return RedBean::store($this->_bean);
|
2015-11-01 22:46:13 +01:00
|
|
|
}
|
|
|
|
}
|