1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace alsvanzelf\jsonapi\helpers; |
4
|
|
|
|
5
|
|
|
use alsvanzelf\jsonapi\exceptions\InputException; |
6
|
|
|
use alsvanzelf\jsonapi\helpers\Validator; |
7
|
|
|
use alsvanzelf\jsonapi\interfaces\ProfileInterface; |
8
|
|
|
use alsvanzelf\jsonapi\objects\ProfileLinkObject; |
9
|
|
|
|
10
|
|
|
abstract class ProfileAliasManager { |
11
|
|
|
/** @var array */ |
12
|
|
|
private $aliasMapping = []; |
13
|
|
|
/** @var array */ |
14
|
|
|
private $keywordMapping = []; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* ProfileInterface |
18
|
|
|
*/ |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @inheritDoc |
22
|
|
|
*/ |
23
|
|
|
public function __construct(array $aliases=[]) { |
24
|
|
|
$officialKeywords = $this->getOfficialKeywords(); |
25
|
|
|
if ($officialKeywords === []) { |
26
|
|
|
return; |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
$this->keywordMapping = array_combine($officialKeywords, $officialKeywords); |
30
|
|
|
if ($aliases === []) { |
31
|
|
|
return; |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
foreach ($aliases as $keyword => $alias) { |
35
|
|
|
if ($alias === $keyword) { |
36
|
|
|
throw new InputException('an alias should be different from its keyword'); |
37
|
|
|
} |
38
|
|
|
if (in_array($keyword, $officialKeywords, $strict=true) === false) { |
39
|
|
|
throw new InputException('unknown keyword "'.$keyword.'" to alias'); |
40
|
|
|
} |
41
|
|
|
Validator::checkMemberName($alias); |
42
|
|
|
|
43
|
|
|
$this->keywordMapping[$keyword] = $alias; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
$this->aliasMapping = $aliases; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @inheritDoc |
51
|
|
|
*/ |
52
|
|
|
public function getKeyword($keyword) { |
53
|
|
|
if (isset($this->keywordMapping[$keyword]) === false) { |
54
|
|
|
throw new InputException('unknown keyword "'.$keyword.'"'); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
return $this->keywordMapping[$keyword]; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @inheritDoc |
62
|
|
|
*/ |
63
|
|
|
abstract public function getOfficialKeywords(); |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* @inheritDoc |
67
|
|
|
*/ |
68
|
|
|
abstract public function getOfficialLink(); |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* @inheritDoc |
72
|
|
|
*/ |
73
|
|
|
public function getAliasedLink() { |
74
|
|
|
if ($this->aliasMapping === []) { |
75
|
|
|
return $this->getOfficialLink(); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
return new ProfileLinkObject($this->getOfficialLink(), $this->aliasMapping); |
79
|
|
|
} |
80
|
|
|
} |
81
|
|
|
|