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