Completed
Push — master ( f7852e...e666ea )
by Richard
06:08
created

Graph::nodes()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 6
ccs 4
cts 4
cp 1
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 4
nc 2
nop 1
crap 2
1
<?php
2
/******************************************************************************
3
 * An implementation of dicto (scg.unibe.ch/dicto) in and for PHP.
4
 * 
5
 * Copyright (c) 2016 Richard Klees <[email protected]>
6
 *
7
 * This software is licensed under The MIT License. You should have received 
8
 * a copy of the license along with the code.
9
 */
10
11
namespace Lechimp\Dicto\Graph;
12
13
/**
14
 * The complete graph.
15
 */
16
class Graph {
17
    /**
18
     * @var array<int, Node>
19
     */
20
    protected $nodes = [];
21
22
    /**
23
     * @var int
24
     */
25
    protected $id_counter = 0;
26
27
    /**
28
     * Create a new node in the graph.
29
     *
30
     * @param   string              $type
31
     * @param   array<string,mixed> $properties
32
     * @return  Node
33
     */
34 67
    public function create_node($type, array $properties) {
35 67
        $node = $this->build_node($this->id_counter, $type, $properties);
36 67
        $this->nodes[] = $node;
37 67
        $this->id_counter++;
38 67
        return $node;
39
    }
40
41 67
    protected function build_node($id, $type, array $properties) {
42 67
        return new Node($id, $type, $properties);
43
    }
44
45
    /**
46
     * Add a relation to the graph.
47
     *
48
     * @param   Node                $left
49
     * @param   string              $type
50
     * @param   array<string,mixed> $properties
51
     * @param   Node                $right
52
     * @return  Relation
53
     */
54 53
    public function add_relation(Node $left, $type, array $properties, Node $right) {
55 53
        return $left->add_relation($type, $properties, $right);
56
    }
57
58
    /**
59
     * Get nodes from the graph, maybe filtered by a filter.
60
     *
61
     * @param   \Closure|null    $filter
62
     * @return  Node[]
63
     */
64 64
    public function nodes(\Closure $filter = null) {
65 64
        if ($filter === null) {
66 63
            return $this->nodes;
67
        }
68 1
        return array_filter($this->nodes, $filter);
69
    }
70
71
    /**
72
     * Get the node with the given id.
73
     *
74
     * @param   int     $id
75
     * @throws  \InvalidArgumentException   if $id is unknown
76
     * @return  Node
77
     */
78 1
    public function node($id) {
79 1
        assert('is_int($id)');
80 1
        if (!array_key_exists($id, $this->nodes)) {
81 1
            throw new \InvalidArgumentException("Unknown node id '$id'");
82
        }
83 1
        return $this->nodes[$id];
84
    }
85
86
    /**
87
     * Build a query on the graph.
88
     *
89
     * @return  Query
90
     */
91 7
    public function query() {
92 7
        return new QueryImpl($this);
93
    }
94
}
95