]> git.mxchange.org Git - friendica.git/blob - src/BaseCollection.php
Introduce Repository, Factory, Collection, Model base classes
[friendica.git] / src / BaseCollection.php
1 <?php
2
3 namespace Friendica;
4
5 use Friendica\Database\Database;
6 use Friendica\Database\DBA;
7 use Psr\Log\LoggerInterface;
8
9 /**
10  * The Collection classes inheriting from this abstract class are meant to represent a list of database record.
11  * The associated model class has to be provided in the child classes.
12  *
13  * Collections can be used with foreach(), accessed like an array and counted.
14  */
15 abstract class BaseCollection extends \ArrayIterator
16 {
17         /**
18          * This property is used with paginated results to hold the total number of items satisfying the paginated request.
19          * @var int
20          */
21         protected $totalCount = 0;
22
23         /**
24          * @param BaseModel[] $models
25          * @param int|null    $totalCount
26          */
27         public function __construct(array $models = [], int $totalCount = null)
28         {
29                 parent::__construct($models);
30
31                 $this->models = $models;
32                 $this->totalCount = $totalCount ?? count($models);
33         }
34
35         /**
36          * @return int
37          */
38         public function getTotalCount()
39         {
40                 return $this->totalCount;
41         }
42 }