2014-04-19 16:56:16 +04:00
|
|
|
<?php
|
|
|
|
/**
|
2016-07-21 18:07:57 +03:00
|
|
|
* @copyright Copyright (c) 2016, ownCloud, Inc.
|
|
|
|
*
|
2015-03-26 13:44:34 +03:00
|
|
|
* @author Bernhard Posselt <dev@bernhard-posselt.com>
|
2019-12-03 21:57:53 +03:00
|
|
|
* @author Christoph Wurst <christoph@winzerhof-wurst.at>
|
|
|
|
* @author Daniel Kesselberg <mail@danielkesselberg.de>
|
2015-03-26 13:44:34 +03:00
|
|
|
* @author Morris Jobke <hey@morrisjobke.de>
|
|
|
|
*
|
|
|
|
* @license AGPL-3.0
|
|
|
|
*
|
|
|
|
* This code is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Affero General Public License, version 3,
|
|
|
|
* as published by the Free Software Foundation.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU Affero General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Affero General Public License, version 3,
|
2019-12-03 21:57:53 +03:00
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>
|
2015-03-26 13:44:34 +03:00
|
|
|
*
|
|
|
|
*/
|
2015-02-26 13:37:37 +03:00
|
|
|
|
2014-04-19 16:56:16 +04:00
|
|
|
namespace OCP\AppFramework\Db;
|
|
|
|
|
2019-10-22 15:54:21 +03:00
|
|
|
use function lcfirst;
|
|
|
|
use function substr;
|
|
|
|
|
2014-04-19 17:25:36 +04:00
|
|
|
/**
|
|
|
|
* @method integer getId()
|
|
|
|
* @method void setId(integer $id)
|
2015-04-16 18:00:08 +03:00
|
|
|
* @since 7.0.0
|
2014-04-19 17:25:36 +04:00
|
|
|
*/
|
2014-04-19 16:56:16 +04:00
|
|
|
abstract class Entity {
|
|
|
|
public $id;
|
|
|
|
|
2020-03-26 11:30:18 +03:00
|
|
|
private $_updatedFields = [];
|
|
|
|
private $_fieldTypes = ['id' => 'integer'];
|
2014-04-19 16:56:16 +04:00
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Simple alternative constructor for building entities from a request
|
|
|
|
* @param array $params the array which was obtained via $this->params('key')
|
|
|
|
* in the controller
|
|
|
|
* @return Entity
|
2015-04-16 18:00:08 +03:00
|
|
|
* @since 7.0.0
|
2014-04-19 16:56:16 +04:00
|
|
|
*/
|
|
|
|
public static function fromParams(array $params) {
|
|
|
|
$instance = new static();
|
|
|
|
|
2020-04-10 15:19:56 +03:00
|
|
|
foreach ($params as $key => $value) {
|
2014-04-19 16:56:16 +04:00
|
|
|
$method = 'set' . ucfirst($key);
|
|
|
|
$instance->$method($value);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $instance;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Maps the keys of the row array to the attributes
|
|
|
|
* @param array $row the row to map onto the entity
|
2015-04-16 18:00:08 +03:00
|
|
|
* @since 7.0.0
|
2014-04-19 16:56:16 +04:00
|
|
|
*/
|
2020-04-09 14:53:40 +03:00
|
|
|
public static function fromRow(array $row) {
|
2014-04-19 16:56:16 +04:00
|
|
|
$instance = new static();
|
|
|
|
|
2020-04-10 15:19:56 +03:00
|
|
|
foreach ($row as $key => $value) {
|
2014-04-19 16:56:16 +04:00
|
|
|
$prop = ucfirst($instance->columnToProperty($key));
|
|
|
|
$setter = 'set' . $prop;
|
|
|
|
$instance->$setter($value);
|
|
|
|
}
|
|
|
|
|
|
|
|
$instance->resetUpdatedFields();
|
|
|
|
|
|
|
|
return $instance;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2015-04-16 18:00:08 +03:00
|
|
|
* @return array with attribute and type
|
|
|
|
* @since 7.0.0
|
2014-04-19 16:56:16 +04:00
|
|
|
*/
|
|
|
|
public function getFieldTypes() {
|
|
|
|
return $this->_fieldTypes;
|
|
|
|
}
|
|
|
|
|
2019-11-14 21:57:43 +03:00
|
|
|
|
2014-04-19 16:56:16 +04:00
|
|
|
/**
|
|
|
|
* Marks the entity as clean needed for setting the id after the insertion
|
2015-04-16 18:00:08 +03:00
|
|
|
* @since 7.0.0
|
2014-04-19 16:56:16 +04:00
|
|
|
*/
|
2020-04-09 14:53:40 +03:00
|
|
|
public function resetUpdatedFields() {
|
2020-03-26 11:30:18 +03:00
|
|
|
$this->_updatedFields = [];
|
2014-04-19 16:56:16 +04:00
|
|
|
}
|
|
|
|
|
2015-06-19 11:51:36 +03:00
|
|
|
/**
|
|
|
|
* Generic setter for properties
|
|
|
|
* @since 7.0.0
|
|
|
|
*/
|
2014-04-19 16:56:16 +04:00
|
|
|
protected function setter($name, $args) {
|
|
|
|
// setters should only work for existing attributes
|
2020-04-10 15:19:56 +03:00
|
|
|
if (property_exists($this, $name)) {
|
|
|
|
if ($this->$name === $args[0]) {
|
2014-04-23 15:43:17 +04:00
|
|
|
return;
|
|
|
|
}
|
2014-04-19 16:56:16 +04:00
|
|
|
$this->markFieldUpdated($name);
|
|
|
|
|
|
|
|
// if type definition exists, cast to correct type
|
2020-04-10 15:19:56 +03:00
|
|
|
if ($args[0] !== null && array_key_exists($name, $this->_fieldTypes)) {
|
2020-08-28 15:30:33 +03:00
|
|
|
$type = $this->_fieldTypes[$name];
|
|
|
|
if ($type === 'blob') {
|
|
|
|
// (B)LOB is treated as string when we read from the DB
|
|
|
|
$type = 'string';
|
|
|
|
}
|
|
|
|
settype($args[0], $type);
|
2014-04-19 16:56:16 +04:00
|
|
|
}
|
|
|
|
$this->$name = $args[0];
|
|
|
|
} else {
|
2019-11-14 21:57:43 +03:00
|
|
|
throw new \BadFunctionCallException($name .
|
2014-04-19 16:56:16 +04:00
|
|
|
' is not a valid attribute');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-06-19 11:51:36 +03:00
|
|
|
/**
|
|
|
|
* Generic getter for properties
|
|
|
|
* @since 7.0.0
|
|
|
|
*/
|
2014-04-19 16:56:16 +04:00
|
|
|
protected function getter($name) {
|
|
|
|
// getters should only work for existing attributes
|
2020-04-10 15:19:56 +03:00
|
|
|
if (property_exists($this, $name)) {
|
2014-04-19 16:56:16 +04:00
|
|
|
return $this->$name;
|
|
|
|
} else {
|
2019-11-14 21:57:43 +03:00
|
|
|
throw new \BadFunctionCallException($name .
|
2014-04-19 16:56:16 +04:00
|
|
|
' is not a valid attribute');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Each time a setter is called, push the part after set
|
2019-11-14 21:57:43 +03:00
|
|
|
* into an array: for instance setId will save Id in the
|
2014-04-19 16:56:16 +04:00
|
|
|
* updated fields array so it can be easily used to create the
|
|
|
|
* getter method
|
2015-04-16 18:00:08 +03:00
|
|
|
* @since 7.0.0
|
2014-04-19 16:56:16 +04:00
|
|
|
*/
|
2019-10-22 15:54:21 +03:00
|
|
|
public function __call($methodName, $args) {
|
|
|
|
if (strpos($methodName, 'set') === 0) {
|
|
|
|
$this->setter(lcfirst(substr($methodName, 3)), $args);
|
|
|
|
} elseif (strpos($methodName, 'get') === 0) {
|
|
|
|
return $this->getter(lcfirst(substr($methodName, 3)));
|
2019-11-14 21:57:43 +03:00
|
|
|
} elseif ($this->isGetterForBoolProperty($methodName)) {
|
2019-10-22 15:54:21 +03:00
|
|
|
return $this->getter(lcfirst(substr($methodName, 2)));
|
2014-04-19 16:56:16 +04:00
|
|
|
} else {
|
2019-10-22 15:54:21 +03:00
|
|
|
throw new \BadFunctionCallException($methodName .
|
|
|
|
' does not exist');
|
2014-04-19 16:56:16 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-14 21:57:43 +03:00
|
|
|
/**
|
|
|
|
* @param string $methodName
|
|
|
|
* @return bool
|
|
|
|
* @since 18.0.0
|
|
|
|
*/
|
|
|
|
protected function isGetterForBoolProperty(string $methodName): bool {
|
|
|
|
if (strpos($methodName, 'is') === 0) {
|
|
|
|
$fieldName = lcfirst(substr($methodName, 2));
|
|
|
|
return isset($this->_fieldTypes[$fieldName]) && strpos($this->_fieldTypes[$fieldName], 'bool') === 0;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
2014-04-19 16:56:16 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Mark am attribute as updated
|
|
|
|
* @param string $attribute the name of the attribute
|
2015-06-19 11:51:36 +03:00
|
|
|
* @since 7.0.0
|
2014-04-19 16:56:16 +04:00
|
|
|
*/
|
2020-04-09 14:53:40 +03:00
|
|
|
protected function markFieldUpdated($attribute) {
|
2014-04-19 16:56:16 +04:00
|
|
|
$this->_updatedFields[$attribute] = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2019-11-14 21:57:43 +03:00
|
|
|
* Transform a database columnname to a property
|
2014-04-19 16:56:16 +04:00
|
|
|
* @param string $columnName the name of the column
|
|
|
|
* @return string the property name
|
2015-04-16 18:00:08 +03:00
|
|
|
* @since 7.0.0
|
2014-04-19 16:56:16 +04:00
|
|
|
*/
|
2020-04-09 14:53:40 +03:00
|
|
|
public function columnToProperty($columnName) {
|
2014-04-19 16:56:16 +04:00
|
|
|
$parts = explode('_', $columnName);
|
|
|
|
$property = null;
|
|
|
|
|
2020-04-10 15:19:56 +03:00
|
|
|
foreach ($parts as $part) {
|
|
|
|
if ($property === null) {
|
2014-04-19 16:56:16 +04:00
|
|
|
$property = $part;
|
|
|
|
} else {
|
|
|
|
$property .= ucfirst($part);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $property;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Transform a property to a database column name
|
|
|
|
* @param string $property the name of the property
|
|
|
|
* @return string the column name
|
2015-04-16 18:00:08 +03:00
|
|
|
* @since 7.0.0
|
2014-04-19 16:56:16 +04:00
|
|
|
*/
|
2020-04-09 14:53:40 +03:00
|
|
|
public function propertyToColumn($property) {
|
2014-04-19 16:56:16 +04:00
|
|
|
$parts = preg_split('/(?=[A-Z])/', $property);
|
|
|
|
$column = null;
|
|
|
|
|
2020-04-10 15:19:56 +03:00
|
|
|
foreach ($parts as $part) {
|
|
|
|
if ($column === null) {
|
2014-04-19 16:56:16 +04:00
|
|
|
$column = $part;
|
|
|
|
} else {
|
|
|
|
$column .= '_' . lcfirst($part);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $column;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return array array of updated fields for update query
|
2015-04-16 18:00:08 +03:00
|
|
|
* @since 7.0.0
|
2014-04-19 16:56:16 +04:00
|
|
|
*/
|
2020-04-09 14:53:40 +03:00
|
|
|
public function getUpdatedFields() {
|
2014-04-19 16:56:16 +04:00
|
|
|
return $this->_updatedFields;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Adds type information for a field so that its automatically casted to
|
|
|
|
* that value once its being returned from the database
|
|
|
|
* @param string $fieldName the name of the attribute
|
|
|
|
* @param string $type the type which will be used to call settype()
|
2015-06-19 11:51:36 +03:00
|
|
|
* @since 7.0.0
|
2014-04-19 16:56:16 +04:00
|
|
|
*/
|
2020-04-09 14:53:40 +03:00
|
|
|
protected function addType($fieldName, $type) {
|
2014-04-19 16:56:16 +04:00
|
|
|
$this->_fieldTypes[$fieldName] = $type;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Slugify the value of a given attribute
|
|
|
|
* Warning: This doesn't result in a unique value
|
|
|
|
* @param string $attributeName the name of the attribute, which value should be slugified
|
|
|
|
* @return string slugified value
|
2015-04-16 18:00:08 +03:00
|
|
|
* @since 7.0.0
|
2014-04-19 16:56:16 +04:00
|
|
|
*/
|
2020-04-09 14:53:40 +03:00
|
|
|
public function slugify($attributeName) {
|
2014-04-19 16:56:16 +04:00
|
|
|
// toSlug should only work for existing attributes
|
2020-04-10 15:19:56 +03:00
|
|
|
if (property_exists($this, $attributeName)) {
|
2014-04-19 16:56:16 +04:00
|
|
|
$value = $this->$attributeName;
|
|
|
|
// replace everything except alphanumeric with a single '-'
|
|
|
|
$value = preg_replace('/[^A-Za-z0-9]+/', '-', $value);
|
|
|
|
$value = strtolower($value);
|
|
|
|
// trim '-'
|
|
|
|
return trim($value, '-');
|
|
|
|
} else {
|
|
|
|
throw new \BadFunctionCallException($attributeName .
|
|
|
|
' is not a valid attribute');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|