3 * @copyright Copyright (C) 2010-2022, the Friendica project
5 * @license GNU AGPL version 3 or any later version
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.
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.
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/>.
25 * The Collection classes inheriting from this class are meant to represent a list of structured objects of a single type.
27 * Collections can be used with foreach(), accessed like an array and counted.
29 class BaseCollection extends \ArrayIterator
32 * This property is used with paginated results to hold the total number of items satisfying the paginated request.
35 protected $totalCount = 0;
38 * @param BaseEntity[] $entities
39 * @param int|null $totalCount
41 public function __construct(array $entities = [], int $totalCount = null)
43 parent::__construct($entities);
45 $this->totalCount = $totalCount ?? count($entities);
51 public function offsetSet($offset, $value)
53 if (is_null($offset)) {
57 parent::offsetSet($offset, $value);
63 public function offsetUnset($offset)
65 if ($this->offsetExists($offset)) {
69 parent::offsetUnset($offset);
75 public function getTotalCount()
77 return $this->totalCount;
81 * Return the values from a single field in the collection
83 * @param string $column
84 * @param int|null $index_key
88 public function column($column, $index_key = null)
90 return array_column($this->getArrayCopy(true), $column, $index_key);
94 * Apply a callback function on all elements in the collection and returns a new collection with the updated elements
96 * @param callable $callback
97 * @return BaseCollection
100 public function map(callable $callback)
102 return new static(array_map($callback, $this->getArrayCopy()), $this->getTotalCount());
106 * Filters the collection based on a callback that returns a boolean whether the current item should be kept.
108 * @param callable|null $callback
110 * @return BaseCollection
111 * @see array_filter()
113 public function filter(callable $callback = null, int $flag = 0)
115 return new static(array_filter($this->getArrayCopy(), $callback, $flag));
119 * Reverse the orders of the elements in the collection
123 public function reverse(): BaseCollection
125 return new static(array_reverse($this->getArrayCopy()), $this->getTotalCount());
131 * includes recursion for entity::toArray() function
132 * @see BaseEntity::toArray()
134 public function getArrayCopy(bool $recursive = false): array
137 return parent::getArrayCopy();
140 return array_map(function ($item) {
141 return is_object($item) ? $item->toArray() : $item;
142 }, parent::getArrayCopy());