vigihdev/serializer

A Symfony-inspired serializer library for PHP

Installs: 5

Dependents: 0

Suggesters: 0

Security: 0

Stars: 0

Watchers: 0

Forks: 0

Open Issues: 0

pkg:composer/vigihdev/serializer

dev-main 2025-10-28 19:02 UTC

This package is auto-updated.

Last update: 2025-10-28 19:09:13 UTC


README

PHP Version License

Serializer Package

A simple, powerful JSON serializer library for PHP inspired by Symfony components. Transform JSON data into PHP objects with ease.

Installation

composer require vigihdev/serializer

Quick Start

use Serializer\Factory\JsonTransformerFactory;

// Transform JSON file to objects
$transformer = JsonTransformerFactory::create(YourDTO::class);
$objects = $transformer->transformWithFile('data.json');

// Or transform JSON string
$json = '{"name": "John", "age": 30}';
$object = $transformer->transformJson($json);

Features

  • ✅ Transform JSON to PHP objects
  • ✅ Handle both single objects and arrays
  • ✅ File-based and string-based transformation
  • ✅ Symfony Serializer integration
  • ✅ Proper error handling with custom exceptions
  • ✅ Type safety with PHP 8.1+ features

Usage Examples

Basic DTO

class UserDto
{
    public function __construct(
        public readonly string $name,
        public readonly int $age,
        public readonly string $email
    ) {}
}

Transform from File

$transformer = JsonTransformerFactory::create(UserDto::class);
$users = $transformer->transformWithFile('users.json');

foreach ($users as $user) {
    echo $user->name . ': ' . $user->email;
}

Transform from JSON String

$json = '{"name": "Alice", "age": 25, "email": "alice@example.com"}';
$user = $transformer->transformJson($json);

echo $user->name; // "Alice"

Error Handling

use Serializer\Exception\TransformerException;

try {
    $transformer->transformWithFile('invalid.json');
} catch (TransformerException $e) {
    echo "Error: " . $e->getMessage();
}

Requirements

  • PHP 8.1 or higher
  • Symfony Serializer Component

License

MIT License - feel free to use in your projects!

Simple · Fast · Reliable