1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* rdfa-lite |
5
|
|
|
* |
6
|
|
|
* @category Jkphl |
7
|
|
|
* @package Jkphl\Rdfalite |
8
|
|
|
* @subpackage Jkphl\Rdfalite\Domain |
9
|
|
|
* @author Joschi Kuphal <[email protected]> / @jkphl |
10
|
|
|
* @copyright Copyright © 2017 Joschi Kuphal <[email protected]> / @jkphl |
11
|
|
|
* @license http://opensource.org/licenses/MIT The MIT License (MIT) |
12
|
|
|
*/ |
13
|
|
|
|
14
|
|
|
/*********************************************************************************** |
15
|
|
|
* The MIT License (MIT) |
16
|
|
|
* |
17
|
|
|
* Copyright © 2017 Joschi Kuphal <[email protected]> / @jkphl |
18
|
|
|
* |
19
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy of |
20
|
|
|
* this software and associated documentation files (the "Software"), to deal in |
21
|
|
|
* the Software without restriction, including without limitation the rights to |
22
|
|
|
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of |
23
|
|
|
* the Software, and to permit persons to whom the Software is furnished to do so, |
24
|
|
|
* subject to the following conditions: |
25
|
|
|
* |
26
|
|
|
* The above copyright notice and this permission notice shall be included in all |
27
|
|
|
* copies or substantial portions of the Software. |
28
|
|
|
* |
29
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
30
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS |
31
|
|
|
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR |
32
|
|
|
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER |
33
|
|
|
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN |
34
|
|
|
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
35
|
|
|
***********************************************************************************/ |
36
|
|
|
|
37
|
|
|
namespace Jkphl\Rdfalite\Domain\Property; |
38
|
|
|
|
39
|
|
|
use Jkphl\Rdfalite\Domain\Exceptions\RuntimeException; |
40
|
|
|
use Jkphl\Rdfalite\Domain\Vocabulary\VocabularyInterface; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Property |
44
|
|
|
* |
45
|
|
|
* @package Jkphl\Rdfalite |
46
|
|
|
* @subpackage Jkphl\Rdfalite\Domain |
47
|
|
|
*/ |
48
|
|
|
class Property implements PropertyInterface |
49
|
|
|
{ |
50
|
|
|
/** |
51
|
|
|
* Property name |
52
|
|
|
* |
53
|
|
|
* @var string |
54
|
|
|
*/ |
55
|
|
|
protected $name; |
56
|
|
|
/** |
57
|
|
|
* Vocabulary |
58
|
|
|
* |
59
|
|
|
* @var VocabularyInterface |
60
|
|
|
*/ |
61
|
|
|
protected $vocabulary; |
62
|
|
|
/** |
63
|
|
|
* Property value |
64
|
|
|
* |
65
|
|
|
* @var string |
66
|
|
|
*/ |
67
|
|
|
protected $value; |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* Property constructor |
71
|
|
|
* |
72
|
|
|
* @param string $name Property name |
73
|
|
|
* @param VocabularyInterface $vocabulary Property vocabulary |
74
|
|
|
* @param string $value Property value |
75
|
|
|
*/ |
76
|
3 |
|
public function __construct($name, VocabularyInterface $vocabulary, $value) |
77
|
|
|
{ |
78
|
3 |
|
$this->name = self::validatePropertyName($name); |
79
|
2 |
|
$this->vocabulary = $vocabulary; |
80
|
2 |
|
$this->value = $value; |
81
|
2 |
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* Validate a property name |
85
|
|
|
* |
86
|
|
|
* @param string $name Property name |
87
|
|
|
* @return string Sanitized property name |
88
|
|
|
* @throws RuntimeException If the property name is invalid |
89
|
|
|
*/ |
90
|
4 |
|
public static function validatePropertyName($name) |
91
|
|
|
{ |
92
|
4 |
|
$name = trim($name); |
93
|
|
|
|
94
|
|
|
// If the property name is invalid |
95
|
4 |
|
if (!strlen($name) || !preg_match('/^[a-z][a-zA-Z0-9]*$/', $name)) { |
96
|
1 |
|
throw new RuntimeException( |
97
|
1 |
|
sprintf(RuntimeException::INVALID_PROPERTY_NAME_STR, $name), RuntimeException::INVALID_PROPERTY_NAME |
98
|
1 |
|
); |
99
|
|
|
} |
100
|
|
|
|
101
|
3 |
|
return $name; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* Return the property name |
106
|
|
|
* |
107
|
|
|
* @return string Property name |
108
|
|
|
*/ |
109
|
2 |
|
public function getName() |
110
|
|
|
{ |
111
|
2 |
|
return $this->name; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* Return the property vocabulary |
116
|
|
|
* |
117
|
|
|
* @return VocabularyInterface Property vocabulary |
118
|
|
|
*/ |
119
|
1 |
|
public function getVocabulary() |
120
|
|
|
{ |
121
|
1 |
|
return $this->vocabulary; |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* Property value |
126
|
|
|
* |
127
|
|
|
* @return string Property value |
128
|
|
|
*/ |
129
|
1 |
|
public function getValue() |
130
|
|
|
{ |
131
|
1 |
|
return $this->value; |
132
|
|
|
} |
133
|
|
|
} |
134
|
|
|
|