baudev/php-object-converter-bundle

此 Symfony 扩展将 PHP 对象转换为不同语言的对象

v1.0.0 2019-04-08 20:32 UTC

This package is auto-updated.

Last update: 2024-09-09 14:10:27 UTC


README

GitHub release PHP MINIMUM VERSION LICENSE

此 Symfony 扩展将 PHP 对象转换为不同语言的对象。

安装

下载 Bundle

composer require baudev/php-object-converter-bundle

此命令要求您全局安装 Composer,如 Composer 文档的安装章节所述。

启用 Bundle

  • 如果您使用 Flex

该 Bundle 将自动启用 🎉

  • 如果您不使用 Flex

您必须将 Bundle 添加到您的内核中

// app/AppKernel.php

// ...
class AppKernel extends Kernel
{
    public function registerBundles()
    {
        $bundles = [
            // ...
            new Baudev\PHPObjectConverterBundle\PHPObjectConverterBundle(),
        ];

        // ...
    }

    // ...
}

如何使用此 Bundle?

使用以下命令转换您的 PHP 类

bin/console converter:generate converter source [output]

命令参数如下

可用转换器

您可以根据此处的解释创建自己的转换器。

演示

此示例使用 typescript 转换器。这是以下命令的结果 bin/console converter:generate typescript src build

输入类

// src/Entity/Person.php
class Person extends \App\Kernel
{
    /**
     * @var iterable
     */
    public static $firstName;
    /**
     * @var string
     */
    protected $lastName;
    /**
     * @var int
     */
    private $age;
}

输出类

// build/Person.ts
class Person extends Kernel 
{

    public static firstName:any;

    protected lastName:string;

    private age:number;

}

设置

创建文件 config/packages/php_object_converter.yaml 并定义

php_object_converter:
    enable_annotation: true # Enables @Converter annotation. See below.
    ignore_annotation: true # Enables @ConverterIgnore. See below.
    attributes:
        add_private: true # Converts private attributes. Ignore them if false.
        add_protected: true # Converts protected attributes. Ignore them if false.
  • 如果 enable_annotationtrue,则只有带有 @Converter 注释的类将被转换。

  • 如果 disable_annotationtrue,则带有 @ConverterIgnore 的类将被忽略。

待办事项

  • 编写测试。
  • 支持接口和方法的转换。
  • 添加更多转换器。

鸣谢

许可证

MIT License  
  
Copyright (c) 2019 Baudev
  
Permission is hereby granted, free of charge, to any person obtaining a copy  
of this software and associated documentation files (the "Software"), to deal  
in the Software without restriction, including without limitation the rights  
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell  
copies of the Software, and to permit persons to whom the Software is  
furnished to do so, subject to the following conditions:  
  
The above copyright notice and this permission notice shall be included in all  
copies or substantial portions of the Software.  
  
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR  
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,  
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE  
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER  
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,  
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE  
SOFTWARE.