e1a8aafb0c67dbf94a23bdf6c12c827f9cda2a69
[yaffs-website] / web / modules / contrib / migrate_plus / src / Plugin / migrate_plus / data_parser / XmlTrait.php
1 <?php
2
3 namespace Drupal\migrate_plus\Plugin\migrate_plus\data_parser;
4
5 /**
6  * Common functionality for XML data parsers.
7  */
8 trait XmlTrait {
9
10   /**
11    * Registers the iterator's namespaces to a SimpleXMLElement.
12    *
13    * @param \SimpleXMLElement $xml
14    *   The element to apply namespace registrations to.
15    */
16   protected function registerNamespaces(\SimpleXMLElement $xml) {
17     if (isset($this->configuration['namespaces']) && is_array($this->configuration['namespaces'])) {
18       foreach ($this->configuration['namespaces'] as $prefix => $ns) {
19         $xml->registerXPathNamespace($prefix, $ns);
20       }
21     }
22   }
23
24   /**
25    * Parses a LibXMLError to a error message string.
26    *
27    * @param \LibXMLError $error
28    *   Error thrown by the XML.
29    *
30    * @return string
31    *   Error message
32    */
33   public static function parseLibXmlError(\LibXMLError $error) {
34     $error_code_name = 'Unknown Error';
35     switch ($error->level) {
36       case LIBXML_ERR_WARNING:
37         $error_code_name = t('Warning');
38         break;
39
40       case LIBXML_ERR_ERROR:
41         $error_code_name = t('Error');
42         break;
43
44       case LIBXML_ERR_FATAL:
45         $error_code_name = t('Fatal Error');
46         break;
47     }
48
49     return t(
50       "@libxmlerrorcodename @libxmlerrorcode: @libxmlerrormessage\n" .
51       "Line: @libxmlerrorline\n" .
52       "Column: @libxmlerrorcolumn\n" .
53       "File: @libxmlerrorfile",
54       [
55         '@libxmlerrorcodename' => $error_code_name,
56         '@libxmlerrorcode' => $error->code,
57         '@libxmlerrormessage' => trim($error->message),
58         '@libxmlerrorline' => $error->line,
59         '@libxmlerrorcolumn' => $error->column,
60         '@libxmlerrorfile' => (($error->file)) ? $error->file : NULL,
61       ]
62     );
63   }
64
65 }