Completed
Push — master ( 887cf7...2ea773 )
by Emily
02:13
created

AbstractMap::add()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1.037

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 3
cp 0.6667
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 2
crap 1.037
1
<?php
2
/**
3
 * This file is part of the Composite Utils package.
4
 *
5
 * (c) Emily Shepherd <[email protected]>
6
 *
7
 * For the full copyright and license information, please view the
8
 * LICENSE.md file that was distributed with this source code.
9
 *
10
 * @package spaark/composite-utils
11
 * @author Emily Shepherd <[email protected]>
12
 * @license MIT
13
 */
14
15
namespace Spaark\CompositeUtils\Model\Collection;
16
17
/**
18
 * Represents an abstract collection which maps one value to another
19
 *
20
 * These are stored as pairs
21
 *
22
 * @generic KeyType
23
 * @generic ValueType
24
 */
25
abstract class AbstractMap
26
    extends AbstractCollection
0 ignored issues
show
Coding Style introduced by
The extends keyword must be on the same line as the class name
Loading history...
Coding Style introduced by
Expected 0 spaces between "AbstractCollection" and comma; 1 found
Loading history...
27
    implements MapInterface
0 ignored issues
show
Coding Style introduced by
The implements keyword must be on the same line as the class name
Loading history...
28
{
29
    /**
30
     * Adds an element to the Map
31
     *
32
     * @param KeyType $key The key to add
33
     * @param ValueType $value The value to add
34
     */
35
    public function offsetSet($key, $value)
36
    {
37
        $this->add($key, $value);
38
    }
39
40
    /**
41
     * Adds an element to the Map
42
     *
43
     * @param KeyType $key The key to add
44
     * @param ValueType $value The value to add
45
     */
46 1
    public function add($key, $value)
47
    {
48 1
        $this->insert(new Pair($key, $value));
49
    }
50
51
    /**
52
     * Checks if a key exists
53
     *
54
     * @param KeyType $key The key to search for
55
     * @return boolean
56
     */
57
    public function offsetExists($key) : bool
58
    {
59
        return $this->contains($key);
60
    }
61
62
    /**
63
     * Removes an item from the map
64
     *
65
     * @param KeyType $key The key of the keypair to remove
66
     */
67
    public function offsetUnset($key)
68
    {
69
        $this->remove($key);
70
    }
71
72
    /**
73
     * Gets an item from the map, looking it up by the specified key
74
     *
75
     * @param KeyType $key
76
     * @return ValueType
77
     */
78
    public function offsetGet($key)
79
    {
80
        return $this->get($key);
81
    }
82
}
83