1<?php 2 3//if(!defined('DOKU_INC')) die(); 4 5 6//~ abstract class BEZ_mdl_Dummy_Entity { 7 8 //~ protected $model; 9 10 //~ protected $id = NULL; 11 12 //~ public function __get($property) { 13 //~ if ($property === 'id') { 14 //~ return $this->id; 15 //~ } 16 //~ } 17 18 //~ public function get_table_singular() { 19 //~ $class = get_class($this); 20 //~ $exp = explode('_', $class); 21 //~ $singular = array_pop($exp); 22 //~ return lcfirst($singular); 23 //~ } 24 25 //~ public function get_table_name() { 26 //~ $singlar = $this->get_table_singular(); 27 //~ return $singular.'s'; 28 //~ } 29 30 //~ public function acl_of($field) { 31 //~ return $this->model->acl->check_field($this, $field); 32 //~ } 33 34 //~ public function __construct($model) { 35 //~ $this->model = $model; 36 //~ } 37//~ } 38 39namespace dokuwiki\plugin\bez\mdl; 40/* 41 * All fields are stored in object as strings. 42 * NULLs are converted to empty string. 43 * If any attribute in object === NULL -> it means that it was not initialized 44 * But we always inserts NULLs instead of empty strings. 45 * https://stackoverflow.com/questions/1267999/mysql-better-to-insert-null-or-empty-string 46 **/ 47 48use dokuwiki\plugin\bez\meta\PermissionDeniedException; 49use dokuwiki\plugin\bez\meta\ValidationException; 50 51abstract class Entity {// extends BEZ_mdl_Dummy_Entity { 52 53 /** @var Model */ 54 protected $model; 55 56 /** @var Validator */ 57 protected $validator; 58 59 abstract public static function get_columns(); 60 61 public static function get_select_columns() { 62 $class = get_called_class(); 63 return $class::get_columns(); 64 } 65 66 public function get_assoc($filter=NULL) { 67 $assoc = array(); 68 69 $columns = $this->get_select_columns(); 70 if ($filter !== NULL) { 71 $columns = array_intersect($columns, $filter); 72 } 73 74 foreach ($columns as $col) { 75 $assoc[$col] = $this->$col; 76 } 77 return $assoc; 78 } 79 80 public function get_table_name() { 81 $class = (new \ReflectionClass($this))->getShortName(); 82 return lcfirst($class); 83 } 84 85 public function __get($property) { 86 if (!property_exists($this, $property) || !in_array($property, $this->get_columns())) { 87 throw new \Exception('there is no column: "'.$property. '"" in table: "' . $this->get_table_name() . '"'); 88 } 89 90 //now only normal db columns has ACL, it should be fixed 91// if ($this->acl_of($property) < BEZ_PERMISSION_VIEW) { 92// throw new PermissionDeniedException(); 93// } 94 95 return $this->$property; 96 97 } 98 99 protected function set_property($property, $value) { 100 101 if (!in_array($property, $this->get_columns())) { 102 throw new \Exception('trying to set not existing column'); 103 } 104 105 //throws ValidationException 106 $this->validator->validate_field($property, $value); 107 108 //throws PermissionDeniedException 109 $this->model->acl->can($this, $property, BEZ_PERMISSION_CHANGE); 110 111 $this->$property = $value; 112 } 113 114 protected function set_property_array($array) { 115 foreach ($array as $k => $v) { 116 $this->set_property($k, $v); 117 } 118 } 119 120 public function set_data($post) { 121 $val_data = $this->validator->validate($post); 122 if ($val_data === false) { 123 throw new ValidationException($this->get_table_name(), $this->validator->get_errors()); 124 } 125 126 $this->set_property_array($val_data); 127 128 } 129 130 public function changable_fields($filter=NULL) { 131 $fields = $this->model->acl->check($this); 132 133 if ($filter !== NULL) { 134 $fields = array_filter($fields, function ($k) use ($filter) { 135 return in_array($k, $filter); 136 }, ARRAY_FILTER_USE_KEY); 137 } 138 139 return array_keys(array_filter($fields, function ($var) { 140 return $var >= BEZ_PERMISSION_CHANGE; 141 })); 142 } 143 144 public function acl_of($field) { 145 return $this->model->acl->check_field($this, $field); 146 } 147 148 public function can_be_null($field) { 149 $rule = $this->validator->get_rule($field); 150 $null = $rule[1]; 151 if (strtolower($null) == 'null') { 152 return true; 153 } 154 155 return false; 156 } 157 158 public function __construct($model) { 159 $this->model = $model; 160 $this->validator = new Validator($this->model); 161 } 162} 163