PHPackages                             ryankubanempire/xsd2php - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. ryankubanempire/xsd2php

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

ryankubanempire/xsd2php
=======================

Convert XSD (XML Schema) definitions into PHP classes and JMS metadata. This is a fork of goetas-webservices/xsd2php

022PHP

Since Feb 21Pushed 1y agoCompare

[ Source](https://github.com/ryankubanempire/xsd2php)[ Packagist](https://packagist.org/packages/ryankubanempire/xsd2php)[ RSS](/packages/ryankubanempire-xsd2php/feed)WikiDiscussions master Synced 1mo ago

READMEChangelogDependenciesVersions (1)Used By (0)

> # UKRAINE NEEDS YOUR HELP NOW!
>
> [](#ukraine-needs-your-help-now)
>
> On 24 February 2022, Russian [President Vladimir Putin ordered an invasion of Ukraine by Russian Armed Forces](https://www.bbc.com/news/world-europe-60504334).
>
> Your support is urgently needed.
>
> - Donate to the volunteers. Here is the volunteer fund helping the Ukrainian army to provide all the necessary equipment:  or
> - Triple-check social media sources. Russian disinformation is attempting to coverup and distort the reality in Ukraine.
> - Help Ukrainian refugees who are fleeing Russian attacks and shellings:
> - Put pressure on your political representatives to provide help to Ukraine.
> - Believe in the Ukrainian people, they will not surrender, they don't have another Ukraine.
>
> THANK YOU!

---

xsd2php
=======

[](#xsd2php)

[![Build Status](https://camo.githubusercontent.com/1ba806fecd79adecc326418b211666b8a686dc13ac29124521a6b1c3f0f3fabc/68747470733a2f2f7472617669732d63692e6f72672f676f657461732d77656273657276696365732f787364327068702e7376673f6272616e63683d6d6173746572)](https://travis-ci.org/goetas-webservices/xsd2php)[![Code Coverage](https://camo.githubusercontent.com/4fca176dc72a63fd53470d5050b31a0bf4879360e09e0bcbedc2c9c773dcf25a/68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f676f657461732d77656273657276696365732f787364327068702f6261646765732f636f7665726167652e706e673f623d6d6173746572)](https://scrutinizer-ci.com/g/goetas-webservices/xsd2php/?branch=master)[![Scrutinizer Code Quality](https://camo.githubusercontent.com/e1994905afd2e8f1584da4927e039794559f4ea18d64396e72e866f02f639e6a/68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f676f657461732d77656273657276696365732f787364327068702f6261646765732f7175616c6974792d73636f72652e706e673f623d6d6173746572)](https://scrutinizer-ci.com/g/goetas-webservices/xsd2php/?branch=master)

Convert XSD into PHP classes.

With `goetas-webservices/xsd2php` you can convert any XSD/WSDL definition into PHP classes.

XSD2PHP can also generate [JMS Serializer](http://jmsyst.com/libs/serializer) compatible metadata that can be used to serialize/unserialize the object instances.

Installation
------------

[](#installation)

There is one recommended way to install xsd2php via [Composer](https://getcomposer.org/):

- adding the dependency to your `composer.json` file:

```
  "require": {
      ..
      "goetas-webservices/xsd2php-runtime":"^0.2.2",
      ..
  },
  "require-dev": {
      ..
      "goetas-webservices/xsd2php":"^0.3",
      ..
  },
```

Usage
-----

[](#usage)

With this example we will convert [OTA XSD definitions](http://opentravel.org/Specifications/OnlineXmlSchema.aspx)into PHP classes.

Suppose that you have all XSD files in `/home/my/ota`, first of all we need a configuration file (as example `config.yml`) that will keep all the namespace and directory mappings information.

```
# config.yml
# Linux Users: PHP Namespaces use back slash \ rather than a forward slash /
# So for destinations_php, the namespace would be TestNs\MyApp

xsd2php:
  namespaces:
    'http://www.example.org/test/': 'TestNs\MyApp'
  destinations_php:
    'TestNs\MyApp': soap/src
#    'TestNs\MyApp': soap\src  #  on Windows

  destinations_jms:
    'TestNs\MyApp': soap/metadata
#    'TestNs\MyApp': soap\metadata  #  on Windows

#  Uncomment this section if you want to have also symfony/validator metadata to be generated from XSD
#  destinations_validation:
#    'TestNs\MyApp': soap/validation
#    'TestNs\MyApp': soap\validation  #  on Windows

  aliases: # optional
    'http://www.example.org/test/':
      MyCustomXSDType:  'MyCustomMappedPHPType'
  naming_strategy: short # optional and default
  path_generator: psr4 # optional and default
#  known_locations: # optional
#    "http://www.example.org/test/somefile.xsd": somefile.xsd
#  known_namespace_locations: # optional
#    "urn:veloconnect:catalog-1.1": xsd/catalog-1.1.xsd
# configs_jms:  #optional
#   xml_cdata: false # Disables CDATA
```

Here is an explanation on the meaning of each parameter:

- `xsd2php.namespaces` (required) defines the mapping between XML namespaces and PHP namespaces. (in the example we have the `http://www.example.org/test/` XML namespace mapped to `TestNs\MyApp`)
- `xsd2php.destinations_php` (required) specifies the directory where to save the PHP classes that belongs to `TestNs\MyApp` PHP namespace. (in this example `TestNs\MyApp` classes will be saved into `soap/src` directory.
- `xsd2php.destinations_jms` (required) specifies the directory where to save JMS Serializer metadata files that belongs to `TestNs\MyApp` PHP namespace. (in this example `TestNs\MyApp` metadata will be saved into `soap/metadata` directory.
- `xsd2php.aliases` (optional) specifies some mappings that are handled by custom JMS serializer handlers. Allows to specify to do not generate metadata for some XML types, and assign them directly a PHP class. For that PHP class is necessary to create a custom JMS serialize/deserialize handler.
- `xsd2php.naming_strategy` (optional) specifies the naming strategy to use when converting XML names PHP classes.
- `xsd2php.path_generator` (optional) specifies the strategy to use for path generation and file saving
- `xsd2php.known_locations` (optional) override remote location with a local file.
- `xsd2php.known_namespace_locations` (optional) Specify schema location by namespace. This can be used to read schemas which import namespaces but do not specify schemaLocation attributes.
- `xsd2php.configs_jms.xml_cdata` (optional) Specify if CDATA should be used or not in serialization.

Generate PHP classes and JMS metadata info
------------------------------------------

[](#generate-php-classes-and-jms-metadata-info)

```
vendor/bin/xsd2php convert config.yml /home/my/ota/OTA_Air*.xsd
```

This command will generate PHP classes and JMS metadata files for all the XSD files matching `/home/my/ota/OTA_Air*.xsd`and using the configuration available in `config.yml`

Serialize / Unserialize
-----------------------

[](#serialize--unserialize)

XSD2PHP can also generate for you [JMS Serializer](http://jmsyst.com/libs/serializer) metadata that you can use to serialize/unserialize the generated PHP class instances.

The parameter `aliases` in the configuration file, will instruct XSD2PHP to not generate any metadata information or PHP class for the `{http://www.example.org/test/}MyCustomXSDType` type. All reference to this type are replaced with the `MyCustomMappedPHPType` name.

You have to provide a [custom serializer](http://jmsyst.com/libs/serializer/master/handlers#subscribing-handlers)for this type/alis.

Here is an example on how to configure JMS serializer to handle custom types

```
