Completed
Push — master ( 813757...d8be45 )
by Ítalo
03:56 queued 02:00
created

Dictionary::addAll()   B

Complexity

Conditions 5
Paths 4

Size

Total Lines 13
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 5

Importance

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