|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
/* |
|
6
|
|
|
* This file is part of the TYPO3 CMS project. |
|
7
|
|
|
* |
|
8
|
|
|
* It is free software; you can redistribute it and/or modify it under |
|
9
|
|
|
* the terms of the GNU General Public License, either version 2 |
|
10
|
|
|
* of the License, or any later version. |
|
11
|
|
|
* |
|
12
|
|
|
* For the full copyright and license information, please read the |
|
13
|
|
|
* LICENSE.txt file that was distributed with this source code. |
|
14
|
|
|
* |
|
15
|
|
|
* The TYPO3 project - inspiring people to share! |
|
16
|
|
|
*/ |
|
17
|
|
|
|
|
18
|
|
|
namespace ApacheSolrForTypo3\Solr\System\Data; |
|
19
|
|
|
|
|
20
|
|
|
use ArrayAccess; |
|
21
|
|
|
use ArrayIterator; |
|
22
|
|
|
use Closure; |
|
23
|
|
|
use Countable; |
|
24
|
|
|
use IteratorAggregate; |
|
25
|
|
|
use ReturnTypeWillChange; |
|
26
|
|
|
use Traversable; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* Class AbstractCollection |
|
30
|
|
|
*/ |
|
31
|
|
|
abstract class AbstractCollection implements IteratorAggregate, Countable, ArrayAccess |
|
32
|
|
|
{ |
|
33
|
|
|
/** |
|
34
|
|
|
* @var array |
|
35
|
|
|
*/ |
|
36
|
|
|
protected array $data = []; |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* @param array $data |
|
40
|
|
|
*/ |
|
41
|
161 |
|
public function __construct(array $data = []) |
|
42
|
|
|
{ |
|
43
|
161 |
|
$this->data = $data; |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* Clears the collection data. |
|
48
|
|
|
* |
|
49
|
|
|
* @noinspection PhpUnused |
|
50
|
|
|
*/ |
|
51
|
|
|
public function clean(): void |
|
52
|
|
|
{ |
|
53
|
|
|
$this->data = []; |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* This method can be used to pass a closure to create a filtered copy. |
|
58
|
|
|
* The closure get a collection item passed and needs to return true when the item should |
|
59
|
|
|
* be kept or false when it can be skipped. |
|
60
|
|
|
* |
|
61
|
|
|
* @param Closure $filter |
|
62
|
|
|
* @return AbstractCollection |
|
63
|
|
|
*/ |
|
64
|
51 |
|
public function getFilteredCopy(Closure $filter): AbstractCollection |
|
65
|
|
|
{ |
|
66
|
51 |
|
$copy = clone $this; |
|
67
|
51 |
|
$filteredData = []; |
|
68
|
51 |
|
foreach ($this->data as $key => $item) { |
|
69
|
39 |
|
if ($filter($item)) { |
|
70
|
36 |
|
$filteredData[$key] = $item; |
|
71
|
|
|
} |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
51 |
|
$copy->data = $filteredData; |
|
75
|
51 |
|
return $copy; |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
/** |
|
79
|
|
|
* @return Traversable |
|
80
|
|
|
*/ |
|
81
|
74 |
|
public function getIterator(): Traversable |
|
82
|
|
|
{ |
|
83
|
74 |
|
return new ArrayIterator($this->data); |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
/** |
|
87
|
|
|
* @return array |
|
88
|
|
|
*/ |
|
89
|
28 |
|
public function getArrayCopy(): array |
|
90
|
|
|
{ |
|
91
|
28 |
|
return $this->data; |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
/** |
|
95
|
|
|
* @param int $position |
|
96
|
|
|
* @return ?object |
|
97
|
|
|
*/ |
|
98
|
42 |
|
public function getByPosition(int $position): ?object |
|
99
|
|
|
{ |
|
100
|
42 |
|
$keys = array_keys($this->data); |
|
101
|
42 |
|
return $this->data[$keys[$position] ?? ''] ?? null; |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
/** |
|
105
|
|
|
* (PHP 5 >= 5.1.0)<br/> |
|
106
|
|
|
* Count elements of an object |
|
107
|
|
|
* @link http://php.net/manual/en/countable.count.php |
|
108
|
|
|
* @return int The custom count as an integer. |
|
109
|
|
|
* </p> |
|
110
|
|
|
* <p> |
|
111
|
|
|
* The return value is cast to an integer. |
|
112
|
|
|
*/ |
|
113
|
82 |
|
public function count(): int |
|
114
|
|
|
{ |
|
115
|
82 |
|
return count($this->data); |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
/** |
|
119
|
|
|
* @return int |
|
120
|
|
|
*/ |
|
121
|
30 |
|
public function getCount(): int |
|
122
|
|
|
{ |
|
123
|
30 |
|
return $this->count(); |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
|
|
/** |
|
127
|
|
|
* @inheritDoc |
|
128
|
|
|
*/ |
|
129
|
35 |
|
public function offsetExists($offset): bool |
|
130
|
|
|
{ |
|
131
|
35 |
|
return array_key_exists($offset, $this->data); |
|
132
|
|
|
} |
|
133
|
|
|
|
|
134
|
|
|
/** |
|
135
|
|
|
* Offset to retrieve |
|
136
|
|
|
* |
|
137
|
|
|
* @param mixed $offset |
|
138
|
|
|
* @return mixed |
|
139
|
|
|
* @noinspection PhpLanguageLevelInspection |
|
140
|
|
|
*/ |
|
141
|
4 |
|
#[ReturnTypeWillChange] |
|
142
|
2 |
|
public function offsetGet($offset) |
|
143
|
|
|
{ |
|
144
|
6 |
|
if ($this->offsetExists($offset)) { |
|
145
|
6 |
|
return $this->data[$offset]; |
|
146
|
|
|
} |
|
147
|
|
|
return null; |
|
148
|
|
|
} |
|
149
|
|
|
|
|
150
|
|
|
/** |
|
151
|
|
|
* Offset to set |
|
152
|
|
|
* |
|
153
|
|
|
* @param mixed $offset |
|
154
|
|
|
* @param mixed $value |
|
155
|
|
|
*/ |
|
156
|
40 |
|
public function offsetSet($offset, $value): void |
|
157
|
|
|
{ |
|
158
|
40 |
|
if ($offset === null) { |
|
159
|
40 |
|
$this->data[] = $value; |
|
160
|
40 |
|
return; |
|
161
|
|
|
} |
|
162
|
|
|
$this->data[$offset] = $value; |
|
163
|
|
|
} |
|
164
|
|
|
|
|
165
|
|
|
/** |
|
166
|
|
|
* Offset to unset |
|
167
|
|
|
* |
|
168
|
|
|
* @param mixed $offset |
|
169
|
|
|
*/ |
|
170
|
|
|
public function offsetUnset($offset): void |
|
171
|
|
|
{ |
|
172
|
|
|
if ($this->offsetExists($offset)) { |
|
173
|
|
|
unset($this->data[$offset]); |
|
174
|
|
|
} |
|
175
|
|
|
} |
|
176
|
|
|
} |
|
177
|
|
|
|