/**
  *
  * @param mixed $xml
  * @return \Zend\Code\Generator\ClassGenerator[]
  */
 protected function getPhpClasses($xml, array $types = array())
 {
     $creator = new PhpConverter(new ShortNamingStrategy());
     $creator->addNamespace('', 'Example');
     foreach ($types as $typeData) {
         list($ns, $name, $type) = $typeData;
         $creator->addAliasMapType($ns, $name, $type);
     }
     $generator = new ClassGenerator();
     $reader = new SchemaReader();
     if (!is_array($xml)) {
         $xml = ['schema.xsd' => $xml];
     }
     $schemas = [];
     foreach ($xml as $name => $str) {
         $schemas[] = $reader->readString($str, $name);
     }
     $items = $creator->convert($schemas);
     $classes = array();
     foreach ($items as $k => $item) {
         if ($codegen = $generator->generate($item)) {
             $classes[$k] = $codegen;
         }
     }
     return $classes;
 }
Example #2
0
 public function testMissingClass()
 {
     $expectedItems = array('Epa\\Schema\\AdditionalIdentifier', 'Epa\\Schema\\AdditionalIdentifierType', 'Epa\\Schema\\AdditionalIdentifierTypes', 'Epa\\Schema\\AdditionalIdentifiers');
     $expectedItems = array_combine($expectedItems, $expectedItems);
     $reader = new SchemaReader();
     $schema = $reader->readFile(__DIR__ . '/data.xsd');
     $yamlConv = new YamlConverter(new ShortNamingStrategy());
     $yamlConv->addNamespace('', 'Epa\\Schema');
     $yamlItems = $yamlConv->convert([$schema]);
     $this->assertCount(count($expectedItems), $yamlItems);
     $this->assertEmpty(array_diff_key($expectedItems, $yamlItems));
     $phpConv = new PhpConverter(new ShortNamingStrategy());
     $phpConv->addNamespace('', 'Epa\\Schema');
     $phpClasses = $phpConv->convert([$schema]);
     $this->assertCount(count($expectedItems), $phpClasses);
     $this->assertEmpty(array_diff_key($expectedItems, $phpClasses));
     $yamlClass = $yamlItems['Epa\\Schema\\AdditionalIdentifier']['Epa\\Schema\\AdditionalIdentifier'];
     $yamlProperty = $yamlClass['properties']['additionalIdentifierType'];
     /** @var PHPClass $phpClass */
     $phpClass = $phpClasses['Epa\\Schema\\AdditionalIdentifier'];
     /** @var PHPProperty $phpProperty */
     $phpProperty = $phpClass->getProperty('additionalIdentifierType');
     /** @var PHPClass $phpType */
     $phpType = $phpProperty->getType();
     $this->assertSame($yamlProperty['type'], $phpType->getFullName());
 }
Example #3
0
 public function testNaming()
 {
     $reader = new SchemaReader();
     $schema = $reader->readFile(__DIR__ . '/data.xsd');
     $phpConv = new PhpConverter(new ShortNamingStrategy());
     $phpConv->addNamespace('http://www.example.com/', 'Epa');
     $phpClasses = $phpConv->convert([$schema]);
     $this->assertEquals('convertToReseller', $phpClasses['Epa\\Two']->getProperty('convertToReseller')->getType()->getArg()->getName());
 }
Example #4
0
 public function testNaming()
 {
     $reader = new SchemaReader();
     $schema = $reader->readFile(__DIR__ . '/data.xsd');
     $phpConv = new PhpConverter(new ShortNamingStrategy());
     $phpConv->addNamespace('http://www.example.com/', 'Epa');
     $phpClasses = $phpConv->convert([$schema]);
     $this->assertArrayHasKey('Epa\\ABType', $phpClasses);
     $class = $phpClasses['Epa\\ABType'];
     $this->assertArrayHasKey('cDe', $class->getProperties());
     $this->assertArrayHasKey('fGh', $class->getProperties());
 }
Example #5
0
 public function testNamespace()
 {
     $reader = new SchemaReader();
     $schema = $reader->readFile(__DIR__ . '/data.xsd');
     $jmsConv = new YamlConverter(new ShortNamingStrategy());
     $jmsConv->addNamespace('http://www.example.com', 'Tst');
     $jmsConv->addNamespace('http://www.example2.com', 'Tst');
     $phpClasses = $jmsConv->convert([$schema]);
     $type1 = $phpClasses['Tst\\ComplexType1Type']['Tst\\ComplexType1Type'];
     $propertyElement2 = $type1['properties']['element2'];
     $this->assertEquals('http://www.example2.com', $propertyElement2['xml_element']['namespace']);
     $propertyElement1 = $type1['properties']['element1'];
     $this->assertEquals('http://www.example.com', $propertyElement1['xml_element']['namespace']);
 }
 public static function setUpBeforeClass()
 {
     if (!self::$files) {
         self::$files = self::getXmlFiles();
     }
     self::$generator = new Generator(['http://www.opentravel.org/OTA/2003/05' => self::$namespace], [['http://www.opentravel.org/OTA/2003/05', 'DateOrTimeOrDateTimeType', 'GoetasWebservices\\Xsd\\XsdToPhp\\Tests\\JmsSerializer\\OTA\\OTADateTime'], ['http://www.opentravel.org/OTA/2003/05', 'DateOrDateTimeType', 'GoetasWebservices\\Xsd\\XsdToPhp\\Tests\\JmsSerializer\\OTA\\OTADateTime'], ['http://www.opentravel.org/OTA/2003/05', 'TimeOrDateTimeType', 'GoetasWebservices\\Xsd\\XsdToPhp\\Tests\\JmsSerializer\\OTA\\OTADateTime']]);
     $reader = new SchemaReader();
     $schemas = array();
     foreach (self::$files as $d) {
         if (!isset($schemas[$d[1]])) {
             $schemas[$d[1]] = $reader->readFile($d[1]);
         }
     }
     self::$generator->generate($schemas);
     self::$generator->registerAutoloader();
 }
Example #7
0
 public function testMissingClass()
 {
     $expectedItems = array('Epa\\Job', 'Epa\\Item', 'Epa\\Item\\PriceAType');
     $reader = new SchemaReader();
     $schema = $reader->readFile(__DIR__ . '/data.xsd');
     $yamlConv = new YamlConverter(new ShortNamingStrategy());
     $yamlConv->addNamespace('http://www.trogon.si/Schemas/2010/JobXML/2.0', 'Epa');
     $yamlItems = $yamlConv->convert([$schema]);
     $yamlItems = array_keys($yamlItems);
     $this->assertEmpty(array_diff($expectedItems, $yamlItems));
     $phpConv = new PhpConverter(new ShortNamingStrategy());
     $phpConv->addNamespace('http://www.trogon.si/Schemas/2010/JobXML/2.0', 'Epa');
     $phpClasses = $phpConv->convert([$schema]);
     $phpClasses = array_keys($phpClasses);
     $this->assertEmpty(array_diff_key($expectedItems, $phpClasses));
 }
Example #8
0
 public static function setUpBeforeClass()
 {
     $tmp = sys_get_temp_dir();
     if (is_writable("/dev/shm")) {
         $tmp = "/dev/shm";
     }
     self::$phpDir = "{$tmp}/OTASerializationTestPHP";
     self::$jmsDir = "{$tmp}/OTASerializationTestJMS";
     self::$loader = new ClassLoader();
     self::$loader->addPsr4(self::$namespace . "\\", self::$phpDir);
     self::$loader->register();
     if (is_dir(self::$phpDir)) {
         self::delTree(self::$phpDir);
     }
     if (is_dir(self::$jmsDir)) {
         self::delTree(self::$jmsDir);
     }
     if (!is_dir(self::$phpDir)) {
         mkdir(self::$phpDir);
     }
     if (!is_dir(self::$jmsDir)) {
         mkdir(self::$jmsDir);
     }
     $reader = new SchemaReader();
     $schemas = array();
     foreach (self::getXmlFiles() as $d) {
         if (!isset($schemas[$d[1]])) {
             $schemas[$d[1]] = $reader->readFile($d[1]);
         }
     }
     self::generateJMSFiles($schemas);
     self::generatePHPFiles($schemas);
     $serializerBuiler = \JMS\Serializer\SerializerBuilder::create();
     $serializerBuiler->configureHandlers(function (HandlerRegistryInterface $h) use($serializerBuiler) {
         $serializerBuiler->addDefaultHandlers();
         $h->registerSubscribingHandler(new BaseTypesHandler());
         $h->registerSubscribingHandler(new XmlSchemaDateHandler());
         $h->registerSubscribingHandler(new OTASchemaDateHandler());
     });
     $serializerBuiler->addMetadataDir(self::$jmsDir, self::$namespace);
     self::$serializer = $serializerBuiler->build();
 }
 /**
  *
  * @return \oetas\XML\WSDLReader\Wsdl\Definitions
  */
 public function readNode(\DOMElement $node, $file = 'wsdl.xsd')
 {
     $this->loadedFiles[$file] = $rootDefinitions = new Definitions();
     $schema = new Schema();
     $schema->addSchema($this->reader->getGlobalSchema());
     $rootDefinitions->setSchema($schema);
     $callbacks = $this->rootNode($rootDefinitions, $node);
     foreach ($callbacks as $callback) {
         call_user_func($callback);
     }
     return $rootDefinitions;
 }
 /**
  *
  * @param mixed $xml
  * @return \Zend\Code\Generator\ClassGenerator[]
  */
 protected function getClasses($xml)
 {
     $phpcreator = new PhpConverter(new ShortNamingStrategy());
     $phpcreator->addNamespace('http://www.example.com', 'Example');
     $generator = new ClassGenerator();
     $reader = new SchemaReader();
     if (!is_array($xml)) {
         $xml = ['schema.xsd' => $xml];
     }
     $schemas = [];
     foreach ($xml as $name => $str) {
         $schemas[] = $reader->readString($str, $name);
     }
     $items = $phpcreator->convert($schemas);
     $classes = array();
     foreach ($items as $k => $item) {
         if ($codegen = $generator->generate($item)) {
             $classes[$k] = $codegen;
         }
     }
     return $classes;
 }
 /**
  * @param XsdGeneratePhpArgs $input
  * @param PhpConverter $converter
  * @param string $source
  * @return \Goetas\XML\XSDReader\Schema\Schema
  */
 private function readSource(XsdGeneratePhpArgs $input, PhpConverter $converter, $source)
 {
     $this->outputWriteLine(" + <comment>{$this->outputFormatterEscape($source)}</comment>");
     $xml = new \DOMDocument('1.0', 'UTF-8');
     if (!$xml->load($source)) {
         throw new \RuntimeException("Can't load the schema '{$source}'");
     }
     $targetNamespace = $xml->documentElement->getAttribute("targetNamespace");
     if (!$converter->isNamespaceMapped($targetNamespace)) {
         $converter->addNamespace($targetNamespace, $this->getPhpNamespace($input));
     }
     return $this->schemaReader->readFile($source);
 }
Example #12
0
 /**
  * @group long
  */
 public function testOpcGeneration()
 {
     $nss = array("http://schemas.openxmlformats.org/package/2006/metadata/core-properties" => "Iag/ECMA376/Package/Model/CoreProperties/", "http://purl.org/dc/elements/1.1/" => "Iag/ECMA376/Package/Model/CoreProperties/DcElements/", "http://purl.org/dc/terms/" => "Iag/ECMA376/Package/Model/CoreProperties/DcTerms/", "http://purl.org/dc/dcmitype/" => "Iag/ECMA376/Package/Model/CoreProperties/DcMiType/");
     $reader = new SchemaReader();
     $reader->addKnownSchemaLocation('http://dublincore.org/schemas/xmls/qdc/2003/04/02/dc.xsd', __DIR__ . '/opc/dc.xsd');
     $reader->addKnownSchemaLocation('http://dublincore.org/schemas/xmls/qdc/2003/04/02/dcterms.xsd', __DIR__ . '/opc/dcterms.xsd');
     $reader->addKnownSchemaLocation('http://dublincore.org/schemas/xmls/qdc/2003/04/02/dcterms.xsd', __DIR__ . '/opc/dcterms.xsd');
     $reader->addKnownSchemaLocation('http://dublincore.org/schemas/xmls/qdc/2003/04/02/dcmitype.xsd', __DIR__ . '/opc/dcmitype.xsd');
     $schema = $reader->readFile(__DIR__ . '/opc/opc-coreProperties.xsd');
     $generator = new Generator($nss);
     list($phpClasses, $yamlItems) = $generator->getData([$schema]);
     $this->assertEquals(count($phpClasses), count($yamlItems));
     $this->assertGreaterThan(0, count($phpClasses));
 }
Example #13
0
 /**
  * @group long
  */
 public function testOpcGeneration()
 {
     $nss = array("http://schemas.openxmlformats.org/package/2006/metadata/core-properties" => "Iag/ECMA376/Package/Model/CoreProperties/", "http://purl.org/dc/elements/1.1/" => "Iag/ECMA376/Package/Model/CoreProperties/DcElements/", "http://purl.org/dc/terms/" => "Iag/ECMA376/Package/Model/CoreProperties/DcTerms/", "http://purl.org/dc/dcmitype/" => "Iag/ECMA376/Package/Model/CoreProperties/DcMiType/");
     $reader = new SchemaReader();
     $reader->addKnownSchemaLocation('http://dublincore.org/schemas/xmls/qdc/2003/04/02/dc.xsd', __DIR__ . '/opc/dc.xsd');
     $reader->addKnownSchemaLocation('http://dublincore.org/schemas/xmls/qdc/2003/04/02/dcterms.xsd', __DIR__ . '/opc/dcterms.xsd');
     $reader->addKnownSchemaLocation('http://dublincore.org/schemas/xmls/qdc/2003/04/02/dcterms.xsd', __DIR__ . '/opc/dcterms.xsd');
     $reader->addKnownSchemaLocation('http://dublincore.org/schemas/xmls/qdc/2003/04/02/dcmitype.xsd', __DIR__ . '/opc/dcmitype.xsd');
     $schema = $reader->readFile(__DIR__ . '/opc/opc-coreProperties.xsd');
     $yamlConv = new YamlConverter(new ShortNamingStrategy());
     $phpConv = new PhpConverter(new ShortNamingStrategy());
     foreach ($nss as $ns => $php) {
         $yamlConv->addNamespace($ns, $php);
         $phpConv->addNamespace($ns, $php);
     }
     $yamlItems = $yamlConv->convert([$schema]);
     $phpClasses = $phpConv->convert([$schema]);
     $this->assertEquals(count($phpClasses), count($yamlItems));
 }
 protected function getClasses($xml)
 {
     $schema = $this->reader->readString($xml);
     return $this->converter->convert(array($schema));
 }
Example #15
0
 /**
  *
  * @see Console\Command\Command
  */
 protected function execute(Console\Input\InputInterface $input, Console\Output\OutputInterface $output)
 {
     $src = $input->getArgument('src');
     $nsMap = $input->getOption('ns-map');
     if (!$nsMap) {
         throw new \RuntimeException(__CLASS__ . " requires at least one ns-map.");
     }
     $nsTarget = $input->getOption('ns-dest');
     if (!$nsTarget) {
         throw new \RuntimeException(__CLASS__ . " requires at least one ns-target.");
     }
     if ($input->getOption('naming-strategy') == 'short') {
         $naming = new ShortNamingStrategy();
     } elseif ($input->getOption('naming-strategy') == 'long') {
         $naming = new LongNamingStrategy();
     } else {
         throw new \InvalidArgumentException("Unsupported naming strategy");
     }
     $converter = $this->getConverterter($naming);
     $nsMapKeyed = array();
     $output->writeln("Namespaces:");
     foreach ($nsMap as $val) {
         if (substr_count($val, ';') !== 1) {
             throw new Exception("Invalid syntax for --ns-map");
         }
         list($xmlNs, $phpNs) = explode(";", $val, 2);
         $nsMapKeyed[$xmlNs] = $phpNs;
         $converter->addNamespace($xmlNs, trim(strtr($phpNs, "./", "\\\\"), "\\"));
         $output->writeln("\tXML namepsace: <comment>{$xmlNs}</comment> => PHP namepsace: <info>{$phpNs}</info>");
     }
     $targets = array();
     $output->writeln("Target directories:");
     foreach ($nsTarget as $val) {
         if (substr_count($val, ';') !== 1) {
             throw new Exception("Invalid syntax for --ns-dest");
         }
         list($phpNs, $dir) = explode(";", $val, 2);
         $phpNs = strtr($phpNs, "./", "\\\\");
         $targets[$phpNs] = $dir;
         $output->writeln("\tPHP namepsace: <comment>" . strtr($phpNs, "\\", "/") . "</comment> => Destination directory: <info>{$dir}</info>");
     }
     $arrayMap = $input->getOption('alias-map');
     if ($arrayMap) {
         $output->writeln("Aliases:");
         foreach ($arrayMap as $val) {
             if (substr_count($val, ';') !== 2) {
                 throw new Exception("Invalid syntax for --alias-map");
             }
             list($xmlNs, $name, $type) = explode(";", $val, 3);
             $converter->addAliasMapType($xmlNs, $name, $type);
             $output->writeln("\tXML Type: <comment>{$xmlNs}</comment>#<comment>{$name}</comment>  => PHP Class: <info>{$type}</info> ");
         }
     }
     $reader = new SchemaReader();
     $schemas = array();
     foreach ($src as $file) {
         $output->writeln("Reading <comment>{$file}</comment>");
         $xml = new \DOMDocument('1.0', 'UTF-8');
         if (!$xml->load($file)) {
             throw new \Exception("Can't load the schema '{$file}'");
         }
         if (!isset($nsMapKeyed[$xml->documentElement->getAttribute("targetNamespace")])) {
             $output->writeln("\tSkipping <comment>" . $xml->documentElement->getAttribute("targetNamespace") . "</comment>, can't find a PHP-equivalent namespace. Use --ns-map option?");
             continue;
         }
         $schema = $reader->readFile($file);
         $schemas[spl_object_hash($schema)] = $schema;
     }
     $this->convert($converter, $schemas, $targets, $output);
     return 0;
 }