Completed
Push — master ( 3b667c...fb9863 )
by Owen
02:06
created

KeyedCollection::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
3
namespace Mundanity\Collection;
4
5
6
/**
7
 * A keyed collection.
8
 *
9
 */
10
class KeyedCollection extends Collection implements KeyedCollectionInterface
11
{
12
    /**
13
     * Constructor
14
     *
15
     * @param array $data
16
     *   An array of data to populate the collection with.
17
     *
18
     */
19
    public function __construct(array $data = [])
20
    {
21
        $this->data = $data;
22
    }
23
24
25
    /**
26
     * {@inheritdoc{
27
     *
28
     */
29
    public function getAtIndex($index)
30
    {
31
        if (!is_numeric($index)) {
32
            return;
33
        }
34
35
        $values = array_values($this->data);
36
        return isset($values[$index]) ? $values[$index] : null;
37
    }
38
39
40
    /**
41
     * {@inheritdoc}
42
     *
43
     */
44
    public function hasKey($key)
45
    {
46
        return array_key_exists($key, $this->data);
47
    }
48
49
50
    /**
51
     * {@inheritdoc}
52
     *
53
     */
54
    public function getByKey($key)
55
    {
56
        if ($this->hasKey($key)) {
57
            return is_object($this->data[$key]) ? clone $this->data[$key] : $this->data[$key];
58
        }
59
    }
60
}
61