]> git.mxchange.org Git - friendica.git/blob - src/BaseModel.php
Remove unused code
[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          * @param Database        $dba
33          * @param LoggerInterface $logger
34          * @param array           $data   Table row attributes
35          */
36         public function __construct(Database $dba, LoggerInterface $logger, array $data = [])
37         {
38                 $this->dba = $dba;
39                 $this->logger = $logger;
40                 $this->data = $data;
41         }
42
43         /**
44          * Performance-improved model creation in a loop
45          *
46          * @param BaseModel $prototype
47          * @param array     $data
48          * @return BaseModel
49          */
50         public static function createFromPrototype(BaseModel $prototype, array $data)
51         {
52                 $model = clone $prototype;
53                 $model->data = $data;
54
55                 return $model;
56         }
57
58         /**
59          * Magic getter. This allows to retrieve model fields with the following syntax:
60          * - $model->field (outside of class)
61          * - $this->field (inside of class)
62          *
63          * @param $name
64          * @return mixed
65          * @throws HTTPException\InternalServerErrorException
66          */
67         public function __get($name)
68         {
69                 if (empty($this->data['id'])) {
70                         throw new HTTPException\InternalServerErrorException(static::class . ' record uninitialized');
71                 }
72
73                 if (!array_key_exists($name, $this->data)) {
74                         throw new HTTPException\InternalServerErrorException('Field ' . $name . ' not found in ' . static::class);
75                 }
76
77                 return $this->data[$name];
78         }
79
80         /**
81          * @param string $name
82          * @param mixed $value
83          */
84         public function __set($name, $value)
85         {
86                 $this->data[$name] = $value;
87         }
88
89         public function toArray()
90         {
91                 return $this->data;
92         }
93 }