Completed
Pull Request — master (#1)
by Roman
03:56
created

KeyedCollection   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 38
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
wmc 7
lcom 1
cbo 1
dl 0
loc 38
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A getAtIndex() 0 9 3
A hasKey() 0 4 1
A getByKey() 0 6 3
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
     * {@inheritdoc}
14
     *
15
     */
16
    public function getAtIndex($index)
17
    {
18
        if (!is_numeric($index)) {
19
            return;
20
        }
21
22
        $values = array_values($this->data);
23
        return isset($values[$index]) ? $values[$index] : null;
24
    }
25
26
27
    /**
28
     * {@inheritdoc}
29
     *
30
     */
31
    public function hasKey($key)
32
    {
33
        return array_key_exists($key, $this->data);
34
    }
35
36
37
    /**
38
     * {@inheritdoc}
39
     *
40
     */
41
    public function getByKey($key)
42
    {
43
        if ($this->hasKey($key)) {
44
            return is_object($this->data[$key]) ? clone $this->data[$key] : $this->data[$key];
45
        }
46
    }
47
}
48