]> git.mxchange.org Git - friendica.git/blob - src/BaseCollection.php
Reverted some changes that won't work with PHP7.3
[friendica.git] / src / BaseCollection.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 /**
25  * The Collection classes inheriting from this class are meant to represent a list of structured objects of a single type.
26  *
27  * Collections can be used with foreach(), accessed like an array and counted.
28  */
29 class BaseCollection extends \ArrayIterator
30 {
31         /**
32          * This property is used with paginated results to hold the total number of items satisfying the paginated request.
33          * @var int
34          */
35         protected $totalCount = 0;
36
37         /**
38          * @param BaseEntity[] $entities
39          * @param int|null     $totalCount
40          */
41         public function __construct(array $entities = [], int $totalCount = null)
42         {
43                 parent::__construct($entities);
44
45                 $this->totalCount = $totalCount ?? count($entities);
46         }
47
48         /**
49          * @inheritDoc
50          */
51         public function offsetSet($offset, $value)
52         {
53                 if (is_null($offset)) {
54                         $this->totalCount++;
55                 }
56
57                 parent::offsetSet($offset, $value);
58         }
59
60         /**
61          * @inheritDoc
62          */
63         public function offsetUnset($offset)
64         {
65                 if ($this->offsetExists($offset)) {
66                         $this->totalCount--;
67                 }
68
69                 parent::offsetUnset($offset);
70         }
71
72         /**
73          * Getter for total count
74          *
75          * @return int Total count
76          */
77         public function getTotalCount(): int
78         {
79                 return $this->totalCount;
80         }
81
82         /**
83          * Return the values from a single field in the collection
84          *
85          * @param string   $column
86          * @param int|null $index_key
87          * @return array
88          * @see array_column()
89          */
90         public function column(string $column, $index_key = null): array
91         {
92                 return array_column($this->getArrayCopy(true), $column, $index_key);
93         }
94
95         /**
96          * Apply a callback function on all elements in the collection and returns a new collection with the updated elements
97          *
98          * @param callable $callback
99          * @return BaseCollection
100          * @see array_map()
101          */
102         public function map(callable $callback): BaseCollection
103         {
104                 return new static(array_map($callback, $this->getArrayCopy()), $this->getTotalCount());
105         }
106
107         /**
108          * Filters the collection based on a callback that returns a boolean whether the current item should be kept.
109          *
110          * @param callable|null $callback
111          * @param int           $flag
112          * @return BaseCollection
113          * @see array_filter()
114          */
115         public function filter(callable $callback = null, int $flag = 0): BaseCollection
116         {
117                 return new static(array_filter($this->getArrayCopy(), $callback, $flag));
118         }
119
120         /**
121          * Reverse the orders of the elements in the collection
122          *
123          * @return $this
124          */
125         public function reverse(): BaseCollection
126         {
127                 return new static(array_reverse($this->getArrayCopy()), $this->getTotalCount());
128         }
129
130         /**
131          * @inheritDoc
132          *
133          * includes recursion for entity::toArray() function
134          * @see BaseEntity::toArray()
135          */
136         public function getArrayCopy(bool $recursive = false): array
137         {
138                 if (!$recursive) {
139                         return parent::getArrayCopy();
140                 }
141
142                 return array_map(function ($item) {
143                         return is_object($item) ? $item->toArray() : $item;
144                 }, parent::getArrayCopy());
145         }
146 }