1<?php 2 3namespace dokuwiki\plugin\bez\mdl; 4/* 5 * All fields are stored in object as strings. 6 * NULLs are converted to empty string. 7 * If any attribute in object === NULL -> it means that it was not initialized 8 * But we always inserts NULLs instead of empty strings. 9 * https://stackoverflow.com/questions/1267999/mysql-better-to-insert-null-or-empty-string 10 **/ 11 12use dokuwiki\plugin\bez\meta\PermissionDeniedException; 13use dokuwiki\plugin\bez\meta\ValidationException; 14 15abstract class Entity { 16 17 /** @var Model */ 18 protected $model; 19 20 /** @var Validator */ 21 protected $validator; 22 23 /** @var Acl */ 24 protected $acl; 25 26 abstract public static function get_columns(); 27 28 public static function get_select_columns() { 29 $class = get_called_class(); 30 return $class::get_columns(); 31 } 32 33 public function get_assoc($filter=NULL) { 34 $assoc = array(); 35 36 $columns = $this->get_select_columns(); 37 if ($filter !== NULL) { 38 $columns = array_intersect($columns, $filter); 39 } 40 41 foreach ($columns as $col) { 42 $assoc[$col] = $this->$col; 43 } 44 return $assoc; 45 } 46 47 public function get_table_name() { 48 $class = (new \ReflectionClass($this))->getShortName(); 49 return lcfirst($class); 50 } 51 52 public function __get($property) { 53 if (!property_exists($this, $property) || !in_array($property, $this->get_columns())) { 54 throw new \Exception('there is no column: "'.$property. '"" in table: "' . $this->get_table_name() . '"'); 55 } 56 57 //it slows down the execution and must be solved diffirently 58// if ($this->acl_of($property) < BEZ_PERMISSION_VIEW) { 59// throw new PermissionDeniedException(); 60// } 61 62 return $this->$property; 63 64 } 65 66 protected function set_property($property, $value) { 67 if ($this->acl_of($property) < BEZ_PERMISSION_CHANGE) { 68 throw new PermissionDeniedException("cannot change field $property"); 69 } 70 $this->$property = $value; 71 } 72 73 protected function set_property_array($array) { 74 foreach ($array as $k => $v) { 75 $this->set_property($k, $v); 76 } 77 } 78 79 public function set_data($post) { 80 $val_data = $this->validator->validate($post); 81 if ($val_data === false) { 82 throw new ValidationException($this->get_table_name(), $this->validator->get_errors()); 83 } 84 85 $this->set_property_array($val_data); 86 } 87 88 public function changable_fields($filter=NULL) { 89 $fields = $this->acl->get_list(); 90 91 if ($filter !== NULL) { 92 $fields = array_filter($fields, function ($k) use ($filter) { 93 return in_array($k, $filter); 94 }, ARRAY_FILTER_USE_KEY); 95 } 96 97 return array_keys(array_filter($fields, function ($var) { 98 return $var >= BEZ_PERMISSION_CHANGE; 99 })); 100 } 101 102 public function can_be_null($field) { 103 $rule = $this->validator->get_rule($field); 104 $null = $rule[1]; 105 if (strtolower($null) == 'null') { 106 return true; 107 } 108 109 return false; 110 } 111 112 public function __construct($model) { 113 $this->model = $model; 114 $this->validator = new Validator($this->model); 115 116 $this->acl = new Acl($this->model->get_level(), $this->get_select_columns()); 117 } 118 119 public function acl_of($field) { 120 return $this->acl->acl_of($field); 121 } 122 123} 124