]> git.mxchange.org Git - friendica.git/blob - src/BaseCollection.php
Merge pull request #8222 from annando/ap-gnusocial
[friendica.git] / src / BaseCollection.php
1 <?php
2
3 namespace Friendica;
4
5 /**
6  * The Collection classes inheriting from this abstract class are meant to represent a list of database record.
7  * The associated model class has to be provided in the child classes.
8  *
9  * Collections can be used with foreach(), accessed like an array and counted.
10  */
11 abstract class BaseCollection extends \ArrayIterator
12 {
13         /**
14          * This property is used with paginated results to hold the total number of items satisfying the paginated request.
15          * @var int
16          */
17         protected $totalCount = 0;
18
19         /**
20          * @param BaseEntity[] $entities
21          * @param int|null     $totalCount
22          */
23         public function __construct(array $entities = [], int $totalCount = null)
24         {
25                 parent::__construct($entities);
26
27                 $this->totalCount = $totalCount ?? count($entities);
28         }
29
30         /**
31          * @inheritDoc
32          */
33         public function offsetSet($offset, $value)
34         {
35                 if (is_null($offset)) {
36                         $this->totalCount++;
37                 }
38
39                 parent::offsetSet($offset, $value);
40         }
41
42         /**
43          * @inheritDoc
44          */
45         public function offsetUnset($offset)
46         {
47                 if ($this->offsetExists($offset)) {
48                         $this->totalCount--;
49                 }
50
51                 parent::offsetUnset($offset);
52         }
53
54         /**
55          * @return int
56          */
57         public function getTotalCount()
58         {
59                 return $this->totalCount;
60         }
61
62         /**
63          * Return the values from a single field in the collection
64          *
65          * @param string   $column
66          * @param int|null $index_key
67          * @return array
68          * @see array_column()
69          */
70         public function column($column, $index_key = null)
71         {
72                 return array_column($this->getArrayCopy(), $column, $index_key);
73         }
74
75         /**
76          * Apply a callback function on all elements in the collection and returns a new collection with the updated elements
77          *
78          * @param callable $callback
79          * @return BaseCollection
80          * @see array_map()
81          */
82         public function map(callable $callback)
83         {
84                 return new static(array_map($callback, $this->getArrayCopy()), $this->getTotalCount());
85         }
86
87         /**
88          * Filters the collection based on a callback that returns a boolean whether the current item should be kept.
89          *
90          * @param callable|null $callback
91          * @param int           $flag
92          * @return BaseCollection
93          * @see array_filter()
94          */
95         public function filter(callable $callback = null, int $flag = 0)
96         {
97                 return new static(array_filter($this->getArrayCopy(), $callback, $flag));
98         }
99 }