spiral/marshaller-bridge
Bridge package for Spiral Framework that wires up the Spiral Marshaller with sensible defaults. Provides a bootloader, replaces SerializerBootloader, and lets you configure the MapperFactory and type matchers for custom mapping behavior.
Installation Add the package via Composer:
composer require spiral/marshaller-bridge
Register the MarshallerBridge service provider in config/app.php under providers:
Spiral\Marshaller\MarshallerBridge::class,
First Use Case: Basic Marshalling
Inject the MarshallerInterface into a Spiral handler or service:
use Spiral\Marshaller\MarshallerInterface;
class MyHandler
{
public function __construct(private MarshallerInterface $marshaller) {}
public function __invoke($data)
{
// Convert object to array
$array = $this->marshaller->marshal($data);
// Convert array back to object
$object = $this->marshaller->unmarshal($array, MyModel::class);
return $object;
}
}
Where to Look First
config/marshaller.php (if auto-generated) for default settings like strict_mode or ignore_properties.DTO Marshalling Use the marshaller to convert between DTOs and arrays for API requests/responses:
// Incoming request → DTO
$dto = $this->marshaller->unmarshal($request->all(), CreateUserDto::class);
// DTO → Response array
$responseData = $this->marshaller->marshal($dto);
Database Hydration Automate model hydration from database results:
$users = $this->marshaller->unmarshal(
$dbResults,
User::class,
['collection' => true] // For collections
);
Nested Object Handling
Leverage MarshallerBridge to handle nested objects/arrays recursively:
$complexData = $this->marshaller->unmarshal($input, ComplexModel::class);
ValidatorInterface to validate marshalled data before processing.DateTime, Uuid) via MarshallerBridge::extend().Strict Mode
strict_mode is true (default in some configs), unmarshalling will throw exceptions for missing/extra properties.strict_mode: false in config or handle exceptions gracefully:
try {
$obj = $this->marshaller->unmarshal($data, MyClass::class);
} catch (MarshallerException $e) {
// Fallback logic
}
Circular References
Parent → Child → Parent) may cause infinite loops.ignore_properties or implement a custom MarshallerBridge extension.Type Mismatches
MarshallerBridge::setTypeMapper().$this->marshaller->setLogger(new MonologLogger());
dd($this->marshaller->getConfig());
null, empty arrays, and deeply nested structures.Custom Marshaller
Create a custom marshaller by extending MarshallerBridge:
class CustomMarshaller extends MarshallerBridge
{
protected function getTypeMapper(): TypeMapperInterface
{
return new CustomTypeMapper();
}
}
Property Transformations
Use MarshallerBridge::setPropertyTransformer() to modify properties during marshalling:
$this->marshaller->setPropertyTransformer(
fn (string $property, $value) => strtoupper($value)
);
Event Listeners Attach listeners to marshal/unmarshal events for pre/post-processing:
$this->marshaller->on(MarshallerEvents::UNMARSHAL, function ($context) {
// Modify $context->data before unmarshalling
});
How can I help you explore Laravel packages today?