]> git.mxchange.org Git - friendica.git/blob - src/BaseCollection.php
Merge pull request #8149 from annando/fix-warning
[friendica.git] / src / BaseCollection.php
1 <?php
2
3 namespace Friendica;
4
5 /**
6  * The Collection classes inheriting from this abstract class are meant to represent a list of database record.
7  * The associated model class has to be provided in the child classes.
8  *
9  * Collections can be used with foreach(), accessed like an array and counted.
10  */
11 abstract class BaseCollection extends \ArrayIterator
12 {
13         /**
14          * This property is used with paginated results to hold the total number of items satisfying the paginated request.
15          * @var int
16          */
17         protected $totalCount = 0;
18
19         /**
20          * @param BaseModel[] $models
21          * @param int|null    $totalCount
22          */
23         public function __construct(array $models = [], int $totalCount = null)
24         {
25                 parent::__construct($models);
26
27                 $this->models = $models;
28                 $this->totalCount = $totalCount ?? count($models);
29         }
30
31         /**
32          * @inheritDoc
33          */
34         public function offsetSet($offset, $value)
35         {
36                 if (is_null($offset)) {
37                         $this->totalCount++;
38                 }
39
40                 parent::offsetSet($offset, $value);
41         }
42
43         /**
44          * @inheritDoc
45          */
46         public function offsetUnset($offset)
47         {
48                 if ($this->offsetExists($offset)) {
49                         $this->totalCount--;
50                 }
51
52                 parent::offsetUnset($offset);
53         }
54
55         /**
56          * @return int
57          */
58         public function getTotalCount()
59         {
60                 return $this->totalCount;
61         }
62
63         /**
64          * Return the values from a single field in the collection
65          *
66          * @param string   $column
67          * @param int|null $index_key
68          * @return array
69          * @see array_column()
70          */
71         public function column($column, $index_key = null)
72         {
73                 return array_column($this->getArrayCopy(), $column, $index_key);
74         }
75
76         /**
77          * Apply a callback function on all elements in the collection and returns a new collection with the updated elements
78          *
79          * @param callable $callback
80          * @return BaseCollection
81          * @see array_map()
82          */
83         public function map(callable $callback)
84         {
85                 return new static(array_map($callback, $this->getArrayCopy()), $this->getTotalCount());
86         }
87
88         /**
89          * Filters the collection based on a callback that returns a boolean whether the current item should be kept.
90          *
91          * @param callable|null $callback
92          * @param int           $flag
93          * @return BaseCollection
94          * @see array_filter()
95          */
96         public function filter(callable $callback = null, int $flag = 0)
97         {
98                 return new static(array_filter($this->getArrayCopy(), $callback, $flag));
99         }
100 }