6 use Friendica\Capabilities\ICanCreateFromTableRow;
7 use Friendica\Database\Database;
8 use Friendica\Network\HTTPException\NotFoundException;
9 use Psr\Log\LoggerInterface;
12 * Depositories are meant to store and retrieve Entities from the database.
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
18 * Similarly, we can't make an overloaded method return type more strict until we only support PHP version 7.4 but this
21 abstract class BaseDepository
26 * @var string This should be set to the main database table name the depository is using
28 protected static $table_name;
33 /** @var LoggerInterface */
36 /** @var ICanCreateFromTableRow */
39 public function __construct(Database $database, LoggerInterface $logger, ICanCreateFromTableRow $factory)
41 $this->db = $database;
42 $this->logger = $logger;
43 $this->factory = $factory;
48 * @param array $condition
49 * @param array $params
50 * @return BaseCollection
53 protected function _select(array $condition, array $params = []): BaseCollection
55 $rows = $this->db->selectToArray(static::$table_name, [], $condition, $params);
57 $Entities = new BaseCollection();
58 foreach ($rows as $fields) {
59 $Entities[] = $this->factory->createFromTableRow($fields);
66 * @param array $condition
67 * @param array $params
69 * @throws NotFoundException
71 protected function _selectOne(array $condition, array $params = []): BaseEntity
73 $fields = $this->db->selectFirst(static::$table_name, [], $condition, $params);
74 if (!$this->db->isResult($fields)) {
75 throw new NotFoundException();
78 return $this->factory->createFromTableRow($fields);
82 * @param array $condition
83 * @param array $params
87 public function count(array $condition, array $params = []): int
89 return $this->db->count(static::$table_name, $condition, $params);
93 * @param array $condition
97 public function exists(array $condition): bool
99 return $this->db->exists(static::$table_name, $condition);