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