/kernel/class.entity.php

https://gitlab.com/x33n/SellCloudMusic · PHP · 344 lines · 175 code · 72 blank · 97 comment · 27 complexity · 5d244c3ba2295e1ad997da38aaede134 MD5 · raw file

  1. <?php
  2. require_once('kernel/class.transformer.php');
  3. define('ENT_NO_FLAG', 0x00);
  4. define('ENT_FLG_SAFE', 0x40);
  5. define('SLOT_FLAG', 0);
  6. define('SLOT_DATA', 1);
  7. class NTError {
  8. public $message;
  9. public $slot;
  10. public function __construct($msg, $slot = 'default') {
  11. $this->message = $msg;
  12. $this->slot = strtolower($slot);
  13. }
  14. }
  15. /**
  16. * @package ?
  17. * @version 1.0
  18. * @author Roman Bruckner
  19. * @license ?
  20. * @access ?
  21. * @php 5.1 or higher
  22. * */
  23. abstract class Entity extends Transformer implements Serializable {
  24. const STATUS_NEW = 'new';
  25. const STATUS_UPD = 'update';
  26. const STATUS_INS = 'insert';
  27. const STATUS_DEL = 'delete';
  28. const STATUS_SET = 'load';
  29. const LABEL_ID = '__IDN';
  30. const LABEL_STATUS = '__STATUS';
  31. const LABEL_ACCESS = '__ACCESS';
  32. // general metadata
  33. private $__GMData = array(
  34. self::LABEL_ID => 'id',
  35. self::LABEL_STATUS => self::STATUS_NEW
  36. );
  37. // slots metadata
  38. private $__SMData = array();
  39. //SLOT_FLAG => array(),SLOT_DATA => array());
  40. /**
  41. * deteminates whether a selected slot is an private variable.
  42. * @access private
  43. * @param slot
  44. * @return boolean
  45. * */
  46. private function is_public($var) {
  47. return @ctype_alnum(@substr($var, 0, 2));
  48. }
  49. /**
  50. * fills object vars from an assocciative array
  51. * @access public
  52. * @param an array
  53. * @return instance of this class
  54. * */
  55. public function loadArray(& $param, $postfix = '') {
  56. $vars = @get_object_vars($this);
  57. while ($key = @key($vars)) {
  58. // eliminates private variables (starting with '__'). Fixing scoping behaviour.
  59. if ($this->is_public($key) && @isset($param[$key . $postfix]) && !@empty($param[$key . $postfix])) {
  60. $this->$key = $param[$key . $postfix];
  61. }
  62. @next($vars);
  63. }
  64. $this->setStatus(self::STATUS_SET);
  65. return $this;
  66. }
  67. /**
  68. * fills object vars from an object
  69. * @access public
  70. * @param an object
  71. * @return instance of this class
  72. * */
  73. public function loadObject(& $param) {
  74. $vars = @get_object_vars($this);
  75. $inputs = @get_object_vars($param);
  76. while ($key = @key($vars)) {
  77. // eliminates private variables (starting with '__'). Fixing scoping behaviour.
  78. if ($this->is_public($key) && @property_exists($param, $key))
  79. $this->$key = $inputs[$key];
  80. @next($vars);
  81. }
  82. $this->setStatus(self::STATUS_SET);
  83. return $this;
  84. }
  85. /**
  86. * returns a selected general metadata
  87. * @access public
  88. * @param string , object
  89. * @return none
  90. * */
  91. public function setGlobalData($key, $val) {
  92. if (@isset($val)) {
  93. $this->__GMData[$key] = $val;
  94. } else {
  95. unset($this->__GMData[$key]);
  96. }
  97. }
  98. /**
  99. * sets data up in the slots metadata
  100. * @access public
  101. * @param string
  102. * @return none
  103. * */
  104. public function setSlotData($nvar, $data) {
  105. if (@property_exists($this, $nvar) && $this->is_public($nvar)) {
  106. $this->__SMData[$nvar][SLOT_DATA] = $data;
  107. }
  108. }
  109. /**
  110. * adds data into the slots metadata
  111. * @access public
  112. * @param string
  113. * @return none
  114. * */
  115. public function addSlotData($nvar, $label, $value) {
  116. if (@property_exists($this, $nvar) && $this->is_public($nvar)) {
  117. $this->__SMData[$nvar][SLOT_DATA][$label] = $value;
  118. }
  119. }
  120. /**
  121. * @access public
  122. * @param string
  123. * @return none
  124. * */
  125. public function setFlags($nvar, $flg) {
  126. if (@property_exists($this, $nvar) && $this->is_public($nvar)) {
  127. $this->__SMData[$nvar][SLOT_FLAG] = $flg;
  128. }
  129. }
  130. /**
  131. * @access public
  132. * @param string
  133. * @return none
  134. * */
  135. public function addFlags($nvar, $flg) {
  136. if (@property_exists($this, $nvar) && $this->is_public($nvar)) {
  137. $this->__SMData[$nvar][SLOT_FLAG] |= $flg;
  138. }
  139. }
  140. /**
  141. * returns a selected general metadata
  142. * @access public
  143. * @param string
  144. * @return object
  145. * */
  146. public function getGlobalData($key) {
  147. if (@array_key_exists($key, $this->__GMData)) {
  148. return $this->__GMData[$key];
  149. }
  150. }
  151. /**
  152. * returns metadata of a selected slot
  153. * @access public
  154. * @param string
  155. * @return object
  156. * */
  157. public function getSlotData($key) {
  158. if (@isset($this->__SMData[$key][SLOT_DATA])) {
  159. return $this->__SMData[$key][SLOT_DATA];
  160. }
  161. return null;
  162. }
  163. /**
  164. * returns metadata of a selected slot
  165. * @access public
  166. * @param string
  167. * @return object
  168. * */
  169. public function getFlags($key) {
  170. if (@isset($this->__SMData[$key][SLOT_FLAG])) {
  171. return $this->__SMData[$key][SLOT_FLAG];
  172. }
  173. return 0;
  174. }
  175. /**
  176. * fills the id slot with param
  177. * @access public
  178. * @param string | integer
  179. * @return none
  180. * */
  181. public function setID($param) {
  182. $this->{$this->__GMData[self::LABEL_ID]} = $param;
  183. }
  184. /**
  185. * returns a value of the id slot
  186. * @access public
  187. * @param none
  188. * @return object
  189. * */
  190. public function getID() {
  191. if (@property_exists($this, $this->__GMData[self::LABEL_ID])) {
  192. return (int) $this->{$this->__GMData[self::LABEL_ID]};
  193. }
  194. return 0;
  195. }
  196. public function setStatus($param) {
  197. $this->__GMData[self::LABEL_STATUS] = $param;
  198. }
  199. public function getStatus() {
  200. return $this->__GMData[self::LABEL_STATUS];
  201. }
  202. /**
  203. * returns true whether is a choosen slot flagged with param flag
  204. * @access public
  205. * @param string,int
  206. * @return boolean
  207. * */
  208. public function isFlagged($var, $flag) {
  209. return ($this->getFlags($var) & $flag) == $flag;
  210. }
  211. //serializing
  212. public function serialize() {
  213. return serialize(get_object_vars($this));
  214. }
  215. public function unserialize($data) {
  216. foreach (unserialize($data) as $var => $value) {
  217. $this->$var = $value;
  218. }
  219. }
  220. public function clear($flag = ENT_NO_FLAG) {
  221. $vars = @get_object_vars($this);
  222. while ($key = @key($vars)) {
  223. if ($this->is_public($key) && $this->isFlagged($key, $flag)) {
  224. $this->$key = "";
  225. }
  226. @next($vars);
  227. }
  228. }
  229. public function safe() {
  230. $this->clear(ENT_FLG_SAFE);
  231. }
  232. //TODO: allow stack entities with store
  233. public function store() {
  234. $_SESSION[@get_called_class()] = serialize($this);
  235. }
  236. public static function restore() {
  237. $nt = @get_called_class();
  238. if (@isset($_SESSION[$nt])) {
  239. return unserialize($_SESSION[$nt]);
  240. } else {
  241. return (new $nt());
  242. }
  243. }
  244. public static function isStored() {
  245. return @isset($_SESSION[@get_called_class()]);
  246. }
  247. public static function clearStored() {
  248. unset($_SESSION[@get_called_class()]);
  249. }
  250. public function __toString() {
  251. return @strtolower(@get_class($this));
  252. }
  253. public function beforeInsert() {
  254. }
  255. public function afterInsert() {
  256. }
  257. public function beforeUpdate() {
  258. }
  259. public function afterUpdate() {
  260. }
  261. public function beforeLoad() {
  262. }
  263. public function afterLoad() {
  264. }
  265. public function beforeFind() {
  266. }
  267. public function afterFind() {
  268. }
  269. public function beforeDelete() {
  270. }
  271. }
  272. ?>