Entity.php 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Bernhard Posselt <dev@bernhard-posselt.com>
  6. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  7. * @author Daniel Kesselberg <mail@danielkesselberg.de>
  8. * @author Morris Jobke <hey@morrisjobke.de>
  9. *
  10. * @license AGPL-3.0
  11. *
  12. * This code is free software: you can redistribute it and/or modify
  13. * it under the terms of the GNU Affero General Public License, version 3,
  14. * as published by the Free Software Foundation.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU Affero General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU Affero General Public License, version 3,
  22. * along with this program. If not, see <http://www.gnu.org/licenses/>
  23. *
  24. */
  25. namespace OCP\AppFramework\Db;
  26. use function lcfirst;
  27. use function substr;
  28. /**
  29. * @method integer getId()
  30. * @method void setId(integer $id)
  31. * @since 7.0.0
  32. */
  33. abstract class Entity {
  34. public $id;
  35. private $_updatedFields = array();
  36. private $_fieldTypes = array('id' => 'integer');
  37. /**
  38. * Simple alternative constructor for building entities from a request
  39. * @param array $params the array which was obtained via $this->params('key')
  40. * in the controller
  41. * @return Entity
  42. * @since 7.0.0
  43. */
  44. public static function fromParams(array $params) {
  45. $instance = new static();
  46. foreach($params as $key => $value) {
  47. $method = 'set' . ucfirst($key);
  48. $instance->$method($value);
  49. }
  50. return $instance;
  51. }
  52. /**
  53. * Maps the keys of the row array to the attributes
  54. * @param array $row the row to map onto the entity
  55. * @since 7.0.0
  56. */
  57. public static function fromRow(array $row){
  58. $instance = new static();
  59. foreach($row as $key => $value){
  60. $prop = ucfirst($instance->columnToProperty($key));
  61. $setter = 'set' . $prop;
  62. $instance->$setter($value);
  63. }
  64. $instance->resetUpdatedFields();
  65. return $instance;
  66. }
  67. /**
  68. * @return array with attribute and type
  69. * @since 7.0.0
  70. */
  71. public function getFieldTypes() {
  72. return $this->_fieldTypes;
  73. }
  74. /**
  75. * Marks the entity as clean needed for setting the id after the insertion
  76. * @since 7.0.0
  77. */
  78. public function resetUpdatedFields(){
  79. $this->_updatedFields = array();
  80. }
  81. /**
  82. * Generic setter for properties
  83. * @since 7.0.0
  84. */
  85. protected function setter($name, $args) {
  86. // setters should only work for existing attributes
  87. if(property_exists($this, $name)){
  88. if($this->$name === $args[0]) {
  89. return;
  90. }
  91. $this->markFieldUpdated($name);
  92. // if type definition exists, cast to correct type
  93. if($args[0] !== null && array_key_exists($name, $this->_fieldTypes)) {
  94. settype($args[0], $this->_fieldTypes[$name]);
  95. }
  96. $this->$name = $args[0];
  97. } else {
  98. throw new \BadFunctionCallException($name .
  99. ' is not a valid attribute');
  100. }
  101. }
  102. /**
  103. * Generic getter for properties
  104. * @since 7.0.0
  105. */
  106. protected function getter($name) {
  107. // getters should only work for existing attributes
  108. if(property_exists($this, $name)){
  109. return $this->$name;
  110. } else {
  111. throw new \BadFunctionCallException($name .
  112. ' is not a valid attribute');
  113. }
  114. }
  115. /**
  116. * Each time a setter is called, push the part after set
  117. * into an array: for instance setId will save Id in the
  118. * updated fields array so it can be easily used to create the
  119. * getter method
  120. * @since 7.0.0
  121. */
  122. public function __call($methodName, $args) {
  123. if (strpos($methodName, 'set') === 0) {
  124. $this->setter(lcfirst(substr($methodName, 3)), $args);
  125. } elseif (strpos($methodName, 'get') === 0) {
  126. return $this->getter(lcfirst(substr($methodName, 3)));
  127. } elseif ($this->isGetterForBoolProperty($methodName)) {
  128. return $this->getter(lcfirst(substr($methodName, 2)));
  129. } else {
  130. throw new \BadFunctionCallException($methodName .
  131. ' does not exist');
  132. }
  133. }
  134. /**
  135. * @param string $methodName
  136. * @return bool
  137. * @since 18.0.0
  138. */
  139. protected function isGetterForBoolProperty(string $methodName): bool {
  140. if (strpos($methodName, 'is') === 0) {
  141. $fieldName = lcfirst(substr($methodName, 2));
  142. return isset($this->_fieldTypes[$fieldName]) && strpos($this->_fieldTypes[$fieldName], 'bool') === 0;
  143. }
  144. return false;
  145. }
  146. /**
  147. * Mark am attribute as updated
  148. * @param string $attribute the name of the attribute
  149. * @since 7.0.0
  150. */
  151. protected function markFieldUpdated($attribute){
  152. $this->_updatedFields[$attribute] = true;
  153. }
  154. /**
  155. * Transform a database columnname to a property
  156. * @param string $columnName the name of the column
  157. * @return string the property name
  158. * @since 7.0.0
  159. */
  160. public function columnToProperty($columnName){
  161. $parts = explode('_', $columnName);
  162. $property = null;
  163. foreach($parts as $part){
  164. if($property === null){
  165. $property = $part;
  166. } else {
  167. $property .= ucfirst($part);
  168. }
  169. }
  170. return $property;
  171. }
  172. /**
  173. * Transform a property to a database column name
  174. * @param string $property the name of the property
  175. * @return string the column name
  176. * @since 7.0.0
  177. */
  178. public function propertyToColumn($property){
  179. $parts = preg_split('/(?=[A-Z])/', $property);
  180. $column = null;
  181. foreach($parts as $part){
  182. if($column === null){
  183. $column = $part;
  184. } else {
  185. $column .= '_' . lcfirst($part);
  186. }
  187. }
  188. return $column;
  189. }
  190. /**
  191. * @return array array of updated fields for update query
  192. * @since 7.0.0
  193. */
  194. public function getUpdatedFields(){
  195. return $this->_updatedFields;
  196. }
  197. /**
  198. * Adds type information for a field so that its automatically casted to
  199. * that value once its being returned from the database
  200. * @param string $fieldName the name of the attribute
  201. * @param string $type the type which will be used to call settype()
  202. * @since 7.0.0
  203. */
  204. protected function addType($fieldName, $type){
  205. $this->_fieldTypes[$fieldName] = $type;
  206. }
  207. /**
  208. * Slugify the value of a given attribute
  209. * Warning: This doesn't result in a unique value
  210. * @param string $attributeName the name of the attribute, which value should be slugified
  211. * @return string slugified value
  212. * @since 7.0.0
  213. */
  214. public function slugify($attributeName){
  215. // toSlug should only work for existing attributes
  216. if(property_exists($this, $attributeName)){
  217. $value = $this->$attributeName;
  218. // replace everything except alphanumeric with a single '-'
  219. $value = preg_replace('/[^A-Za-z0-9]+/', '-', $value);
  220. $value = strtolower($value);
  221. // trim '-'
  222. return trim($value, '-');
  223. } else {
  224. throw new \BadFunctionCallException($attributeName .
  225. ' is not a valid attribute');
  226. }
  227. }
  228. }