baudev/php-object-converter-bundle

This Symfony bundle converts PHP objects into ones of different languages

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

This package is auto-updated.

Last update: 2024-04-09 13:16:44 UTC


README

GitHub release PHP MINIMUM VERSION LICENSE

This Symfony bundle converts PHP objects into ones of different languages.

Installation

Download the Bundle

composer require baudev/php-object-converter-bundle

This command requires you to have Composer installed globally, as explained in the installation chapter of the Composer documentation.

Enable the Bundle

  • If you're using Flex:

The bundle is automatically enabled 🎉

  • If you're not using Flex:

You must add the bundle to your kernel:

// app/AppKernel.php

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

        // ...
    }

    // ...
}

How to use this Bundle?

Use the following command to convert your PHP classes:

bin/console converter:generate converter source [output]

The command parameters are the following ones:

Parameter Details
converter (required) Name of a converter. E.g. typescript. See available converters.
source (required) The directory containing classes to scan.
output (optional) The output directory for generated classes.

Available Converters

Name Converter file
typescript Entity/converters/TypeScriptConverter.php
More soon! ...

You can create your own converter as explained here.

Demo

This example uses the typescript converter. It is the result of the follwing command bin/console converter:generate typescript src build.

Input class:

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

Output class:

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

    public static firstName:any;

    protected lastName:string;

    private age:number;

}

Settings

Create file config/packages/php_object_converter.yaml and define:

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.
  • If enable_annotation is true, then only classes with the annotation @Converter will be converted.

  • If disable_annotation is true, then classes with @ConverterIgnore will be ignored.

TODO

  • Write tests.
  • Support interfaces and methods conversion.
  • Add more converters.

Credits

LICENSE

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.