Passed
Pull Request — master (#1317)
by
unknown
21:24
created

AbstractCollection   A

Complexity

Total Complexity 17

Size/Duplication

Total Lines 145
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 71.05%

Importance

Changes 0
Metric Value
wmc 17
lcom 1
cbo 0
dl 0
loc 145
ccs 27
cts 38
cp 0.7105
rs 10
c 0
b 0
f 0

12 Methods

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