1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the LightSAML-Core package. |
5
|
|
|
* |
6
|
|
|
* (c) Milos Tomic <[email protected]> |
7
|
|
|
* |
8
|
|
|
* This source file is subject to the MIT license that is bundled |
9
|
|
|
* with this source code in the file LICENSE. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace LightSaml\Model\Metadata; |
13
|
|
|
|
14
|
|
|
use LightSaml\Error\LightSamlXmlException; |
15
|
|
|
use LightSaml\Model\AbstractSamlModel; |
16
|
|
|
use LightSaml\Model\Context\DeserializationContext; |
17
|
|
|
use LightSaml\Model\SamlElementInterface; |
18
|
|
|
use LightSaml\SamlConstants; |
19
|
|
|
|
20
|
|
|
abstract class Metadata extends AbstractSamlModel |
21
|
|
|
{ |
22
|
|
|
/** |
23
|
|
|
* @param string $path |
24
|
|
|
* |
25
|
|
|
* @return EntitiesDescriptor|EntityDescriptor |
26
|
|
|
*/ |
27
|
2 |
|
public static function fromFile($path) |
28
|
|
|
{ |
29
|
2 |
|
$deserializatonContext = new DeserializationContext(); |
30
|
2 |
|
$xml = file_get_contents($path); |
31
|
|
|
|
32
|
2 |
|
return self::fromXML($xml, $deserializatonContext); |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @param string $xml |
37
|
|
|
* @param DeserializationContext $context |
38
|
|
|
* |
39
|
|
|
* @return EntityDescriptor|EntitiesDescriptor |
40
|
|
|
* |
41
|
|
|
* @throws \Exception |
42
|
|
|
*/ |
43
|
2 |
|
public static function fromXML($xml, DeserializationContext $context) |
44
|
|
|
{ |
45
|
2 |
|
if (false == is_string($xml)) { |
|
|
|
|
46
|
|
|
throw new \InvalidArgumentException('Expecting string'); |
47
|
|
|
} |
48
|
|
|
|
49
|
2 |
|
$context->getDocument()->loadXML($xml); |
50
|
|
|
|
51
|
2 |
|
$node = $context->getDocument()->firstChild; |
52
|
2 |
|
while ($node && $node instanceof \DOMComment) { |
53
|
|
|
$node = $node->nextSibling; |
54
|
|
|
} |
55
|
2 |
|
if (null === $node) { |
56
|
|
|
throw new LightSamlXmlException('Empty XML'); |
57
|
|
|
} |
58
|
|
|
|
59
|
2 |
|
if (SamlConstants::NS_METADATA !== $node->namespaceURI) { |
60
|
|
|
throw new LightSamlXmlException(sprintf( |
61
|
|
|
"Invalid namespace '%s' of the root XML element, expected '%s'", |
62
|
|
|
$node->namespaceURI, |
63
|
|
|
SamlConstants::NS_METADATA |
64
|
|
|
)); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
$map = array( |
68
|
2 |
|
'EntityDescriptor' => '\LightSaml\Model\Metadata\EntityDescriptor', |
69
|
|
|
'EntitiesDescriptor' => '\LightSaml\Model\Metadata\EntitiesDescriptor', |
70
|
|
|
); |
71
|
|
|
|
72
|
2 |
|
$rootElementName = $node->localName; |
73
|
|
|
|
74
|
2 |
View Code Duplication |
if (array_key_exists($rootElementName, $map)) { |
|
|
|
|
75
|
2 |
|
if ($class = $map[$rootElementName]) { |
76
|
|
|
/** @var SamlElementInterface $result */ |
77
|
2 |
|
$result = new $class(); |
78
|
|
|
} else { |
79
|
2 |
|
throw new \LogicException('Deserialization of %s root element is not implemented'); |
80
|
|
|
} |
81
|
|
|
} else { |
82
|
|
|
throw new LightSamlXmlException(sprintf("Unknown SAML metadata '%s'", $rootElementName)); |
83
|
|
|
} |
84
|
|
|
|
85
|
2 |
|
$result->deserialize($node, $context); |
86
|
|
|
|
87
|
2 |
|
return $result; |
88
|
|
|
} |
89
|
|
|
} |
90
|
|
|
|
When comparing two booleans, it is generally considered safer to use the strict comparison operator.