Esempio n. 1
0
 /**
  * @dataProvider provideNodes
  */
 public function testNodes($seek, $expectedDump)
 {
     while ($seek--) {
         $this->reader->read();
     }
     $this->assertDumpMatchesFormat($expectedDump, $this->reader);
 }
 protected function buildMsisdnFormat(XmlReader $xml, $country)
 {
     $formats = array();
     $prefix = null;
     $nationalDialingPrefix = null;
     $exampleMobile = null;
     $missingAttributeMessage = "No msisdn '%s' attribute found for country node '{$country}' in configuration file '" . self::$msisdnFormatConfigFilename . "'";
     // save the prefix
     if (!$xml->moveToAttribute('prefix')) {
         throw new MissingFormatConfigurationException(sprintf($missingAttributeMessage, 'prefix'));
     }
     $prefix = $xml->value;
     // save the national dialing prefix
     if (!$xml->moveToAttribute('nationalDialingPrefix')) {
         throw new MissingFormatConfigurationException(sprintf($missingAttributeMessage, 'nationalDialingPrefix'));
     }
     $nationalDialingPrefix = $xml->value;
     // save the example mobile number
     if (!$xml->moveToAttribute('exampleMobile')) {
         throw new MissingFormatConfigurationException(sprintf($missingAttributeMessage, 'exampleMobile'));
     }
     $exampleMobile = $xml->value;
     if ('' === trim($exampleMobile)) {
         throw new MissingFormatConfigurationException("Attribute 'exampleMobile was blank for country ndoe '{$country}' in configuration file '" . self::$msisdnFormatConfigFilename . "'");
     }
     // read everything, stopping at the format entities
     while ($xml->read()) {
         // we found a format entity
         if (XMLReader::ELEMENT === $xml->nodeType && 'format' === $xml->name) {
             // grab the regular expression
             $xml->moveToAttribute('expression');
             $formats[] = '/^' . $xml->value . '$/';
             continue;
         }
         // break to outer loop if we've advanced beyond the current country
         if ('country' === $xml->name) {
             break;
         }
     }
     if (!count($formats)) {
         throw new MissingFormatConfigurationException("No 'format' node(s) present for country node for '{$country}' within configuration file '" . self::$msisdnFormatConfigFilename . "'.");
     }
     $msisdnFormat = new MsisdnFormat();
     $msisdnFormat->setCountry($country);
     $msisdnFormat->setInternationalPrefix($prefix);
     $msisdnFormat->setNationalDialingPrefix($nationalDialingPrefix);
     $msisdnFormat->setExampleMobile($exampleMobile);
     $msisdnFormat->setFormats($formats);
     return $msisdnFormat;
 }
Esempio n. 3
0
 public function readXml(\XmlReader $xml, $class)
 {
     $xml->read();
     return $this->_readObject($xml, $class, true);
     // TODO check end of file?
 }
Esempio n. 4
0
 public function generateSql($xmlDump, $sqlOutput = false)
 {
     if ($sqlOutput) {
         if (file_exists($sqlOutput)) {
             throw new \Exception('SQL output file exists');
         }
         $output = fopen($sqlOutput, 'w');
         if (!$output) {
             throw new \Exception('Unable to create SQL output file');
         }
     } else {
         $output = false;
     }
     // Load the XML file for reading and advance to the first element.
     $xmlReader = new \XmlReader();
     $xmlReader->open($xmlDump);
     $xmlReader->read();
     /**
      * If the dump is properly formed, the first element should be a
      * mysqldump node.
      */
     if ($xmlReader->name != 'mysqldump') {
         throw new MysqlDumpException('mysqldump node not present');
     }
     /**
      * Iterate through the available XML nodes, while there's still
      * something to read.
      */
     while ($xmlReader->read()) {
         // If this isn't the start of a node, we don't care.  Next.
         if ($xmlReader->nodeType != \XmlReader::ELEMENT) {
             continue;
         }
         switch ($xmlReader->name) {
             case 'database':
                 // Attempt to pull the database name we should be using.
                 $database = $xmlReader->getAttribute('name');
                 if (empty($database)) {
                     throw new MysqlDumpException('No database name');
                 }
                 // Generate that SQL.
                 $createDatabase = sprintf('create database if not exists `%s`;', $database);
                 $this->doOutput("{$createDatabase}\n", $output);
                 $this->doOutput("use `{$database}`;\n", $output);
                 unset($database, $createDatabase);
                 break;
             case 'table_structure':
                 $tableStructure = simplexml_load_string($xmlReader->readOuterXML(), 'Initvector\\Xml2Mysql\\XmlNode\\TableStructure');
                 $this->doOutput($tableStructure->getCreateTable() . "\n", $output);
                 /**
                  * All children for this node have been processed, so we
                  * can safely skip to the next sibling.
                  */
                 $xmlReader->next();
                 unset($tableStructure);
                 break;
             case 'table_data':
                 $table = $xmlReader->getAttribute('name');
                 if (empty($table)) {
                     throw new MysqlDumpException('No table name');
                 }
                 $xmlReader->read();
                 $columns = array();
                 $rowBuffer = array();
                 while ($xmlReader->nodeType != \XMLReader::END_ELEMENT) {
                     if ($xmlReader->name == 'row') {
                         $tableRow = $tableStructure = simplexml_load_string($xmlReader->readOuterXML(), 'Initvector\\Xml2Mysql\\XmlNode\\Row');
                         $rowData = $tableRow->getData();
                         if (empty($columns)) {
                             $columns = array_keys($rowData);
                         }
                         $rowBuffer[] = $rowData;
                         if ($this->outputInsert($table, $columns, $rowBuffer, $output)) {
                             $rowBuffer = array();
                         }
                     }
                     $xmlReader->next();
                 }
                 $this->outputInsert($table, $columns, $rowBuffer, $output, true);
                 break;
         }
     }
     // Close the MySQL XML dump file
     $xmlReader->close();
 }