]> git.mxchange.org Git - friendica.git/blob - src/BaseModel.php
Fix birthday display and setting
[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         /** @var Database */
18         protected $dba;
19         /** @var LoggerInterface */
20         protected $logger;
21
22         /**
23          * Model record abstraction.
24          * Child classes never have to interact directly with it.
25          * Please use the magic getter instead.
26          *
27          * @var array
28          */
29         private $data = [];
30
31         /**
32          * Used to limit/avoid updates if no data was changed.
33          *
34          * @var array
35          */
36     private $originalData = [];
37
38         /**
39          * @param Database        $dba
40          * @param LoggerInterface $logger
41          * @param array           $data   Table row attributes
42          */
43         public function __construct(Database $dba, LoggerInterface $logger, array $data = [])
44         {
45                 $this->dba = $dba;
46                 $this->logger = $logger;
47                 $this->data = $data;
48                 $this->originalData = $data;
49         }
50
51         public function getOriginalData()
52         {
53                 return $this->originalData;
54         }
55
56         /**
57          * Performance-improved model creation in a loop
58          *
59          * @param BaseModel $prototype
60          * @param array     $data
61          * @return BaseModel
62          */
63         public static function createFromPrototype(BaseModel $prototype, array $data)
64         {
65                 $model = clone $prototype;
66                 $model->data = $data;
67                 $model->originalData = $data;
68
69                 return $model;
70         }
71
72         /**
73          * Magic isset method. Returns true if the field exists, either in the data prperty array or in any of the local properties.
74          * Used by array_column() on an array of objects.
75          *
76          * @param $name
77          * @return bool
78          */
79         public function __isset($name)
80         {
81                 return in_array($name, array_merge(array_keys($this->data), array_keys(get_object_vars($this))));
82         }
83
84         /**
85          * Magic getter. This allows to retrieve model fields with the following syntax:
86          * - $model->field (outside of class)
87          * - $this->field (inside of class)
88          *
89          * @param $name
90          * @return mixed
91          * @throws HTTPException\InternalServerErrorException
92          */
93         public function __get($name)
94         {
95                 $this->checkValid();
96
97                 if (!array_key_exists($name, $this->data)) {
98                         throw new HTTPException\InternalServerErrorException('Field ' . $name . ' not found in ' . static::class);
99                 }
100
101                 return $this->data[$name];
102         }
103
104         /**
105          * @param string $name
106          * @param mixed $value
107          */
108         public function __set($name, $value)
109         {
110                 $this->data[$name] = $value;
111         }
112
113         public function toArray()
114         {
115                 return $this->data;
116         }
117
118         protected function checkValid()
119         {
120                 if (empty($this->data['id'])) {
121                         throw new HTTPException\InternalServerErrorException(static::class . ' record uninitialized');
122                 }
123         }
124 }