|
1
|
|
|
<?php |
|
2
|
|
|
declare(strict_types = 1); |
|
3
|
|
|
/** |
|
4
|
|
|
* Contains Validator class. |
|
5
|
|
|
* |
|
6
|
|
|
* PHP version 7.0+ |
|
7
|
|
|
* |
|
8
|
|
|
* LICENSE: |
|
9
|
|
|
* This file is part of Yet Another Php Eve Api Library also know as Yapeal |
|
10
|
|
|
* which can be used to access the Eve Online API data and place it into a |
|
11
|
|
|
* database. |
|
12
|
|
|
* Copyright (C) 2015-2016 Michael Cummings |
|
13
|
|
|
* |
|
14
|
|
|
* This program is free software: you can redistribute it and/or modify it |
|
15
|
|
|
* under the terms of the GNU Lesser General Public License as published by the |
|
16
|
|
|
* Free Software Foundation, either version 3 of the License, or (at your |
|
17
|
|
|
* option) any later version. |
|
18
|
|
|
* |
|
19
|
|
|
* This program is distributed in the hope that it will be useful, but WITHOUT |
|
20
|
|
|
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
21
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License |
|
22
|
|
|
* for more details. |
|
23
|
|
|
* |
|
24
|
|
|
* You should have received a copy of the GNU Lesser General Public License |
|
25
|
|
|
* along with this program. If not, see |
|
26
|
|
|
* <http://spdx.org/licenses/LGPL-3.0.html>. |
|
27
|
|
|
* |
|
28
|
|
|
* You should be able to find a copy of this license in the COPYING-LESSER.md |
|
29
|
|
|
* file. A copy of the GNU GPL should also be available in the COPYING.md file. |
|
30
|
|
|
* |
|
31
|
|
|
* @copyright 2015-2016 Michael Cummings |
|
32
|
|
|
* @license http://www.gnu.org/copyleft/lesser.html GNU LGPL |
|
33
|
|
|
* @author Michael Cummings <[email protected]> |
|
34
|
|
|
*/ |
|
35
|
|
|
namespace Yapeal\Xsd; |
|
36
|
|
|
|
|
37
|
|
|
use Yapeal\Event\EveApiEventEmitterTrait; |
|
38
|
|
|
use Yapeal\Event\EveApiEventInterface; |
|
39
|
|
|
use Yapeal\Event\MediatorInterface; |
|
40
|
|
|
use Yapeal\Exception\YapealFileSystemException; |
|
41
|
|
|
use Yapeal\FileSystem\RelativeFileSearchTrait; |
|
42
|
|
|
use Yapeal\Log\Logger; |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* Class Validator |
|
46
|
|
|
*/ |
|
47
|
|
|
class Validator |
|
48
|
|
|
{ |
|
49
|
|
|
use EveApiEventEmitterTrait, RelativeFileSearchTrait; |
|
50
|
|
|
/** |
|
51
|
|
|
* Constructor. |
|
52
|
|
|
* |
|
53
|
|
|
* @param string $dir Base directory where Eve API XSD files can be found. |
|
54
|
|
|
*/ |
|
55
|
|
|
public function __construct($dir = __DIR__) |
|
56
|
|
|
{ |
|
57
|
|
|
$this->setRelativeBaseDir($dir . '/'); |
|
58
|
|
|
} |
|
59
|
|
|
/** |
|
60
|
|
|
* @param EveApiEventInterface $event |
|
61
|
|
|
* @param string $eventName |
|
62
|
|
|
* @param MediatorInterface $yem |
|
63
|
|
|
* |
|
64
|
|
|
* @return EveApiEventInterface |
|
65
|
|
|
* @throws \DomainException |
|
66
|
|
|
* @throws \InvalidArgumentException |
|
67
|
|
|
* @throws \LogicException |
|
68
|
|
|
*/ |
|
69
|
|
|
public function validateEveApi(EveApiEventInterface $event, $eventName, MediatorInterface $yem) |
|
70
|
|
|
{ |
|
71
|
|
|
$this->setYem($yem); |
|
72
|
|
|
$data = $event->getData(); |
|
73
|
|
|
$yem->triggerLogEvent('Yapeal.Log.log', |
|
74
|
|
|
Logger::DEBUG, |
|
75
|
|
|
$this->getReceivedEventMessage($data, $eventName, __CLASS__)); |
|
76
|
|
|
if (false === $data->getEveApiXml()) { |
|
77
|
|
|
return $event; |
|
78
|
|
|
} |
|
79
|
|
|
$htmlError = strpos($data->getEveApiXml(), '<!DOCTYPE html'); |
|
80
|
|
|
if (false !== $htmlError) { |
|
81
|
|
|
$mess = 'Received HTML result from '; |
|
82
|
|
|
$yem->triggerLogEvent('Yapeal.Log.log', Logger::NOTICE, $this->createEveApiMessage($mess, $data)); |
|
83
|
|
|
$apiName = $data->getEveApiName(); |
|
84
|
|
|
$data->setEveApiName('Invalid_' . $apiName); |
|
85
|
|
|
// Cache error html. |
|
86
|
|
|
$this->emitEvents($data, 'preserve', 'Yapeal.Xml.Error'); |
|
87
|
|
|
$data->setEveApiName($apiName); |
|
88
|
|
|
return $event->setData($data); |
|
89
|
|
|
} |
|
90
|
|
|
try { |
|
91
|
|
|
$xsdName = $this->findRelativeFileWithPath(ucfirst($data->getEveApiSectionName()), |
|
92
|
|
|
$data->getEveApiName(), |
|
93
|
|
|
'xsd'); |
|
94
|
|
|
} catch (YapealFileSystemException $exc) { |
|
95
|
|
|
$mess = 'Failed to find accessible XSD schema file during'; |
|
96
|
|
|
$yem->triggerLogEvent('Yapeal.Log.log', |
|
97
|
|
|
Logger::WARNING, |
|
98
|
|
|
$this->createEventMessage($mess, $data, $eventName), |
|
99
|
|
|
['exception' => $exc]); |
|
100
|
|
|
return $event; |
|
101
|
|
|
} |
|
102
|
|
|
$mess = sprintf('Using %1$s file to validate', $xsdName); |
|
103
|
|
|
$yem->triggerLogEvent('Yapeal.Log.log', Logger::DEBUG, $this->createEveApiMessage($mess, $data)); |
|
104
|
|
|
libxml_clear_errors(); |
|
105
|
|
|
libxml_use_internal_errors(true); |
|
106
|
|
|
libxml_clear_errors(); |
|
107
|
|
|
$dom = new \DOMDocument(); |
|
108
|
|
|
$loaded = $dom->loadXML($data->getEveApiXml()); |
|
109
|
|
|
if (!$loaded || !$dom->schemaValidate($xsdName)) { |
|
110
|
|
|
/** |
|
111
|
|
|
* @var \libXMLError[] $errors |
|
112
|
|
|
*/ |
|
113
|
|
|
$errors = libxml_get_errors(); |
|
114
|
|
|
if (0 !== count($errors)) { |
|
115
|
|
|
foreach ($errors as $error) { |
|
116
|
|
|
if (null !== $error->message) { |
|
117
|
|
|
$yem->triggerLogEvent('Yapeal.Log.log', Logger::NOTICE, $error->message); |
|
118
|
|
|
} |
|
119
|
|
|
} |
|
120
|
|
|
} |
|
121
|
|
|
libxml_clear_errors(); |
|
122
|
|
|
libxml_use_internal_errors(false); |
|
123
|
|
|
libxml_clear_errors(); |
|
124
|
|
|
$apiName = $data->getEveApiName(); |
|
125
|
|
|
$data->setEveApiName('Invalid_' . $apiName); |
|
126
|
|
|
// Cache error causing XML. |
|
127
|
|
|
$this->emitEvents($data, 'preserve', 'Yapeal.Xml.Error'); |
|
128
|
|
|
$data->setEveApiName($apiName); |
|
129
|
|
|
$event->setData($data); |
|
130
|
|
|
return $event->setHandledSufficiently(false); |
|
131
|
|
|
} |
|
132
|
|
|
libxml_clear_errors(); |
|
133
|
|
|
libxml_use_internal_errors(false); |
|
134
|
|
|
libxml_clear_errors(); |
|
135
|
|
|
// Check for XML error element. |
|
136
|
|
|
if (false !== strpos($data->getEveApiXml(), '<error ')) { |
|
137
|
|
|
$this->emitEvents($data, 'start', 'Yapeal.Xml.Error'); |
|
138
|
|
|
$event->setData($data); |
|
139
|
|
|
return $event->setHandledSufficiently(false); |
|
140
|
|
|
} |
|
141
|
|
|
return $event->setHandledSufficiently(); |
|
142
|
|
|
} |
|
143
|
|
|
} |
|
144
|
|
|
|