]> git.mxchange.org Git - friendica.git/blob - src/BaseRepository.php
Use correct placeholder
[friendica.git] / src / BaseRepository.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2010-2022, the Friendica project
4  *
5  * @license GNU AGPL version 3 or any later version
6  *
7  * This program is free software: you can redistribute it and/or modify
8  * it under the terms of the GNU Affero General Public License as
9  * published by the Free Software Foundation, either version 3 of the
10  * License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU Affero General Public License for more details.
16  *
17  * You should have received a copy of the GNU Affero General Public License
18  * along with this program.  If not, see <https://www.gnu.org/licenses/>.
19  *
20  */
21
22 namespace Friendica;
23
24 use Exception;
25 use Friendica\Capabilities\ICanCreateFromTableRow;
26 use Friendica\Database\Database;
27 use Friendica\Database\DBA;
28 use Friendica\Network\HTTPException\NotFoundException;
29 use Psr\Log\LoggerInterface;
30
31 /**
32  * Repositories are meant to store and retrieve Entities from the database.
33  *
34  * The reason why there are methods prefixed with an underscore is because PHP doesn't support generic polymorphism
35  * which means we can't directly overload base methods and make parameters more strict (from a parent class to a child
36  * class for example)
37  *
38  * Similarly, we can't make an overloaded method return type more strict until we only support PHP version 7.4 but this
39  * is less pressing.
40  */
41 abstract class BaseRepository
42 {
43         const LIMIT = 30;
44
45         /**
46          * @var string This should be set to the main database table name the depository is using
47          */
48         protected static $table_name;
49
50         /** @var Database */
51         protected $db;
52
53         /** @var LoggerInterface */
54         protected $logger;
55
56         /** @var ICanCreateFromTableRow */
57         protected $factory;
58
59         public function __construct(Database $database, LoggerInterface $logger, ICanCreateFromTableRow $factory)
60         {
61                 $this->db      = $database;
62                 $this->logger  = $logger;
63                 $this->factory = $factory;
64         }
65
66         /**
67          * Populates the collection according to the condition. Retrieves a limited subset of entities depending on the
68          * boundaries and the limit. The total count of rows matching the condition is stored in the collection.
69          *
70          * Depends on the corresponding table featuring a numerical auto incremented column called `id`.
71          *
72          * max_id and min_id are susceptible to the query order:
73          * - min_id alone only reliably works with ASC order
74          * - max_id alone only reliably works with DESC order
75          * If the wrong order is detected in either case, we reverse the query order and the entity list order after the query
76          *
77          * Chainable.
78          *
79          * @param array    $condition
80          * @param array    $params
81          * @param int|null $min_id Retrieve models with an id no fewer than this, as close to it as possible
82          * @param int|null $max_id Retrieve models with an id no greater than this, as close to it as possible
83          * @param int      $limit
84          * @return BaseCollection
85          * @throws \Exception
86          */
87         protected function _selectByBoundaries(
88                 array $condition = [],
89                 array $params = [],
90                 int $min_id = null,
91                 int $max_id = null,
92                 int $limit = self::LIMIT
93         ): BaseCollection {
94                 $totalCount = $this->count($condition);
95
96                 $boundCondition = $condition;
97
98                 $reverseOrder = false;
99
100                 if (isset($min_id)) {
101                         $boundCondition = DBA::mergeConditions($boundCondition, ['`id` > ?', $min_id]);
102                         if (!isset($max_id) && isset($params['order']['id']) && ($params['order']['id'] === true || $params['order']['id'] === 'DESC')) {
103                                 $reverseOrder = true;
104
105                                 $params['order']['id'] = 'ASC';
106                         }
107                 }
108
109                 if (isset($max_id) && $max_id > 0) {
110                         $boundCondition = DBA::mergeConditions($boundCondition, ['`id` < ?', $max_id]);
111                         if (!isset($min_id) && (!isset($params['order']['id']) || $params['order']['id'] === false || $params['order']['id'] === 'ASC')) {
112                                 $reverseOrder = true;
113
114                                 $params['order']['id'] = 'DESC';
115                         }
116                 }
117
118                 $params['limit'] = $limit;
119
120                 $Entities = $this->_select($boundCondition, $params);
121                 if ($reverseOrder) {
122                         $Entities->reverse();
123                 }
124
125                 return new BaseCollection($Entities->getArrayCopy(), $totalCount);
126         }
127
128         /**
129          * @param array $condition
130          * @param array $params
131          * @return BaseCollection
132          * @throws Exception
133          */
134         protected function _select(array $condition, array $params = []): BaseCollection
135         {
136                 $rows = $this->db->selectToArray(static::$table_name, [], $condition, $params);
137
138                 $Entities = new BaseCollection();
139                 foreach ($rows as $fields) {
140                         $Entities[] = $this->factory->createFromTableRow($fields);
141                 }
142
143                 return $Entities;
144         }
145
146         /**
147          * @param array $condition
148          * @param array $params
149          * @return BaseEntity
150          * @throws NotFoundException
151          */
152         protected function _selectOne(array $condition, array $params = []): BaseEntity
153         {
154                 $fields = $this->db->selectFirst(static::$table_name, [], $condition, $params);
155                 if (!$this->db->isResult($fields)) {
156                         throw new NotFoundException();
157                 }
158
159                 return $this->factory->createFromTableRow($fields);
160         }
161
162         /**
163          * @param array $condition
164          * @param array $params
165          * @return int
166          * @throws Exception
167          */
168         public function count(array $condition, array $params = []): int
169         {
170                 return $this->db->count(static::$table_name, $condition, $params);
171         }
172
173         /**
174          * @param array $condition
175          * @return bool
176          * @throws Exception
177          */
178         public function exists(array $condition): bool
179         {
180                 return $this->db->exists(static::$table_name, $condition);
181         }
182 }