1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace ApacheSolrForTypo3\Solr\System\Data; |
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
|
|
|
/** |
19
|
|
|
* Class AbstractCollection |
20
|
|
|
*/ |
21
|
|
|
abstract class AbstractCollection implements \IteratorAggregate, \Countable, \ArrayAccess |
22
|
|
|
{ |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @var array |
26
|
|
|
*/ |
27
|
|
|
protected $data = []; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @param array $data |
31
|
|
|
*/ |
32
|
125 |
|
public function __construct(array $data = []) |
33
|
|
|
{ |
34
|
125 |
|
$this->data = $data; |
35
|
125 |
|
} |
36
|
|
|
|
37
|
|
|
public function clean() |
38
|
|
|
{ |
39
|
|
|
$this->data = []; |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* This method can be used to pass a closure to created a filtered copy. |
44
|
|
|
* The closure get an collection item passed and needs to return true when the item should |
45
|
|
|
* be kept or false when it can be skipped. |
46
|
|
|
* |
47
|
|
|
* @param callable $filter |
48
|
|
|
* @return AbstractCollection |
49
|
|
|
*/ |
50
|
|
|
public function getFilteredCopy(\Closure $filter) |
51
|
|
|
{ |
52
|
|
|
$copy = clone $this; |
53
|
22 |
|
$filteredData = []; |
54
|
|
|
foreach ($this->data as $key => $item) { |
55
|
22 |
|
if ($filter($item)) { |
56
|
22 |
|
$filteredData[$key] = $item; |
57
|
22 |
|
} |
58
|
22 |
|
} |
59
|
19 |
|
|
60
|
|
|
$copy->data = $filteredData; |
61
|
|
|
return $copy; |
62
|
|
|
} |
63
|
22 |
|
|
64
|
22 |
|
/** |
65
|
|
|
* @return \ArrayIterator|\Traversable |
66
|
|
|
*/ |
67
|
|
|
public function getIterator() |
68
|
|
|
{ |
69
|
|
|
return new \ArrayIterator($this->data); |
70
|
44 |
|
} |
71
|
|
|
|
72
|
44 |
|
/** |
73
|
|
|
* @return array |
74
|
|
|
*/ |
75
|
|
|
public function getArrayCopy() |
76
|
|
|
{ |
77
|
|
|
return $this->data; |
78
|
28 |
|
} |
79
|
|
|
|
80
|
28 |
|
/** |
81
|
|
|
* @param int $position |
82
|
|
|
* @return object |
83
|
|
|
*/ |
84
|
|
|
public function getByPosition($position) |
85
|
|
|
{ |
86
|
|
|
$keys = array_keys($this->data); |
87
|
42 |
|
return isset($this->data[$keys[$position]]) ? $this->data[$keys[$position]] : null; |
88
|
|
|
} |
89
|
42 |
|
|
90
|
42 |
|
/** |
91
|
|
|
* (PHP 5 >= 5.1.0)<br/> |
92
|
|
|
* Count elements of an object |
93
|
|
|
* @link http://php.net/manual/en/countable.count.php |
94
|
|
|
* @return int The custom count as an integer. |
95
|
|
|
* </p> |
96
|
|
|
* <p> |
97
|
|
|
* The return value is cast to an integer. |
98
|
|
|
*/ |
99
|
|
|
public function count() |
100
|
|
|
{ |
101
|
|
|
return count($this->data); |
102
|
46 |
|
} |
103
|
|
|
|
104
|
46 |
|
/** |
105
|
|
|
* @return int |
106
|
|
|
*/ |
107
|
|
|
public function getCount() |
108
|
|
|
{ |
109
|
|
|
return $this->count(); |
110
|
8 |
|
} |
111
|
|
|
|
112
|
8 |
|
/** |
113
|
|
|
* Whether a offset exists |
114
|
|
|
* |
115
|
|
|
* @param mixed $offset |
116
|
|
|
* @return bool true on success or false on failure |
117
|
|
|
*/ |
118
|
|
|
public function offsetExists($offset) |
119
|
|
|
{ |
120
|
|
|
return array_key_exists($offset, $this->data); |
121
|
6 |
|
} |
122
|
|
|
|
123
|
6 |
|
/** |
124
|
|
|
* Offset to retrieve |
125
|
|
|
* |
126
|
|
|
* @param mixed $offset |
127
|
|
|
* @return mixed |
128
|
|
|
*/ |
129
|
|
|
public function offsetGet($offset) |
130
|
|
|
{ |
131
|
|
|
if ($this->offsetExists($offset)) { |
132
|
6 |
|
return $this->data[$offset]; |
133
|
|
|
} |
134
|
6 |
|
return null; |
135
|
6 |
|
} |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* Offset to set |
139
|
|
|
* |
140
|
|
|
* @param mixed $offset |
141
|
|
|
* @param mixed $value |
142
|
|
|
*/ |
143
|
|
|
public function offsetSet($offset, $value) |
144
|
|
|
{ |
145
|
|
|
if ($offset === null) { |
146
|
|
|
$this->data[] = $value; |
147
|
|
|
return; |
148
|
14 |
|
} |
149
|
|
|
$this->data[$offset] = $value; |
150
|
14 |
|
} |
151
|
14 |
|
|
152
|
14 |
|
/** |
153
|
|
|
* Offset to unset |
154
|
|
|
* |
155
|
|
|
* @param mixed $offset |
156
|
|
|
*/ |
157
|
|
|
public function offsetUnset($offset) |
158
|
|
|
{ |
159
|
|
|
if ($this->offsetExists($offset)) { |
160
|
|
|
unset($this->data[$offset]); |
161
|
|
|
} |
162
|
|
|
} |
163
|
|
|
} |
164
|
|
|
|