1. Go to this page and download the library: Download codememory/dto 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/ */
codememory / dto example snippets
use Symfony\Component\Cache\Adapter\FilesystemAdapter;
use Codememory\Reflection\ReflectorManager;
use Symfony\Component\EventDispatcher\EventDispatcher;
use Codememory\Dto\PropertyGrouper;
use Codememory\Dto\Factory\PropertyExecutionContextFactory;
use Codememory\Dto\NameConverter\SnakeCaseNameConverter;
use Codememory\Dto\Processors\ClassDecoratorProcessor;
use Codememory\Dto\Registrars\ClassDecoratorRegistrar;
use Codememory\Dto\Processors\PropertyDecoratorProcessor;
use Codememory\Dto\Registrars\DecoratorTypeRegistrar;
use Codememory\Dto\Registrars\PropertyDecoratorRegistrar;
use Codememory\Dto\Factory\ClassExecutionContextFactory;
$cache = new FilesystemAdapter('codememory', directory: 'cache');
$reflectorManager = new ReflectorManager($cache);
$eventDispatcher = new EventDispatcher();
$propertyDecoratorRegistrar = new PropertyDecoratorRegistrar();
$manager = new DataTransferObjectManager(
$reflectorManager,
new PropertyGrouper(
new PropertyExecutionContextFactory(
new SnakeCaseNameConverter()
)
),
new ClassDecoratorProcessor(
new ClassDecoratorRegistrar(),
$eventDispatcher
),
new PropertyDecoratorProcessor(
new DecoratorTypeRegistrar(),
$propertyDecoratorRegistrar,
$eventDispatcher
),
new ClassExecutionContextFactory(
new PropertyWrapperFactory()
)
);
use Codememory\Dto\Decorators\Property;
enum MyEnum {
case FOO_BAR;
}
enum MyEnum2: string {
case TEST = 'value';
}
class MyObject {
public function __construct(
public string $fooBar,
#[Property\ToEnum]
public MyEnum $case,
#[Property\ToEnum(value: true)]
public MyEnum2 $case2
) {}
}
$result = $manager->hydrate(MyObject::class, [
'foo_bar' => 'Foo Bar',
'case' => 'FOO_BAR',
'case2' => 'value'
]);
use Codememory\Dto\Decorators\Property;
use Symfony\Component\Validator\Constraints as Assert;
// DTO
class MyObject {
public function __construct(
#[Property\SymfonyValidation([
new Assert\NotBlank(message: 'Foo Bar is
use Codememory\Dto\Events\AfterProcessedTypeDecoratorsEvent;
use Symfony\Component\Validator\Constraints\Collection;
use Symfony\Component\Validator\Validation;
use Codememory\Dto\Decorators\Property\SymfonyValidationHandler;
class SymfonyValidationEventListener {
public function onProcessed(AfterProcessedTypeDecoratorsEvent $event): void
{
if (SymfonyValidationDecoratorTypeInterface::class === $event->type) {
// This contains an array of all the constants of the object into which we store the data
$metadata = $event->classExecutionContext->getMetadata()[SymfonyValidationHandler::METADATA_KEY] ?? false;
if (false !== $metadata) {
$validator = Validation::createValidator();
$violations = $validator->validate($event->data, new Collection($metadata, missingFieldsMessage: 'Not all fields have been transferred.'));
foreach ($violations as $error) {
throw new RuntimeException($error->getMessage());
}
}
}
}
}
// Before creating a manager, you must refer to the EventDispatcher that you pass to the manager
$listener = new SymfonyValidationEventListener();
$eventDispatcher->addListener(AfterProcessedTypeDecoratorsEvent::class, $listener->onProcessed(...));
// Decorator
namespace Codememory\Dto\Decorators\Property;
use Attribute;
use Codememory\Dto\Interfaces\PropertyDecoratorInterface;
use Codememory\Dto\Interfaces\SymfonyValidationDecoratorTypeInterface;
use Symfony\Component\Validator\Constraint;
#[Attribute(Attribute::TARGET_PROPERTY | Attribute::TARGET_PARAMETER)]
class SymfonyValidation implements PropertyDecoratorInterface
{
/**
* @param array<int, Constraint> $constraints
*/
public function __construct(
public array $constraints
) {
}
public function getType(): string
{
return SymfonyValidationDecoratorTypeInterface::class;
}
public function getHandler(): string
{
return SymfonyValidationHandler::class;
}
}
namespace Codememory\Dto\Decorators\Property;
use Codememory\Dto\Interfaces\DecoratorInterface;
use Codememory\Dto\Interfaces\PropertyDecoratorHandlerInterface;
use Codememory\Dto\Interfaces\PropertyExecutionContextInterface;
class SymfonyValidationHandler implements PropertyDecoratorHandlerInterface
{
public const string METADATA_KEY = '__symfony_constraints';
/**
* @param SymfonyValidation $decorator
*/
public function process(DecoratorInterface $decorator, PropertyExecutionContextInterface $executionContext): void
{
$metadata = $executionContext->getClassExecutionContext()->getMetadata();
$inputName = $executionContext->getInputName();
if (!array_key_exists(self::METADATA_KEY, $metadata)) {
$metadata[self::METADATA_KEY] = [];
}
if (!array_key_exists($inputName, $metadata[self::METADATA_KEY])) {
$metadata[self::METADATA_KEY][$inputName] = [];
}
$metadata[self::METADATA_KEY][$inputName] += $decorator->constraints;
$executionContext->getClassExecutionContext()->setMetadata($metadata);
}
}
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.