]> git.mxchange.org Git - friendica.git/blob - src/BaseDepository.php
Rename Model\Post\UserNotifications type constants
[friendica.git] / src / BaseDepository.php
1 <?php
2
3 namespace Friendica;
4
5 use Exception;
6 use Friendica\Capabilities\ICanCreateFromTableRow;
7 use Friendica\Database\Database;
8 use Friendica\Network\HTTPException\NotFoundException;
9 use Psr\Log\LoggerInterface;
10
11 /**
12  * Depositories are meant to store and retrieve Entities from the database.
13  *
14  * The reason why there are methods prefixed with an underscore is because PHP doesn't support generic polymorphism
15  * which means we can't direcly overload base methods and make parameters more strict (from a parent class to a child
16  * class for example)
17  *
18  * Similarly, we can't make an overloaded method return type more strict until we only support PHP version 7.4 but this
19  * is less pressing.
20  */
21 abstract class BaseDepository
22 {
23         const LIMIT = 30;
24
25         /**
26          * @var string This should be set to the main database table name the depository is using
27          */
28         protected static $table_name;
29
30         /** @var Database */
31         protected $db;
32
33         /** @var LoggerInterface */
34         protected $logger;
35
36         /** @var ICanCreateFromTableRow */
37         protected $factory;
38
39         public function __construct(Database $database, LoggerInterface $logger, ICanCreateFromTableRow $factory)
40         {
41                 $this->db      = $database;
42                 $this->logger  = $logger;
43                 $this->factory = $factory;
44         }
45
46
47         /**
48          * @param array $condition
49          * @param array $params
50          * @return BaseCollection
51          * @throws Exception
52          */
53         protected function _select(array $condition, array $params = []): BaseCollection
54         {
55                 $rows = $this->db->selectToArray(static::$table_name, [], $condition, $params);
56
57                 $Entities = new BaseCollection();
58                 foreach ($rows as $fields) {
59                         $Entities[] = $this->factory->createFromTableRow($fields);
60                 }
61
62                 return $Entities;
63         }
64
65         /**
66          * @param array $condition
67          * @param array $params
68          * @return BaseEntity
69          * @throws NotFoundException
70          */
71         protected function _selectOne(array $condition, array $params = []): BaseEntity
72         {
73                 $fields = $this->db->selectFirst(static::$table_name, [], $condition, $params);
74                 if (!$this->db->isResult($fields)) {
75                         throw new NotFoundException();
76                 }
77
78                 return $this->factory->createFromTableRow($fields);
79         }
80
81         /**
82          * @param array $condition
83          * @param array $params
84          * @return int
85          * @throws Exception
86          */
87         public function count(array $condition, array $params = []): int
88         {
89                 return $this->db->count(static::$table_name, $condition, $params);
90         }
91
92         /**
93          * @param array $condition
94          * @return bool
95          * @throws Exception
96          */
97         public function exists(array $condition): bool
98         {
99                 return $this->db->exists(static::$table_name, $condition);
100         }
101 }