1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace OpenConext\Value\Saml; |
4
|
|
|
|
5
|
|
|
use OpenConext\Value\Exception\InvalidArgumentException; |
6
|
|
|
|
7
|
|
|
final class Entity |
8
|
|
|
{ |
9
|
|
|
/** |
10
|
|
|
* @var EntityId |
11
|
|
|
*/ |
12
|
|
|
private $entityId; |
13
|
|
|
/** |
14
|
|
|
* @var EntityType |
15
|
|
|
*/ |
16
|
|
|
private $entityType; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* @param array $descriptor array('entity-id', 'sp or idp') |
20
|
|
|
* @return Entity |
21
|
|
|
*/ |
22
|
|
|
public static function fromDescriptor(array $descriptor) |
23
|
|
|
{ |
24
|
|
|
if (count($descriptor) !== 2) { |
25
|
|
|
throw new InvalidArgumentException( |
26
|
|
|
'EntityDescriptor must be an array with two elements (both a string), the first must be the EntityId,' |
27
|
|
|
. ' the second the EntityType' |
28
|
|
|
); |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
switch ($descriptor[1]) { |
32
|
|
|
case 'sp': |
33
|
|
|
return new Entity(new EntityId($descriptor[0]), EntityType::SP()); |
34
|
|
|
case 'idp': |
35
|
|
|
return new Entity(new EntityId($descriptor[0]), EntityType::IdP()); |
36
|
|
|
default: |
37
|
|
|
throw new InvalidArgumentException('Entity descriptor type is neither "sp" nor "idp"'); |
38
|
|
|
} |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
public function __construct(EntityId $entityId, EntityType $entityType) |
42
|
|
|
{ |
43
|
|
|
$this->entityId = $entityId; |
44
|
|
|
$this->entityType = $entityType; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @param EntityId $entityId |
49
|
|
|
* @return bool |
50
|
|
|
*/ |
51
|
|
|
public function hasEntityId(EntityId $entityId) |
52
|
|
|
{ |
53
|
|
|
return $this->entityId->equals($entityId); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @return bool |
58
|
|
|
*/ |
59
|
|
|
public function isServiceProvider() |
60
|
|
|
{ |
61
|
|
|
return $this->entityType->isServiceProvider(); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* @return bool |
66
|
|
|
*/ |
67
|
|
|
public function isIdentityProvider() |
68
|
|
|
{ |
69
|
|
|
return $this->entityType->isIdentityProvider(); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* @return EntityId |
74
|
|
|
*/ |
75
|
|
|
public function getEntityId() |
76
|
|
|
{ |
77
|
|
|
return $this->entityId; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* @return EntityType |
82
|
|
|
*/ |
83
|
|
|
public function getEntityType() |
84
|
|
|
{ |
85
|
|
|
return $this->entityType; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* @param Entity $other |
90
|
|
|
* @return bool |
91
|
|
|
*/ |
92
|
|
|
public function equals(Entity $other) |
93
|
|
|
{ |
94
|
|
|
return $this->entityId->equals($other->entityId) && $this->entityType->equals($other->entityType); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
public function __toString() |
98
|
|
|
{ |
99
|
|
|
return $this->entityId . ' (' . $this->entityType . ')'; |
100
|
|
|
} |
101
|
|
|
} |
102
|
|
|
|