1. Go to this page and download the library: Download neuron-php/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/ */
neuron-php / dto example snippets
use Neuron\Dto\Factory;
// Create DTO from configuration
$factory = new Factory('user.yaml');
$dto = $factory->create();
// Set values
$dto->username = 'johndoe';
$dto->email = '[email protected]';
$dto->age = 25;
// Validate
if (!$dto->validate()) {
$errors = $dto->getErrors();
// Handle validation errors
}
// Export as JSON
echo $dto->getAsJson();
use Neuron\Dto\Factory;
// Load from file
$factory = new Factory('path/to/neuron.yaml');
$dto = $factory->create();
// Set properties
$dto->firstName = 'John';
$dto->email = '[email protected]';
$dto->age = 30;
use Neuron\Dto\Dto;
use Neuron\Dto\Property;
$dto = new Dto();
// Create string property
$username = new Property();
$username->setName('username');
$username->setType('string');
$username->setRequired(true);
$username->addLengthValidator(3, 20);
$dto->addProperty($username);
// Create email property
$email = new Property();
$email->setName('email');
$email->setType('email');
$email->setRequired(true);
$dto->addProperty($email);
use Neuron\Validation\IValidator;
class CustomValidator implements IValidator
{
public function validate($value): bool
{
// Custom validation logic
return $value !== 'forbidden';
}
public function getError(): string
{
return 'Value cannot be "forbidden"';
}
}
// Add to property
$property->addValidator(new CustomValidator());
use Neuron\Dto\Factory;
// Create DTO with image property
$factory = new Factory([
'profile_pic' => [
'type' => 'image',
'imageData;
// Or use data URI format
$dto->profile_pic = 'data:image/jpeg;base64,' . $imageData;
// Validate
$dto->validate();
// Get JSON output (image remains as base64 string)
$json = $dto->getAsJson();
// Create a custom validator with SVG enabled
$imageValidator = new \Neuron\Validation\IsImage(
[], // allowed MIME types (empty = all)
null, // max size
true, // check image data
true // ALLOW SVG (security risk!)
);
use Neuron\Dto\Dto;
class UserDto extends Dto
{
public function __construct()
{
parent::__construct();
$this->loadConfiguration('user.yaml');
}
public function getFullName(): string
{
return $this->firstName . ' ' . $this->lastName;
}
public function isAdult(): bool
{
return $this->age >= 18;
}
public function toArray(): array
{
return [
'username' => $this->username,
'email' => $this->email,
'fullName' => $this->getFullName(),
'isAdult' => $this->isAdult()
];
}
}
use Neuron\Dto\Factory;
class CachedDtoFactory extends Factory
{
private static array $cache = [];
public function create(): Dto
{
$cacheKey = md5($this->configPath);
if (!isset(self::$cache[$cacheKey])) {
self::$cache[$cacheKey] = parent::create();
}
// Return deep clone to prevent shared state
return clone self::$cache[$cacheKey];
}
}
use PHPUnit\Framework\TestCase;
use Neuron\Dto\Factory;
class DtoTest extends TestCase
{
private $dto;
protected function setUp(): void
{
$factory = new Factory('test-dto.yaml');
$this->dto = $factory->create();
}
public function testValidation(): void
{
$this->dto->username = 'ab'; // Too short
$this->dto->email = 'invalid-email';
$this->assertFalse($this->dto->validate());
$errors = $this->dto->getErrors();
$this->assertArrayHasKey('username', $errors);
$this->assertArrayHasKey('email', $errors);
}
public function testValidData(): void
{
$this->dto->username = 'johndoe';
$this->dto->email = '[email protected]';
$this->dto->age = 25;
$this->assertTrue($this->dto->validate());
$this->assertEmpty($this->dto->getErrors());
}
public function testNestedObjects(): void
{
$this->dto->address->street = '123 Main St';
$this->dto->address->city = 'New York';
$this->assertEquals('123 Main St', $this->dto->address->street);
$this->assertEquals('New York', $this->dto->address->city);
}
public function testJsonExport(): void
{
$this->dto->username = 'johndoe';
$this->dto->email = '[email protected]';
$json = $this->dto->getAsJson();
$decoded = json_decode($json, true);
$this->assertEquals('johndoe', $decoded['username']);
$this->assertEquals('[email protected]', $decoded['email']);
}
}
class MapperTest extends TestCase
{
public function testDataMapping(): void
{
$factory = new Factory('dto.yaml');
$dto = $factory->create();
$mapperFactory = new MapperFactory('mapping.yaml');
$mapper = $mapperFactory->create();
$sourceData = [
'external' => [
'user_name' => 'johndoe',
'user_email' => '[email protected]'
]
];
$mapper->map($dto, $sourceData);
$this->assertEquals('johndoe', $dto->username);
$this->assertEquals('[email protected]', $dto->email);
}
}
// Always validate before processing
if( !$dto->validate() )
{
// Log errors
Log::error('DTO validation failed', $dto->getErrors());
// Return early with error response
return new ValidationErrorResponse($dto->getErrors());
}
// Process valid data
$result = $service->process($dto);
// Base DTO for common properties
abstract class BaseDto extends Dto
{
protected function addTimestamps(): void
{
$createdAt = new Property();
$createdAt->setName('createdAt');
$createdAt->setType('date_time');
$this->addProperty($createdAt);
$updatedAt = new Property();
$updatedAt->setName('updatedAt');
$updatedAt->setType('date_time');
$this->addProperty($updatedAt);
}
}
// Specific DTO extending base
class UserDto extends BaseDto
{
public function __construct()
{
parent::__construct();
$this->loadConfiguration('user.yaml');
$this->addTimestamps();
}
}
// Cache DTO definitions
class DtoCache
{
private static array $definitions = [];
public static function getDefinition(string $config): array
{
if (!isset(self::$definitions[$config])) {
self::$definitions[$config] = Yaml::parseFile($config);
}
return self::$definitions[$config];
}
}
// Use lazy loading for nested objects
class LazyDto extends Dto
{
private array $lazyProperties = [];
public function __get(string $name)
{
if( isset( $this->lazyProperties[ $name ] ) )
{
// Load only when accessed
$this->loadProperty($name);
}
return parent::__get($name);
}
}
class ApiController
{
private Factory $dtoFactory;
public function createUser(Request $request): Response
{
$dto = $this->dtoFactory->create('user');
// Map request data to DTO
$mapper = new RequestMapper();
$mapper->map($dto, $request->all());
// Validate
if( !$dto->validate() )
{
return response()->json([
'errors' => $dto->getErrors()
], 422);
}
// Process valid data
$user = $this->userService->create($dto);
return response()->json($user, 201);
}
}