Completed
Pull Request — master (#11)
by
unknown
02:28
created

Dictionary   A

Complexity

Total Complexity 22

Size/Duplication

Total Lines 143
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 5

Test Coverage

Coverage 86.27%

Importance

Changes 10
Bugs 1 Features 2
Metric Value
wmc 22
c 10
b 1
f 2
lcom 2
cbo 5
dl 0
loc 143
ccs 44
cts 51
cp 0.8627
rs 10

14 Methods

Rating   Name   Duplication   Size   Complexity  
A at() 0 4 1
A set() 0 6 1
A get() 0 8 2
A add() 0 9 2
A containsKey() 0 4 1
A contains() 0 4 1
A remove() 0 7 1
A removeKey() 0 7 1
A offsetExists() 0 4 2
A offsetGet() 0 4 1
A offsetSet() 0 8 2
A offsetUnset() 0 4 1
A getIterator() 0 4 1
B addAll() 0 13 5
1
<?php
2
3
// Copyright (c) Lellys Informática. All rights reserved. See License.txt in the project root for license information.
4
namespace Collections;
5
6
use Collections\Exception\KeyException;
7
use Collections\Iterator\MapIterator;
8
use Collections\Traits\GuardTrait;
9
use Collections\Traits\StrictKeyedIterableTrait;
10
use InvalidArgumentException;
11
use Traversable;
12
13
/**
14
 * Represents a collection of keys and values.
15
 */
16
class Dictionary extends AbstractCollectionArray implements MapInterface, \ArrayAccess
17
{
18
    use StrictKeyedIterableTrait,
19
        GuardTrait;
20
21
    public function at($k)
22
    {
23
        return $this[$k];
24
    }
25
26 21
    public function set($key, $value)
27
    {
28 21
        $this->container[$key] = $value;
29
30 21
        return $this;
31
    }
32
33
    /**
34
     * {@inheritdoc}
35
     */
36 8
    public function get($index)
37
    {
38 8
        if ($this->containsKey($index) === false) {
39 1
            throw new \OutOfBoundsException('No element at position ' . $index);
40
        }
41
42 7
        return $this->container[$index];
43
    }
44
45
    /**
46
     * {@inheritdoc}
47
     */
48 11
    public function add($key, $value)
49
    {
50 11
        if ($this->containsKey($key)) {
51 1
            throw new KeyException('The key ' . $key . ' already exists!');
52
        }
53 11
        $this->set($key, $value);
54
55 11
        return $this;
56
    }
57
58
    /**
59
     * {@inheritdoc}
60
     */
61 4
    public function addAll($items)
62
    {
63 4
        if (!is_array($items) && !$items instanceof Traversable) {
64 1
            throw new \InvalidArgumentException('The items must be an array or Traversable');
65
        }
66
67 3
        foreach ($items as $key => $value) {
68 3
            if (is_array($value)) {
69 1
                $value = Dictionary::fromArray($value);
70 1
            }
71 3
            $this->add($key, $value);
72 3
        }
73 3
    }
74
75
    /**
76
     * {@inheritdoc}
77
     */
78 16
    public function containsKey($key)
79
    {
80 16
        return array_key_exists($key, $this->container);
81
    }
82
83
    /**
84
     * {@inheritdoc}
85
     */
86
    public function contains($element)
87
    {
88
        return in_array($element, $this->container, true);
89
    }
90
91
    /**
92
     * {@inheritdoc}
93
     */
94 1
    public function remove($element)
95
    {
96 1
        $key = array_search($element, $this->container);
97 1
        $this->removeKey($key);
98
99
        return $this;
100
    }
101
102
    /**
103
     * {@inheritdoc}
104
     */
105 2
    public function removeKey($key)
106
    {
107 2
        $this->validateKeyBounds($key);
108 1
        unset($this->container[$key]);
109
110 1
        return $this;
111
    }
112
113
    /**
114
     * {@inheritdoc}
115
     */
116 1
    public function offsetExists($offset)
117
    {
118 1
        return isset($this->container[$offset]) || array_key_exists($offset, $this->container);
119
    }
120
121
    /**
122
     * {@inheritdoc}
123
     */
124 5
    public function offsetGet($offset)
125
    {
126 5
        return $this->get($offset);
127
    }
128
129
    /**
130
     * {@inheritdoc}
131
     */
132 10
    public function offsetSet($offset, $value)
133
    {
134 10
        if (is_null($offset)) {
135
            $this->add($offset, $value);
136
        } else {
137 10
            $this->set($offset, $value);
138
        }
139 10
    }
140
141
    /**
142
     * {@inheritdoc}
143
     */
144 1
    public function offsetUnset($offset)
145
    {
146 1
        $this->removeKey($offset);
147 1
    }
148
149
    /**
150
     * Gets the collection's iterator
151
     * @return MapIterator
152
     */
153 9
    public function getIterator()
154
    {
155 9
        return new MapIterator($this->container);
156
    }
157
158
}
159