|
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
|
|
|
* A node in the graph. It has an id to make it identifiable. It also |
|
15
|
|
|
* has some relations to other nodes. |
|
16
|
|
|
*/ |
|
17
|
|
|
class Node extends Entity { |
|
18
|
|
|
/** |
|
19
|
|
|
* @var int |
|
20
|
|
|
*/ |
|
21
|
|
|
private $id; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* @var Relation[] |
|
25
|
|
|
*/ |
|
26
|
|
|
private $relations = []; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* @param int $id |
|
30
|
|
|
* @param string $type |
|
31
|
|
|
* @param array<string,mixed> $properties |
|
32
|
|
|
*/ |
|
33
|
79 |
|
public function __construct($id, $type, array $properties) { |
|
34
|
79 |
|
assert('is_int($id)'); |
|
35
|
79 |
|
$this->id = $id; |
|
36
|
79 |
|
parent::__construct($type, $properties); |
|
37
|
79 |
|
} |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* Get the id. |
|
41
|
|
|
* |
|
42
|
|
|
* @return int |
|
43
|
|
|
*/ |
|
44
|
3 |
|
public function id() { |
|
45
|
3 |
|
return $this->id; |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* Add a relation from this graph to another. |
|
50
|
|
|
* |
|
51
|
|
|
* @param string $type |
|
52
|
|
|
* @param array<string,mixed> $properties |
|
53
|
|
|
* @param Node $other |
|
54
|
|
|
* @return Relation |
|
55
|
|
|
*/ |
|
56
|
59 |
|
public function add_relation($type, array $properties, Node $other) { |
|
57
|
59 |
|
$rel = $this->build_relation($type, $properties, $other); |
|
58
|
59 |
|
$this->relations[] = $rel; |
|
59
|
59 |
|
return $rel; |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
59 |
|
protected function build_relation($type, array $properties, $other) { |
|
63
|
59 |
|
return new Relation($type, $properties, $other); |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* Get the relations to other nodes. |
|
68
|
|
|
* |
|
69
|
|
|
* @param \Closure|null $filter |
|
70
|
|
|
* @return Relation[] |
|
71
|
|
|
*/ |
|
72
|
36 |
|
public function relations(\Closure $filter = null) { |
|
73
|
36 |
|
if ($filter !== null) { |
|
74
|
17 |
|
return array_values(array_filter($this->relations, $filter)); |
|
75
|
|
|
} |
|
76
|
|
|
else { |
|
77
|
24 |
|
return $this->relations; |
|
78
|
|
|
} |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
/** |
|
82
|
|
|
* Get all related nodes, where relations might be filtered. |
|
83
|
|
|
* |
|
84
|
|
|
* @param \Closure|null $filter |
|
85
|
|
|
* @return Node[] |
|
86
|
|
|
*/ |
|
87
|
11 |
|
public function related_nodes(\Closure $filter = null) { |
|
88
|
11 |
|
$filtered = $this->relations($filter); |
|
89
|
|
|
$get_node = function($r) { return $r->target(); }; |
|
90
|
11 |
|
return array_values(array_map($get_node, $filtered)); |
|
91
|
|
|
} |
|
92
|
|
|
} |
|
93
|
|
|
|