Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
XmlValidator.php
Go to the documentation of this file.
1 <?php
8 
10 
15 {
19  private $xmlSecurity;
20 
24  private $errorProcessor;
25 
32  public function __construct(
33  \Magento\Framework\Xml\Security $xmlSecurity,
34  ResponseErrorProcessor $errorProcessor
35  ) {
36  $this->xmlSecurity = $xmlSecurity;
37  $this->errorProcessor = $errorProcessor;
38  }
39 
48  public function validate($xmlResponse, $isShippingLabel = false)
49  {
50  if (strlen(trim($xmlResponse)) > 0 && strpos(trim($xmlResponse), '<?xml') === 0) {
51  if (!$this->xmlSecurity->scan($xmlResponse)) {
52  throw new DocumentValidationException(__('The security validation of the XML document has failed.'));
53  }
54  $xml = simplexml_load_string($xmlResponse, \Magento\Shipping\Model\Simplexml\Element::class);
55 
56  if (in_array($xml->getName(), ['ErrorResponse', 'ShipmentValidateErrorResponse'])
57  || isset($xml->GetQuoteResponse->Note->Condition)
58  ) {
60  $exceptionPhrase = $this->errorProcessor->process($xml, $isShippingLabel);
61  throw new DocumentValidationException($exceptionPhrase, null, $exceptionPhrase->getArguments()[0]);
62  }
63  } else {
64  throw new DocumentValidationException(__('The response is in the wrong format'));
65  }
66  }
67 }
__()
Definition: __.php:13
__construct(\Magento\Framework\Xml\Security $xmlSecurity, ResponseErrorProcessor $errorProcessor)