Completed
Branch master (7ef6dd)
by Bingo
07:42 queued 03:47
created

VertexMap::remove()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 1
c 0
b 0
f 0
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
cc 1
nc 1
nop 1
crap 1
1
<?php
2
3
namespace graphp\vertex;
4
5
use ArrayObject;
6
use graphp\edge\EdgeContainerInterface;
7
8
/**
9
 * Class VertexMap
10
 *
11
 * @package graphp\vertex
12
 */
13
class VertexMap extends ArrayObject
14
{
15
    private $vertices = [];
16
17
    /**
18
     * Construct a vertex map. It maps vertices to corresponding edge containers
19
     *
20
     * @param array $input - array of vertex - edge container pairs
21
     */
22 13
    public function __construct(array $input = [])
23
    {
24 13
        parent::__construct($input, ArrayObject::ARRAY_AS_PROPS);
25 13
    }
26
27
    /**
28
     * Get the edge container of the specified vertex
29
     *
30
     * @return null|EdgeContainerInterface
31
     */
32 10
    public function get(VertexInterface $vertex): ?EdgeContainerInterface
33
    {
34 10
        $id = $vertex->getHash();
35 10
        if (array_key_exists($id, $this->getArrayCopy())) {
36 10
            return $this->getArrayCopy()[$id];
37
        }
38 1
        return null;
39
    }
40
41
    /**
42
     * Put a new value to vertexmap
43
     *
44
     * @param VertexInterface $vertex - the vertex
45
     * @param EdgeContainerInterface $ec - the edge container
46
     */
47 11
    public function put(VertexInterface $vertex, ?EdgeContainerInterface $ec = null): void
48
    {
49 11
        $id = $vertex->getHash();
50 11
        parent::offsetSet($id, $ec);
51 11
        $this->vertices[$id] = $vertex;
52 11
    }
53
54
    /**
55
     * Remove a vertex from the vertexmap
56
     *
57
     * @param VertexInterface $vertex - niddle
58
     */
59 2
    public function remove(VertexInterface $vertex): void
60
    {
61 2
        $this->offsetUnset($vertex->getHash());
62 2
    }
63
64
    /**
65
     * Unset VertexMap value by key
66
     *
67
     * @param mixed $offset - array offset
68
     */
69 2
    public function offsetUnset($offset): void
70
    {
71 2
        $iter = $this->getIterator();
72 2
        $iter->offsetUnset($offset);
73 2
        unset($this->vertices[$offset]);
74 2
    }
75
76
    /**
77
     * Get array of vertices
78
     *
79
     * @return array
80
     */
81 11
    public function keySet(): VertexSet
82
    {
83 11
        return new VertexSet(array_values($this->vertices));
0 ignored issues
show
Bug Best Practice introduced by
The expression return new graphp\vertex...alues($this->vertices)) returns the type graphp\vertex\VertexSet which is incompatible with the documented return type array.
Loading history...
84
    }
85
}
86