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
|
|
|
* An entity in the graph has a type and stores key value pairs as properties. |
15
|
|
|
* It will be concretized to a node or a relation. |
16
|
|
|
*/ |
17
|
|
|
abstract class Entity { |
18
|
|
|
/** |
19
|
|
|
* @var string |
20
|
|
|
*/ |
21
|
|
|
private $type; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @var array<string,mixed>|null |
25
|
|
|
*/ |
26
|
|
|
private $properties = []; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @param string $type |
30
|
|
|
* @param array<string,mixed>|null $properties |
31
|
|
|
*/ |
32
|
91 |
|
public function __construct($type, array $properties = null) { |
33
|
91 |
|
assert('is_string($type)'); |
34
|
91 |
|
$this->type = $type; |
35
|
91 |
|
if ($properties !== null) { |
36
|
90 |
|
foreach ($properties as $key => $value) { |
37
|
65 |
|
$this->set_property($key, $value); |
38
|
90 |
|
} |
39
|
90 |
|
} |
40
|
91 |
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Get the type. |
44
|
|
|
* |
45
|
|
|
* @return string |
46
|
|
|
*/ |
47
|
62 |
|
public function type() { |
48
|
62 |
|
return $this->type; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* Set a property. |
53
|
|
|
* |
54
|
|
|
* @param string $key |
55
|
|
|
* @param mixed $value |
56
|
|
|
* @return null |
57
|
|
|
*/ |
58
|
65 |
|
private function set_property($key, $value) { |
59
|
65 |
|
assert('is_string($key)'); |
60
|
65 |
|
$this->properties[$key] = $value; |
61
|
65 |
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Get the properties. |
65
|
|
|
* |
66
|
|
|
* @return array<string,mixed> |
67
|
|
|
*/ |
68
|
8 |
|
public function properties() { |
69
|
8 |
|
if ($this->properties === null) { |
70
|
|
|
return []; |
71
|
|
|
} |
72
|
8 |
|
return $this->properties; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Get one property. |
77
|
|
|
* |
78
|
|
|
* @param string $name |
79
|
|
|
* @throws \InvalidArgumentException if named propery does not exist |
80
|
|
|
* @return mixed |
81
|
|
|
*/ |
82
|
43 |
|
public function property($name) { |
83
|
43 |
|
if (!$this->has_property($name)) { |
84
|
1 |
|
$type = $this->type; |
85
|
1 |
|
throw new \InvalidArgumentException( |
86
|
1 |
|
"Unknown property '$name' for entity with type '$type'"); |
87
|
|
|
} |
88
|
43 |
|
return $this->properties[$name]; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* Check if entity has a property. |
93
|
|
|
* |
94
|
|
|
* @param string $name |
95
|
|
|
* @return bool |
96
|
|
|
*/ |
97
|
44 |
|
public function has_property($name) { |
98
|
44 |
|
return array_key_exists($name, $this->properties); |
99
|
|
|
} |
100
|
|
|
} |
101
|
|
|
|