]> git.mxchange.org Git - friendica.git/blob - src/BaseCollection.php
Escape the "share" as well
[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          * @return int
74          */
75         public function getTotalCount()
76         {
77                 return $this->totalCount;
78         }
79
80         /**
81          * Return the values from a single field in the collection
82          *
83          * @param string   $column
84          * @param int|null $index_key
85          * @return array
86          * @see array_column()
87          */
88         public function column($column, $index_key = null)
89         {
90                 return array_column($this->getArrayCopy(true), $column, $index_key);
91         }
92
93         /**
94          * Apply a callback function on all elements in the collection and returns a new collection with the updated elements
95          *
96          * @param callable $callback
97          * @return BaseCollection
98          * @see array_map()
99          */
100         public function map(callable $callback)
101         {
102                 return new static(array_map($callback, $this->getArrayCopy()), $this->getTotalCount());
103         }
104
105         /**
106          * Filters the collection based on a callback that returns a boolean whether the current item should be kept.
107          *
108          * @param callable|null $callback
109          * @param int           $flag
110          * @return BaseCollection
111          * @see array_filter()
112          */
113         public function filter(callable $callback = null, int $flag = 0)
114         {
115                 return new static(array_filter($this->getArrayCopy(), $callback, $flag));
116         }
117
118         /**
119          * Reverse the orders of the elements in the collection
120          *
121          * @return $this
122          */
123         public function reverse(): BaseCollection
124         {
125                 return new static(array_reverse($this->getArrayCopy()), $this->getTotalCount());
126         }
127
128         /**
129          * @inheritDoc
130          *
131          * includes recursion for entity::toArray() function
132          * @see BaseEntity::toArray()
133          */
134         public function getArrayCopy(bool $recursive = false): array
135         {
136                 if (!$recursive) {
137                         return parent::getArrayCopy();
138                 }
139
140                 return array_map(function ($item) {
141                         return is_object($item) ? $item->toArray() : $item;
142                 }, parent::getArrayCopy());
143         }
144 }