worldstores/dto-generator-bundle

将 XSD/JSON 定义转换为 PHP 类

安装次数: 23

依赖项: 0

建议者: 0

安全性: 0

星标: 5

关注者: 3

分支: 3

类型:symfony-bundle

dev-custom_type_object_generation_fix 2016-04-05 15:08 UTC

README

此组件旨在节省您映射和创建 DTO 以及序列化和反序列化 XML 和 JSON 结构所需的时间。它将使用 JMS 序列化符号为您生成 DTO 类,并设置一个基本的 Behat 功能来测试您的 DTO。

免责声明

此组件仍在开发中。

要求

您需要有 XSD/Json 架构的数据结构。

组件功能是什么?

考虑以下 XML

<xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:element name="CATALOG">
    <xs:complexType>
      <xs:sequence>
        <xs:element name="CD" maxOccurs="unbounded" minOccurs="0">
          <xs:complexType>
            <xs:sequence>
              <xs:element type="xs:string" name="TITLE"/>
              <xs:element type="xs:string" name="ARTIST"/>
              <xs:element type="xs:string" name="COUNTRY"/>
              <xs:element type="xs:string" name="COMPANY"/>
              <xs:element type="xs:float" name="PRICE"/>
              <xs:element type="xs:short" name="YEAR"/>
            </xs:sequence>
          </xs:complexType>
        </xs:element>
      </xs:sequence>
    </xs:complexType>
  </xs:element>
</xs:schema>

此组件将生成以下 DTO 类

<?php
/**
 * DTO CATALOG
 */
namespace AppBundle\Dto;

use JMS\Serializer\Annotation;
use Doctrine\Common\Collections\ArrayCollection;

/**
 * CATALOG
 * 
 * @Annotation\ExclusionPolicy("none")
 * @Annotation\XmlRoot("CATALOG")
 */
class CATALOG
{
    /**
     * @var AppBundle\Dto\CD
     * @Annotation\Type("AppBundle\Dto\CD")
     * @Annotation\SerializedName("CD")
     * @Annotation\XmlElement(cdata=false)
     */
    private $CD;

    /**
     * Set CD
     *
     * @param AppBundle\Dto\CD $CD
     *
     * @return CATALOG
     */
    public function setCD($CD)
    {
        $this->CD = $CD;

        return $this;
    }
                    
    /**
     * Get CD
     *
     * @return AppBundle\Dto\CD
     */
    public function getCD()
    {
        return $this->CD;
    }
}
<?php
/**
 * DTO CD
 */
namespace AppBundle\Dto;

use JMS\Serializer\Annotation;
use Doctrine\Common\Collections\ArrayCollection;

/**
 * CD
 * 
 * @Annotation\ExclusionPolicy("none")
 * @Annotation\XmlRoot("CD")
 */
class CD
{
    /**
     * @var string
     * @Annotation\Type("string")
     * @Annotation\SerializedName("TITLE")
     * @Annotation\XmlElement(cdata=false)
     */
    private $TITLE;
    
    /**
     * @var string
     * @Annotation\Type("string")
     * @Annotation\SerializedName("ARTIST")
     * @Annotation\XmlElement(cdata=false)
     */
    private $ARTIST;
    
    /**
     * @var string
     * @Annotation\Type("string")
     * @Annotation\SerializedName("COUNTRY")
     * @Annotation\XmlElement(cdata=false)
     */
    private $COUNTRY;
    
    /**
     * @var string
     * @Annotation\Type("string")
     * @Annotation\SerializedName("COMPANY")
     * @Annotation\XmlElement(cdata=false)
     */
    private $COMPANY;
    
    /**
     * @var Unknown
     * @Annotation\Type("Unknown")
     * @Annotation\SerializedName("PRICE")
     * @Annotation\XmlElement(cdata=false)
     */
    private $PRICE;
    
    /**
     * @var Unknown
     * @Annotation\Type("Unknown")
     * @Annotation\SerializedName("YEAR")
     * @Annotation\XmlElement(cdata=false)
     */
    private $YEAR;
    

    /**
     * Set TITLE
     *
     * @param string $TITLE
     *
     * @return CD
     */
    public function setTITLE($TITLE)
    {
        $this->TITLE = $TITLE;

        return $this;
    }
                    
    /**
     * Get TITLE
     *
     * @return string
     */
    public function getTITLE()
    {
        return $this->TITLE;
    }
    /**
     * Set ARTIST
     *
     * @param string $ARTIST
     *
     * @return CD
     */
    public function setARTIST($ARTIST)
    {
        $this->ARTIST = $ARTIST;

        return $this;
    }
                    
    /**
     * Get ARTIST
     *
     * @return string
     */
    public function getARTIST()
    {
        return $this->ARTIST;
    }
    /**
     * Set COUNTRY
     *
     * @param string $COUNTRY
     *
     * @return CD
     */
    public function setCOUNTRY($COUNTRY)
    {
        $this->COUNTRY = $COUNTRY;

        return $this;
    }
                    
    /**
     * Get COUNTRY
     *
     * @return string
     */
    public function getCOUNTRY()
    {
        return $this->COUNTRY;
    }
    /**
     * Set COMPANY
     *
     * @param string $COMPANY
     *
     * @return CD
     */
    public function setCOMPANY($COMPANY)
    {
        $this->COMPANY = $COMPANY;

        return $this;
    }
                    
    /**
     * Get COMPANY
     *
     * @return string
     */
    public function getCOMPANY()
    {
        return $this->COMPANY;
    }
    /**
     * Set PRICE
     *
     * @param Unknown $PRICE
     *
     * @return CD
     */
    public function setPRICE($PRICE)
    {
        $this->PRICE = $PRICE;

        return $this;
    }
                    
    /**
     * Get PRICE
     *
     * @return Unknown
     */
    public function getPRICE()
    {
        return $this->PRICE;
    }
    /**
     * Set YEAR
     *
     * @param Unknown $YEAR
     *
     * @return CD
     */
    public function setYEAR($YEAR)
    {
        $this->YEAR = $YEAR;

        return $this;
    }
                    
    /**
     * Get YEAR
     *
     * @return Unknown
     */
    public function getYEAR()
    {
        return $this->YEAR;
    }
 
}

安装

您可以使用 composer 安装此组件

composer require worldstores/dto-generator-bundle dev-master

然后在 AppKernel.php 文件中,将组件添加到开发组件数组中

if (in_array($this->getEnvironment(), array('dev', 'test'), true)) {
            $bundles[] = new Symfony\Bundle\DebugBundle\DebugBundle();
            $bundles[] = new Symfony\Bundle\WebProfilerBundle\WebProfilerBundle();
            $bundles[] = new Sensio\Bundle\DistributionBundle\SensioDistributionBundle();
            $bundles[] = new Sensio\Bundle\GeneratorBundle\SensioGeneratorBundle();
            $bundles[] = new WsSys\DtoGeneratorBundle\WsSysDtoGeneratorBundle();
        }

示例

  1. 将以下 XSD 添加到 Resources/xsd/dto_test.xsd
<xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">
    <xs:element name="note">
        <xs:complexType>
            <xs:sequence>
                <xs:element type="xs:string" name="to"/>
                <xs:element type="xs:string" name="from"/>
                <xs:element type="xs:string" name="heading"/>
                <xs:element type="xs:string" name="body"/>
            </xs:sequence>
        </xs:complexType>
    </xs:element>
</xs:schema>
  1. 运行以下命令
php app/console ws:generator:generate:dto \
    app/Resources/xsd/dto_test.xsd \
    src/AppBundle/Dto/ \
    AppBundle\\Dto \
    xsd \
    0 \
    AppBundle
  1. 确保在 AppBundle 中有 Dto 文件夹

它将生成一个名为 Note.php 的类

命令参数

php app/console ws:generator:generate:dto 源文件 目标类 命名空间 类型 是否生成控制器 组件名称

示例

php app/console ws:generator:generate:dto \
    app/Resources/xsd/dto_test.xsd \
    src/AppBundle/Dto/ \
    AppBundle\\Dto \
    xsd \
    0 \
    AppBundle