]> git.mxchange.org Git - friendica.git/blob - src/BaseEntity.php
Posts per author/server on the community pages (#13764)
[friendica.git] / src / BaseEntity.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2010-2023, the Friendica project
4  *
5  * @license GNU AGPL version 3 or any later version
6  *
7  * This program is free software: you can redistribute it and/or modify
8  * it under the terms of the GNU Affero General Public License as
9  * published by the Free Software Foundation, either version 3 of the
10  * License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU Affero General Public License for more details.
16  *
17  * You should have received a copy of the GNU Affero General Public License
18  * along with this program.  If not, see <https://www.gnu.org/licenses/>.
19  *
20  */
21
22 namespace Friendica;
23
24 use Friendica\Network\HTTPException;
25
26 /**
27  * The Entity classes directly inheriting from this abstract class are meant to represent a single business entity.
28  * Their properties may or may not correspond with the database fields of the table we use to represent it.
29  * Each model method must correspond to a business action being performed on this entity.
30  * Only these methods will be allowed to alter the model data.
31  *
32  * To persist such a model, the associated Repository must be instantiated and the "save" method must be called
33  * and passed the entity as a parameter.
34  *
35  * Ideally, the constructor should only be called in the associated Factory which will instantiate entities depending
36  * on the provided data.
37  *
38  * Since these objects aren't meant to be using any dependency, including logging, unit tests can and must be
39  * written for each and all of their methods
40  */
41 abstract class BaseEntity extends BaseDataTransferObject
42 {
43         /**
44          * @param string $name
45          * @return mixed
46          * @throws HTTPException\InternalServerErrorException
47          */
48         public function __get(string $name)
49         {
50                 if (!property_exists($this, $name)) {
51                         throw new HTTPException\InternalServerErrorException('Unknown property ' . $name . ' in Entity ' . static::class);
52                 }
53
54                 return $this->$name;
55         }
56
57         /**
58          * @param mixed $name
59          * @return bool
60          * @throws HTTPException\InternalServerErrorException
61          */
62         public function __isset($name): bool
63         {
64                 if (!property_exists($this, $name)) {
65                         throw new HTTPException\InternalServerErrorException('Unknown property ' . $name . ' of type ' . gettype($name) . ' in Entity ' . static::class);
66                 }
67
68                 return !empty($this->$name);
69         }
70 }