PHP code example of apie / type-converter

1. Go to this page and download the library: Download apie/type-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/ */

    

apie / type-converter example snippets



use Apie\TypeConverter\DefaultConvertersFactory;
$converter = DefaultConvertersFactory::create();
var_dump($converter->convertTo(12, 'string')); // '12'


use Apie\TypeConverter\DefaultConvertersFactory;

class Dto {
    public string $description;

    public string $name;
}

class DomainObject {
    public function __construct(
        private string $description,
        private string $name
    ) {
    }

    public function getName(): string
    {
        return $this->name;
    }

    public function getDescription(): string
    {
        return $this->description;
    }
}
use Apie\TypeConverter\DefaultConvertersFactory;
$converter = DefaultConvertersFactory::create();
$dto = ($converter->convertTo(new DomainObject('description', 'name'), Dto::class));
var_dump($dto->name); // 'name'
$converter->convertTo($dto, DomainObject::class);


use Apie\Core\ValueObjects\Interfaces\StringValueObjectInterface;
use Apie\TypeConverter\ConverterInterface;

/**
 * @implements ConverterInterface<StringValueObjectInterface, string>
 */
class StringValueObjectToStringConverter implements ConverterInterface
{
    public function convert(StringValueObjectInterface $valueObject): string
    {
        return $valueObject->toNative();
    }
}

use Apie\TypeConverter\DefaultConvertersFactory;
$converter = DefaultConvertersFactory::create(new StringValueObjectToStringConverter());
$converter->convertTo(new Email('[email protected]'), 'string');


use Apie\TypeConverter\Converters\FloatToStringConverter;
use Apie\TypeConverter\Converters\IntToStringConverter;
use Apie\TypeConverter\Converters\ObjectToObjectConverter;
use Apie\TypeConverter\TypeConverter;
$converter = new TypeConverter(
    new ObjectToObjectConverter(),
    new IntToStringConverter(),
    new FloatToStringConverter()
);


use Apie\Core\ValueObjects\Interfaces\StringValueObjectInterface;
use Apie\TypeConverter\ConverterInterface;

/**
 * @implements ConverterInterface<string, Email>
 */
class StringToEmailConverter implements ConverterInterface
{
    public function convert(string $value): Email
    {
        return Email::fromNative($value);
    }
}


use Apie\Core\ValueObjects\Interfaces\StringValueObjectInterface;
use Apie\TypeConverter\ConverterInterface;

/**
 * @implements ConverterInterface<string, StringValueObjectInterface>
 */
class StringToStringValueObjectConverter implements ConverterInterface
{
    public function convert(string $value, \ReflectionNamedType $wantedType): StringValueObjectInterface
    {
        $className = $wantedType->getName();
        return $className::fromNative($value);
    }
}


use Apie\Core\ValueObjects\Interfaces\StringValueObjectInterface;
use Apie\TypeConverter\ConverterInterface;
use Apie\TypeConverter\TypeConverter;

/**
 * @implements ConverterInterface<int, StringValueObjectInterface>
 */
class IntToStringValueObjectConverter implements ConverterInterface
{
    public function convert(int $value, \ReflectionNamedType $wantedType, TypeConverter $typeConverter): StringValueObjectInterface
    {
        $value = $typeConverter->convertTo($value, 'string');
        $className = $wantedType->getName();
        return $className::fromNative($value);
    }
}


use Apie\TypeConverter\Converters\FloatToStringConverter;
use Apie\TypeConverter\Converters\IntToStringConverter;
use Apie\TypeConverter\Converters\ObjectToObjectConverter;
use Apie\TypeConverter\TypeConverter;
$converter = new TypeConverter(
    new ObjectToObjectConverter(),
    new IntToStringConverter(),
    new FloatToStringConverter()
);