]> git.mxchange.org Git - friendica.git/blob - src/BaseModel.php
Merge pull request #8222 from annando/ap-gnusocial
[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 extends BaseEntity
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         public function resetOriginalData()
57         {
58                 $this->originalData = $this->data;
59         }
60
61         /**
62          * Performance-improved model creation in a loop
63          *
64          * @param BaseModel $prototype
65          * @param array     $data
66          * @return BaseModel
67          */
68         public static function createFromPrototype(BaseModel $prototype, array $data)
69         {
70                 $model = clone $prototype;
71                 $model->data = $data;
72                 $model->originalData = $data;
73
74                 return $model;
75         }
76
77         /**
78          * Magic isset method. Returns true if the field exists, either in the data prperty array or in any of the local properties.
79          * Used by array_column() on an array of objects.
80          *
81          * @param $name
82          * @return bool
83          */
84         public function __isset($name)
85         {
86                 return in_array($name, array_merge(array_keys($this->data), array_keys(get_object_vars($this))));
87         }
88
89         /**
90          * Magic getter. This allows to retrieve model fields with the following syntax:
91          * - $model->field (outside of class)
92          * - $this->field (inside of class)
93          *
94          * @param $name
95          * @return mixed
96          * @throws HTTPException\InternalServerErrorException
97          */
98         public function __get($name)
99         {
100                 $this->checkValid();
101
102                 if (!array_key_exists($name, $this->data)) {
103                         throw new HTTPException\InternalServerErrorException('Field ' . $name . ' not found in ' . static::class);
104                 }
105
106                 return $this->data[$name];
107         }
108
109         /**
110          * @param string $name
111          * @param mixed $value
112          */
113         public function __set($name, $value)
114         {
115                 $this->data[$name] = $value;
116         }
117
118         public function toArray()
119         {
120                 return $this->data;
121         }
122
123         protected function checkValid()
124         {
125                 if (empty($this->data['id'])) {
126                         throw new HTTPException\InternalServerErrorException(static::class . ' record uninitialized');
127                 }
128         }
129 }