X-Git-Url: https://git.mxchange.org/?a=blobdiff_plain;f=src%2FBaseEntity.php;h=8e8938febc51be2e975ee26a794e3121db9a04a8;hb=d32b9d5d95a750ab049257c3289959fe88f0984f;hp=9f0cb31f8ea5c8297b34c3112dc6c9f59fe24f53;hpb=0de8e4db080b5739d77d6394eb5c2904e5d1b66f;p=friendica.git diff --git a/src/BaseEntity.php b/src/BaseEntity.php index 9f0cb31f8e..8e8938febc 100644 --- a/src/BaseEntity.php +++ b/src/BaseEntity.php @@ -1,18 +1,70 @@ . + * + */ namespace Friendica; +use Friendica\Network\HTTPException; + /** - * The API entity classes are meant as data transfer objects. As such, their member should be protected. - * Then the JsonSerializable interface ensures the protected members will be included in a JSON encode situation. + * The Entity classes directly inheriting from this abstract class are meant to represent a single business entity. + * Their properties may or may not correspond with the database fields of the table we use to represent it. + * Each model method must correspond to a business action being performed on this entity. + * Only these methods will be allowed to alter the model data. * - * Constructors are supposed to take as arguments the Friendica dependencies/model/collection/data it needs to - * populate the class members. + * To persist such a model, the associated Repository must be instantiated and the "save" method must be called + * and passed the entity as a parameter. + * + * Ideally, the constructor should only be called in the associated Factory which will instantiate entities depending + * on the provided data. + * + * Since these objects aren't meant to be using any dependency, including logging, unit tests can and must be + * written for each and all of their methods */ -abstract class BaseEntity implements \JsonSerializable +abstract class BaseEntity extends BaseDataTransferObject { - public function jsonSerialize() + /** + * @param string $name + * @return mixed + * @throws HTTPException\InternalServerErrorException + */ + public function __get(string $name) { - return get_object_vars($this); + if (!property_exists($this, $name)) { + throw new HTTPException\InternalServerErrorException('Unknown property ' . $name . ' in Entity ' . static::class); + } + + return $this->$name; + } + + /** + * @param $name + * @return bool + * @throws HTTPException\InternalServerErrorException + */ + public function __isset($name) + { + if (!property_exists($this, $name)) { + throw new HTTPException\InternalServerErrorException('Unknown property ' . $name . ' in Entity ' . static::class); + } + + return !empty($this->$name); } }