Update src/Protocol/Feed.php
[friendica.git/.git] / src / BaseCollection.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2020, Friendica
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 /**
25  * The Collection classes inheriting from this abstract class are meant to represent a list of database record.
26  * The associated model class has to be provided in the child classes.
27  *
28  * Collections can be used with foreach(), accessed like an array and counted.
29  */
30 abstract class BaseCollection extends \ArrayIterator
31 {
32         /**
33          * This property is used with paginated results to hold the total number of items satisfying the paginated request.
34          * @var int
35          */
36         protected $totalCount = 0;
37
38         /**
39          * @param BaseEntity[] $entities
40          * @param int|null     $totalCount
41          */
42         public function __construct(array $entities = [], int $totalCount = null)
43         {
44                 parent::__construct($entities);
45
46                 $this->totalCount = $totalCount ?? count($entities);
47         }
48
49         /**
50          * @inheritDoc
51          */
52         public function offsetSet($offset, $value)
53         {
54                 if (is_null($offset)) {
55                         $this->totalCount++;
56                 }
57
58                 parent::offsetSet($offset, $value);
59         }
60
61         /**
62          * @inheritDoc
63          */
64         public function offsetUnset($offset)
65         {
66                 if ($this->offsetExists($offset)) {
67                         $this->totalCount--;
68                 }
69
70                 parent::offsetUnset($offset);
71         }
72
73         /**
74          * @return int
75          */
76         public function getTotalCount()
77         {
78                 return $this->totalCount;
79         }
80
81         /**
82          * Return the values from a single field in the collection
83          *
84          * @param string   $column
85          * @param int|null $index_key
86          * @return array
87          * @see array_column()
88          */
89         public function column($column, $index_key = null)
90         {
91                 return array_column($this->getArrayCopy(), $column, $index_key);
92         }
93
94         /**
95          * Apply a callback function on all elements in the collection and returns a new collection with the updated elements
96          *
97          * @param callable $callback
98          * @return BaseCollection
99          * @see array_map()
100          */
101         public function map(callable $callback)
102         {
103                 return new static(array_map($callback, $this->getArrayCopy()), $this->getTotalCount());
104         }
105
106         /**
107          * Filters the collection based on a callback that returns a boolean whether the current item should be kept.
108          *
109          * @param callable|null $callback
110          * @param int           $flag
111          * @return BaseCollection
112          * @see array_filter()
113          */
114         public function filter(callable $callback = null, int $flag = 0)
115         {
116                 return new static(array_filter($this->getArrayCopy(), $callback, $flag));
117         }
118 }