1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139
<?php
namespace oroboros\core\abstracts\libraries\data;
abstract class AbstractDataObject
extends \oroboros\core\abstracts\OroborosBaseAbstract
implements \oroboros\core\interfaces\api\DataObjectApi
{
const CLASS_SCOPE = \oroboros\core\interfaces\api\ClassScopeApi::CLASS_SCOPE_ABSTRACT_DATA_OBJECT;
const API = '\\oroboros\\core\\interfaces\\api\\DataApi';
private $_data;
private $_format;
private $_valid_formats = array(
'object',
'array',
'serialized',
'json'
);
private $_access_rights = array();
public function __construct(array $params = array(), array $flags = array()) {
parent::__construct($params, $flags);
$this->_init($params, $flags);
}
public function __toString() {
if ($this->_checkLock('read')) {
throw new \oroboros\core\utilities\exception\Exception("Cannot return requested key [" . $key . "] because the parent object is not readable." . PHP_EOL . '> Thrown at ' . __LINE__ . ' of ' . __METHOD__, self::ERROR_SECURITY_LOCKED_RESOURCE);
}
switch ($this->_format) {
case 'serialized':
return serialize($this->_data);
break;
case 'json':
return json_encode($this->_data);
break;
default:
return (string) $this->_data;
break;
}
}
public function initialize(array $params = array(), array $flags = array()) {
parent::initialize($params, $flags);
$this->_init($params, $flags);
}
public function get($id) {
if ($this->_checkLock('read')) {
throw new \oroboros\core\utilities\exception\Exception("Cannot return requested key [" . $id . "] because the parent object is not readable." . PHP_EOL . '> Thrown at ' . __LINE__ . ' of ' . __METHOD__, self::ERROR_SECURITY_LOCKED_RESOURCE);
}
if (!array_key_exists($id, $this->_data)) {
throw new \oroboros\core\utilities\exception\Exception("Requested key [" . $id . "] does not exist." . PHP_EOL . '> Thrown at ' . __LINE__ . ' of ' . __METHOD__, self::ERROR_LOGIC_BAD_PARAMETERS);
}
return $this->_data[$id];
}
public function set($id, $value) {
if ($this->_checkLock('write')) {
throw new \oroboros\core\utilities\exception\Exception("Cannot return requested key [" . $id . "] because the parent object is not writeable." . PHP_EOL . '> Thrown at ' . __LINE__ . ' of ' . __METHOD__, self::ERROR_SECURITY_LOCKED_RESOURCE);
}
parent::set($id, $value);
}
private function _init(array $params = array(), array $flags = array()) {
if (!empty($flags)) {
$this->_setFlags($flags);
}
$this->_setFlags($flags);
if (!empty($params)) {
$this->_setParams($params);
}
if (array_key_exists('package', $params)) {
$this->package($params['package'], ((isset($params['format'])) ? $params['format'] : array()));
}
$this->_data = $params;
}
public function package(array $data = array(), $format = 'object') {
if ($this->isInitialized() && $this->_checkLock('write')) {
throw new \oroboros\core\utilities\exception\Exception("Cannot return requested key [" . $key . "] because the parent object is not writeable." . PHP_EOL . '> Thrown at ' . __LINE__ . ' of ' . __METHOD__, self::ERROR_SECURITY_LOCKED_RESOURCE);
}
if (!in_array($format, $this->_valid_formats)) {
throw new \oroboros\core\utilities\exception\Exception("Invalid data format type: [" . (string) $format . "]. Valid data types are [" . implode(', ', $this->_valid_formats) . "]." . PHP_EOL . '> Thrown at ' . __LINE__ . ' of ' . __METHOD__, self::ERROR_LOGIC_BAD_PARAMETERS);
}
$this->_data = ((is_object($data)) ? get_object_vars($data) : $data);
}
protected function _updateFlagConditions() {
if ($this->isInitialized()) {
if ($this->_checkFlag(self::FLAG_READONLY)) {
$this->_setFlag(self::FLAG_LOCK_WRITE);
$this->_setFlag(self::FLAG_LOCK_EXECUTE);
}
if ($this->_checkFlag(self::FLAG_LOCK_READ)) {
$this->_setLock("read", TRUE);
}
if ($this->_checkFlag(self::FLAG_LOCK_WRITE)) {
$this->_setLock("write", TRUE);
}
if ($this->_checkFlag(self::FLAG_LOCK_EXECUTE)) {
$this->_setLock("execute", TRUE);
}
}
}
}