-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathAbstractAdapter.php
53 lines (44 loc) · 1.43 KB
/
AbstractAdapter.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
<?php
namespace Magium\Configuration\File;
abstract class AbstractAdapter implements AdapterInterface
{
protected $file;
public function __construct(
$file
)
{
$this->file = realpath($file);
if ($this->file === false) {
throw new InvalidFileException('Unable to find file: ' . $file);
}
}
/*
* @return string The location of the file
*/
public function getFile()
{
return $this->file;
}
abstract protected function configureSchema(\DOMElement $element);
/**
* @param \DOMDocument $doc
* @throws InvalidFileStructureException
*/
protected function validateSchema(\DOMDocument $doc)
{
try {
$element = $doc->firstChild;
if (!$element instanceof \DOMElement) {
// This is more for code completion. If the first child is not a DOMElement, PHP is probably broken
throw new \Exception('Invalid XML file');
}
$schema = $this->configureSchema($element);
$out = $doc->saveXML();
$validateDoc = new \DOMDocument();
$validateDoc->loadXML($out);
$validateDoc->schemaValidate($schema);
} catch (\Exception $e) {
throw new InvalidFileStructureException(sprintf('Unable to load file %s due to exception: %s', $this->getFile(), $e->getMessage()), $e->getCode(), $e);
}
}
}