]> git.mxchange.org Git - friendica.git/blob - src/BaseModel.php
f8cef0c13e707ee0d94b3971427d4271e027542a
[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, $data = [])
36         {
37                 $this->dba = $dba;
38                 $this->logger = $logger;
39                 $this->data = $data;
40         }
41
42         /**
43          * Magic getter. This allows to retrieve model fields with the following syntax:
44          * - $model->field (outside of class)
45          * - $this->field (inside of class)
46          *
47          * @param $name
48          * @return mixed
49          * @throws HTTPException\InternalServerErrorException
50          */
51         public function __get($name)
52         {
53                 if (empty($this->data['id'])) {
54                         throw new HTTPException\InternalServerErrorException(static::class . ' record uninitialized');
55                 }
56
57                 if (!array_key_exists($name, $this->data)) {
58                         throw new HTTPException\InternalServerErrorException('Field ' . $name . ' not found in ' . static::class);
59                 }
60
61                 return $this->data[$name];
62         }
63
64         /**
65          * Fetches a single model record. The condition array is expected to contain a unique index (primary or otherwise).
66          *
67          * Chainable.
68          *
69          * @param array $condition
70          * @return BaseModel
71          * @throws HTTPException\NotFoundException
72          */
73         public function fetch(array $condition)
74         {
75                 $data = $this->dba->selectFirst(static::$table_name, [], $condition);
76
77                 if (!$data) {
78                         throw new HTTPException\NotFoundException(static::class . ' record not found.');
79                 }
80
81                 return new static($this->dba, $this->logger, $data);
82         }
83
84         /**
85          * Deletes the model record from the database.
86          * Prevents further methods from being called by wiping the internal model data.
87          */
88         public function delete()
89         {
90                 if ($this->dba->delete(static::$table_name, ['id' => $this->id])) {
91                         $this->data = [];
92                 }
93         }
94 }