Completed
Pull Request — master (#527)
by Mantas
10:45 queued 03:22
created

Collection::key()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
/*
4
 * This file is part of the ONGR package.
5
 *
6
 * (c) NFQ Technologies UAB <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace ONGR\ElasticsearchBundle;
13
14
/**
15
 * This class is a holder for collection of objects.
16
 */
17
class Collection implements \Countable, \Iterator, \ArrayAccess
18
{
19
    /**
20
     * @var array
21
     */
22
    private $elements = [];
23
24
    /**
25
     * Constructor.
26
     *
27
     * @param array $elements
28
     */
29
    public function __construct(array $elements = [])
30
    {
31
        $this->elements = $elements;
32
    }
33
34
    /**
35
     * {@inheritdoc}
36
     */
37
    public function current()
38
    {
39
        return current($this->elements);
40
    }
41
42
    /**
43
     * {@inheritdoc}
44
     */
45
    public function next()
46
    {
47
        next($this->elements);
48
    }
49
50
    /**
51
     * {@inheritdoc}
52
     */
53
    public function key()
54
    {
55
        return key($this->elements);
56
    }
57
58
    /**
59
     * {@inheritdoc}
60
     */
61
    public function valid()
62
    {
63
        return $this->offsetExists($this->key());
64
    }
65
66
    /**
67
     * {@inheritdoc}
68
     */
69
    public function rewind()
70
    {
71
        reset($this->elements);
72
    }
73
74
    /**
75
     * {@inheritdoc}
76
     */
77
    public function offsetExists($offset)
78
    {
79
        return array_key_exists($offset, $this->elements);
80
    }
81
82
    /**
83
     * {@inheritdoc}
84
     */
85
    public function offsetGet($offset)
86
    {
87
        return $this->elements[$offset];
88
    }
89
90
    /**
91
     * {@inheritdoc}
92
     */
93
    public function offsetSet($offset, $value)
94
    {
95
        if ($offset === null) {
96
            $this->elements[] = $value;
97
        } else {
98
            $this->elements[$offset] = $value;
99
        }
100
    }
101
102
    /**
103
     * {@inheritdoc}
104
     */
105
    public function offsetUnset($offset)
106
    {
107
        unset($this->elements[$offset]);
108
    }
109
110
    /**
111
     * {@inheritdoc}
112
     */
113
    public function count()
114
    {
115
        return count($this->elements);
116
    }
117
}
118