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;
|
|
|
|
|
2016-07-26 23:30:51 +02:00
|
|
|
public static function isTableEmpty() {
|
|
|
|
return (RedBean::count(static::TABLE) === 0);
|
2016-07-15 00:06:47 +02:00
|
|
|
}
|
2015-11-03 01:55:59 +01:00
|
|
|
|
2015-11-27 23:57:13 +01:00
|
|
|
public static function getDataStore($value, $property = 'id') {
|
2016-01-15 03:45:22 +01:00
|
|
|
$bean = RedBean::findOne(static::TABLE, static::validateProp($property) . ' =:value', array(
|
2015-11-27 23:57:13 +01:00
|
|
|
':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);
|
2016-05-15 00:08:30 +02:00
|
|
|
$defaultProperties = $this->getDefaultProps();
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-26 23:30:51 +02:00
|
|
|
public function getDefaultProps() {
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
|
2016-05-15 00:08:30 +02:00
|
|
|
public function delete() {
|
|
|
|
RedBean::trash($this->getBeanInstance());
|
|
|
|
unset($this);
|
2015-11-03 01:55:59 +01:00
|
|
|
}
|
|
|
|
|
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) {
|
2016-01-15 03:45:22 +01:00
|
|
|
foreach (static::getProps() 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
|
|
|
}
|
2016-01-15 03:45:22 +01:00
|
|
|
|
|
|
|
private static function validateProp($propToValidate) {
|
|
|
|
$validProp = false;
|
|
|
|
|
|
|
|
foreach (static::getProps() as $prop) {
|
2016-07-26 23:30:51 +02:00
|
|
|
if ($propToValidate === $prop) {
|
2016-01-15 03:45:22 +01:00
|
|
|
$validProp = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return ($validProp) ? $propToValidate : 'id';
|
|
|
|
}
|
2016-07-21 01:39:36 +02:00
|
|
|
|
|
|
|
public function trash() {
|
|
|
|
RedBean::trash($this->_bean);
|
|
|
|
}
|
2015-11-01 22:46:13 +01:00
|
|
|
}
|