]> git.mxchange.org Git - friendica.git/blob - src/BaseModel.php
Introduce Repository, Factory, Collection, Model base classes
[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  * The Model classes inheriting from this abstract class are meant to represent a single database record.
11  * The associated table name has to be provided in the child class, and the table is expected to have a unique `id` field.
12  *
13  * @property int id
14  */
15 abstract class BaseModel
16 {
17         protected static $table_name;
18
19         /** @var Database */
20         protected $dba;
21         /** @var LoggerInterface */
22         protected $logger;
23
24         /**
25          * Model record abstraction.
26          * Child classes never have to interact directly with it.
27          * Please use the magic getter instead.
28          *
29          * @var array
30          */
31         private $data = [];
32
33         /**
34          * @param Database        $dba
35          * @param LoggerInterface $logger
36          * @param array           $data   Table row attributes
37          */
38         public function __construct(Database $dba, LoggerInterface $logger, array $data = [])
39         {
40                 $this->dba = $dba;
41                 $this->logger = $logger;
42                 $this->data = $data;
43         }
44
45         /**
46          * Performance-improved model creation in a loop
47          *
48          * @param BaseModel $prototype
49          * @param array     $data
50          * @return BaseModel
51          */
52         public static function createFromPrototype(BaseModel $prototype, array $data)
53         {
54                 $model = clone $prototype;
55                 $model->data = $data;
56
57                 return $model;
58         }
59
60         /**
61          * Magic getter. This allows to retrieve model fields with the following syntax:
62          * - $model->field (outside of class)
63          * - $this->field (inside of class)
64          *
65          * @param $name
66          * @return mixed
67          * @throws HTTPException\InternalServerErrorException
68          */
69         public function __get($name)
70         {
71                 if (empty($this->data['id'])) {
72                         throw new HTTPException\InternalServerErrorException(static::class . ' record uninitialized');
73                 }
74
75                 if (!array_key_exists($name, $this->data)) {
76                         throw new HTTPException\InternalServerErrorException('Field ' . $name . ' not found in ' . static::class);
77                 }
78
79                 return $this->data[$name];
80         }
81
82         /**
83          * @param string $name
84          * @param mixed $value
85          */
86         public function __set($name, $value)
87         {
88                 $this->data[$name] = $value;
89         }
90
91         public function toArray()
92         {
93                 return $this->data;
94         }
95 }