X-Git-Url: https://git.mxchange.org/?a=blobdiff_plain;f=src%2FBaseModel.php;h=1189f7f33b1187ef75e34a2e6c4d4ba890022535;hb=7b02585b978f7aef9d5c5a2c5d2b238cb674a4b7;hp=055f9c4a19a46dd07c40ad4fa32a612c1a99e807;hpb=592762be27c98bf6b626ea4a5f69971789149e80;p=friendica.git diff --git a/src/BaseModel.php b/src/BaseModel.php index 055f9c4a19..1189f7f33b 100644 --- a/src/BaseModel.php +++ b/src/BaseModel.php @@ -1,4 +1,23 @@ . + * + */ namespace Friendica; @@ -12,7 +31,7 @@ use Psr\Log\LoggerInterface; * * @property int id */ -abstract class BaseModel +abstract class BaseModel extends BaseDataTransferObject { /** @var Database */ protected $dba; @@ -28,6 +47,13 @@ abstract class BaseModel */ private $data = []; + /** + * Used to limit/avoid updates if no data was changed. + * + * @var array + */ + private $originalData = []; + /** * @param Database $dba * @param LoggerInterface $logger @@ -38,6 +64,17 @@ abstract class BaseModel $this->dba = $dba; $this->logger = $logger; $this->data = $data; + $this->originalData = $data; + } + + public function getOriginalData(): array + { + return $this->originalData; + } + + public function resetOriginalData() + { + $this->originalData = $this->data; } /** @@ -47,28 +84,39 @@ abstract class BaseModel * @param array $data * @return BaseModel */ - public static function createFromPrototype(BaseModel $prototype, array $data) + public static function createFromPrototype(BaseModel $prototype, array $data): BaseModel { $model = clone $prototype; $model->data = $data; + $model->originalData = $data; return $model; } + /** + * Magic isset method. Returns true if the field exists, either in the data property array or in any of the local properties. + * Used by array_column() on an array of objects. + * + * @param $name + * @return bool + */ + public function __isset($name): bool + { + return in_array($name, array_merge(array_keys($this->data), array_keys(get_object_vars($this)))); + } + /** * Magic getter. This allows to retrieve model fields with the following syntax: * - $model->field (outside of class) * - $this->field (inside of class) * - * @param $name + * @param string $name Name of data to fetch * @return mixed * @throws HTTPException\InternalServerErrorException */ - public function __get($name) + public function __get(string $name) { - if (empty($this->data['id'])) { - throw new HTTPException\InternalServerErrorException(static::class . ' record uninitialized'); - } + $this->checkValid(); if (!array_key_exists($name, $this->data)) { throw new HTTPException\InternalServerErrorException('Field ' . $name . ' not found in ' . static::class); @@ -78,16 +126,27 @@ abstract class BaseModel } /** + * * Magic setter. This allows to set model fields with the following syntax: + * - $model->field = $value (outside of class) + * - $this->field = $value (inside of class) + * * @param string $name - * @param mixed $value + * @param mixed $value */ - public function __set($name, $value) + public function __set(string $name, $value) { $this->data[$name] = $value; } - public function toArray() + public function toArray(): array { return $this->data; } + + protected function checkValid() + { + if (!isset($this->data['id']) || is_null($this->data['id'])) { + throw new HTTPException\InternalServerErrorException(static::class . ' record uninitialized'); + } + } }