PHP code example of gam6itko / jms-serializer-config-converter

1. Go to this page and download the library: Download gam6itko/jms-serializer-config-converter library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

gam6itko / jms-serializer-config-converter example snippets


use Gam6itko\JSCC\Converter\Converter;
use Gam6itko\JSCC\Denormalizer\XmlDenormalizer;
use Gam6itko\JSCC\Denormalizer\YamlDenormalizer;
use Gam6itko\JSCC\Normalizer\AnnotationNormalizer;
use Gam6itko\JSCC\Normalizer\XmlNormalizer;
use Gam6itko\JSCC\Normalizer\YamlNormalizer;
use Metadata\Driver\FileLocator;

// normalizers
$xmlFileLocator = new FileLocator([
    'Namespace' => 'folder_with_xml'
]);
$xmlNormalizer = new XmlNormalizer($xmlFileLocator);

$yamlFileLocator = new FileLocator([
    'Namespace' => 'folder_with_yaml'
]);
$yamlNormalizer = new YamlNormalizer($yamlFileLocator);

$annotationNormalizer = new AnnotationNormalizer();

// denormalizers
$xmlDenormalizer = new XmlDenormalizer([
    'Namespace' => 'folder_where_to_save_xml'
]);

$yamlDenormalizer = new YamlDenormalizer([
    'Namespace' => 'folder_where_to_save_yaml'
]);

// show time
$converter = new Converter(
    [
        'annotation' => $annotationNormalizer,
        'annot' => $annotationNormalizer,
        'xml'   => $xmlNormalizer,
        'yaml'  => $yamlNormalizer,
        'yml'   => $yamlNormalizer,
        'foo'   => $yamlNormalizer,
    ],
    [
        'xml'  => $xmlDenormalizer,
        'yaml' => $yamlDenormalizer,
        'bar'  => $yamlDenormalizer,
    ]
);

$refClass = new \ReflectionClass('Namespace\ClassName');
// get annotation from `Namespace\ClassName` class and save it to `folder_where_to_save_xml`
$converter->convert($refClass, 'annotation', 'yaml');
// get yaml from `folder_with_xml` and save it to `folder_where_to_save_yaml`
$converter->convert($refClass, 'xml', 'yaml');
// get yaml from `folder_with_yaml` and save it to `folder_where_to_save_xml`
$converter->convert($refClass, 'yml', 'xml');
// get yaml from `folder_with_yaml` and save it to `folder_where_to_save_yaml`
$converter->convert($refClass, 'foo', 'bar');
// exception here !!!
$converter->convert($refClass, 'xml', 'annotation');