Multi-Format Serializer
One entry point to convert data from any supported format to any other: load it once, ask for the format you need.
Why
Every format has its own PHP function and its own edge cases: json_encode(), a YAML
library, SimpleXML, fputcsv(), serialize(). Code that turns a model into JSON today
and into YAML or CSV tomorrow ends up with one branch per format, each with its own way
to skip a property or drop a null.
Serializer puts all of them behind one interface. Load the data with Serialize::from…(),
then ask for the output with to…():
Serialize::from($user)->toJson(); // object → JSON
Serialize::fromJson($payload)->toYaml(); // JSON → YAML
Serialize::fromCsv($file)->toArray(); // CSV → array
Serialize::fromXml($soapBody)->toJson(); // XML → JSON
Serialize::from($rows)
->withIgnoreProperties(['password'])
->toXml(); // array → XML, without a field
Any source works with any output:
| Source | Output |
|---|---|
from() — object or array | toArray() |
fromJson() | toJson() |
fromYaml() | toYaml() |
fromCsv() | toCsv() |
fromXml() | toXml() |
fromPhpSerialize() | toPhpSerialize() |
toPlainText() (output only) |
The property rules (withIgnoreProperties(), withOnlyProperties(),
withDoNotParseNullValues(), attributes, ...) are set once and apply whatever the output.
Features
- Format Conversion: Read objects, arrays, JSON, YAML, XML, CSV or PHP-serialized data, and write any of them, plus plain text
- Property Control: Filter, transform, and manipulate object properties during conversion
- Object Mapping: Copy properties between different object structures with intelligent mapping
- Property Pattern Matching: Customize how properties are matched and transformed
- Attribute Support: Process PHP attributes during serialization and deserialization
- Type Safety: Maintain data types during transformations
Quick Examples
Convert an object to JSON
$object = new MyClass();
$json = \ByJG\Serializer\Serialize::from($object)
->toJson();
Copy properties between objects
$source = ["id" => 1, "name" => "John"];
$target = new User();
\ByJG\Serializer\ObjectCopy::copy($source, $target);
Create a copyable object
class User implements \ByJG\Serializer\ObjectCopyInterface
{
use \ByJG\Serializer\ObjectCopyTrait;
public $id;
public $name;
// Automatically inherits copyFrom() and copyTo() methods
}
Documentation
Core Components
| Component | Description | Link |
|---|---|---|
| Serialize | Core component for converting objects between formats | Documentation |
| ObjectCopy | Final utility class for copying properties between objects | Documentation |
| ObjectCopyTrait | Trait implementing copyable object functionality | Documentation |
| ObjectCopyInterface | Interface for implementing copyable objects | Documentation |
| BaseModel | Abstract base class with object copying functionality | Documentation |
| DirectTransform | Basic property handler for direct transformations in ObjectCopy | Documentation |
Guides
- Formatters - JSON, XML, YAML, CSV, and Plain Text output formatting, and reading XML
- Property Handlers - Transform property names and values during copying
- Advanced Usage - Performance optimization, security, and complex patterns
- Integration Examples - Framework integration (Symfony, Laravel, Doctrine, etc.)
- ByJG Ecosystem - How Serializer integrates with other ByJG components
- Troubleshooting - Common issues and solutions
Installation
composer require "byjg/serializer"
Running Tests
./vendor/bin/phpunit