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);
73 * Getter for total count
75 * @return int Total count
77 public function getTotalCount(): int
79 return $this->totalCount;
83 * Return the values from a single field in the collection
85 * @param string $column
86 * @param int|null $index_key
90 public function column(string $column, $index_key = null): array
92 return array_column($this->getArrayCopy(true), $column, $index_key);
96 * Apply a callback function on all elements in the collection and returns a new collection with the updated elements
98 * @param callable $callback
99 * @return BaseCollection
102 public function map(callable $callback): BaseCollection
104 return new static(array_map($callback, $this->getArrayCopy()), $this->getTotalCount());
108 * Filters the collection based on a callback that returns a boolean whether the current item should be kept.
110 * @param callable|null $callback
112 * @return BaseCollection
113 * @see array_filter()
115 public function filter(callable $callback = null, int $flag = 0): BaseCollection
117 return new static(array_filter($this->getArrayCopy(), $callback, $flag));
121 * Reverse the orders of the elements in the collection
125 public function reverse(): BaseCollection
127 return new static(array_reverse($this->getArrayCopy()), $this->getTotalCount());
133 * includes recursion for entity::toArray() function
134 * @see BaseEntity::toArray()
136 public function getArrayCopy(bool $recursive = false): array
139 return parent::getArrayCopy();
142 return array_map(function ($item) {
143 return is_object($item) ? $item->toArray() : $item;
144 }, parent::getArrayCopy());