]> git.mxchange.org Git - friendica.git/blob - src/BaseModel.php
Merge pull request #7988 from friendica/MrPetovan-notice
[friendica.git] / src / BaseModel.php
1 <?php
2
3 namespace Friendica;
4
5 use Friendica\Database\Database;
6 use Friendica\Network\HTTPException;
7 use Psr\Log\LoggerInterface;
8
9 /**
10  * Class BaseModel
11  *
12  * The Model classes inheriting from this abstract class are meant to represent a single database record.
13  * The associated table name has to be provided in the child class, and the table is expected to have a unique `id` field.
14  *
15  * @property int id
16  */
17 abstract class BaseModel
18 {
19         protected static $table_name;
20
21         /** @var Database */
22         protected $dba;
23         /** @var LoggerInterface */
24         protected $logger;
25
26         /**
27          * Model record abstraction.
28          * Child classes never have to interact directly with it.
29          * Please use the magic getter instead.
30          *
31          * @var array
32          */
33         private $data = [];
34
35         public function __construct(Database $dba, LoggerInterface $logger)
36         {
37                 $this->dba = $dba;
38                 $this->logger = $logger;
39         }
40
41         /**
42          * Magic getter. This allows to retrieve model fields with the following syntax:
43          * - $model->field (outside of class)
44          * - $this->field (inside of class)
45          *
46          * @param $name
47          * @return mixed
48          * @throws HTTPException\InternalServerErrorException
49          */
50         public function __get($name)
51         {
52                 if (empty($this->data['id'])) {
53                         throw new HTTPException\InternalServerErrorException(static::class . ' record uninitialized');
54                 }
55
56                 if (!array_key_exists($name, $this->data)) {
57                         throw new HTTPException\InternalServerErrorException('Field ' . $name . ' not found in ' . static::class);
58                 }
59
60                 return $this->data[$name];
61         }
62
63         /**
64          * Fetches a single model record. The condition array is expected to contain a unique index (primary or otherwise).
65          *
66          * Chainable.
67          *
68          * @param array $condition
69          * @return BaseModel
70          * @throws HTTPException\NotFoundException
71          */
72         public function fetch(array $condition)
73         {
74                 $intro = $this->dba->selectFirst(static::$table_name, [], $condition);
75
76                 if (!$intro) {
77                         throw new HTTPException\NotFoundException(static::class . ' record not found.');
78                 }
79
80                 $this->data = $intro;
81
82                 return $this;
83         }
84
85         /**
86          * Deletes the model record from the database.
87          * Prevents further methods from being called by wiping the internal model data.
88          */
89         public function delete()
90         {
91                 if ($this->dba->delete(static::$table_name, ['id' => $this->id])) {
92                         $this->data = [];
93                 }
94         }
95 }