3 * @copyright Copyright (C) 2010-2023, 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 #[\ReturnTypeWillChange]
52 public function offsetSet($key, $value): void
58 parent::offsetSet($key, $value);
64 #[\ReturnTypeWillChange]
65 public function offsetUnset($key): void
67 if ($this->offsetExists($key)) {
71 parent::offsetUnset($key);
75 * Getter for total count
77 * @return int Total count
79 public function getTotalCount(): int
81 return $this->totalCount;
85 * Return the values from a single field in the collection
87 * @param string $column
88 * @param int|null $index_key
92 public function column(string $column, $index_key = null): array
94 return array_column($this->getArrayCopy(true), $column, $index_key);
98 * Apply a callback function on all elements in the collection and returns a new collection with the updated elements
100 * @param callable $callback
101 * @return BaseCollection
104 public function map(callable $callback): BaseCollection
106 return new static(array_map($callback, $this->getArrayCopy()), $this->getTotalCount());
110 * Filters the collection based on a callback that returns a boolean whether the current item should be kept.
112 * @param callable|null $callback
114 * @return BaseCollection
115 * @see array_filter()
117 public function filter(callable $callback = null, int $flag = 0): BaseCollection
119 return new static(array_filter($this->getArrayCopy(), $callback, $flag));
123 * Reverse the orders of the elements in the collection
127 public function reverse(): BaseCollection
129 return new static(array_reverse($this->getArrayCopy()), $this->getTotalCount());
135 * includes recursion for entity::toArray() function
136 * @see BaseEntity::toArray()
138 public function getArrayCopy(bool $recursive = false): array
141 return parent::getArrayCopy();
144 return array_map(function ($item) {
145 return is_object($item) ? $item->toArray() : $item;
146 }, parent::getArrayCopy());