rkt / magento-data
Data Object & Data Transfer Objects for Magento 2
Installs: 13
Dependents: 1
Suggesters: 0
Security: 0
Stars: 0
Watchers: 1
Forks: 1
Open Issues: 0
Type:magento2-module
Requires
- magento/framework: *
- rakit/validation: ^1.4
This package is auto-updated.
Last update: 2025-05-08 20:29:22 UTC
README
A powerful, lightweight Data Object & DTO (Data Transfer Object) system for Magento 2 β supporting smart instantiation, validation, and serialization.
π Installation
composer require rkt/magento-data
β¨ Features Overview
1. β Easy Data Object Instantiation
Use the static from()
or create()
methods to easily instantiate data objects.
Example
use Rkt\MageData\Data; class ProductImageData extends Data { public function __construct( public string $src, public ?string $alt = null, ) {} }
You can instantiate it like this:
$image = ProductImageData::from([ 'src' => 'https://example.com/image.jpg', 'alt' => 'Awesome image' ]);
Or using create()
:
$image = ProductImageData::create([ 'src' => 'https://example.com/image.jpg', 'alt' => 'Awesome image' ]);
2. π‘ Validation Built In
This module includes built-in validation using rakit/validation
.
πΉ Basic Validation
Just define a rules()
method in your data object:
class Customer extends Data { public function __construct( public string $email, public string $firstname, public string $lastname, public array $street, ) {} public function rules(): array { return [ 'email' => 'email|required', 'firstname' => 'required|min:1|max:250', 'lastname' => 'required|min:1|max:250', 'street.0' => 'required', ]; } }
You can validate array elements (like
street.0
) using dot notation.
πΉ Custom Validation Messages
Override messages()
:
public function messages(): array { return [ 'email:email' => __('Customer email is invalid.'), 'firstname:required' => __('First name cannot be empty.'), ]; }
πΉ Custom Field Aliases
Override aliases()
:
public function aliases(): array { return [ 'email' => __('Email Address'), ]; }
In error messages, email
will now appear as "Email Address".
3. π§© Nested Object Validation
Nested and recursive validation works out of the box:
class Person extends Data { public function __construct(public string $firstname) {} public function rules(): array { return ['firstname' => 'required']; } } class Family extends Data { public function __construct( public Person $father, public Person $mother, public ?array $children = [], ) {} public function rules(): array { return [ 'father' => 'required', 'mother' => 'required', ]; } }
β In this setup:
father
andmother
are required and must passPerson
validation.children
can be a list ofPerson
, and theyβll be validated too (if provided).
4. π§΅ Event-Driven Rule Customization
You can dynamically modify validation rules/messages/aliases using Magento events.
πΉ Example
namespace Rkt\Example\Data; class MyData extends Data { public function __construct(public string $email) {} public function rules(): array { return ['email' => 'required']; } }
πΈ Event Name
When validate()
is called, the event rkt_example_data_mydata_validate_before
is dispatched.
πΉ Observer Configuration (events.xml
)
<event name="rkt_example_data_mydata_validate_before"> <observer name="update_mydata_validation_data" instance="Rkt\Example\Observer\UpdateMyDataValidation" /> </event>
πΉ Sample Observer
class UpdateMyDataValidation implements ObserverInterface { public function execute(Observer $observer) { $transport = $observer->getData('transport'); $rules = $transport->getData('rules'); $rules['email'] = 'required|email'; $aliases = $transport->getData('aliases'); $aliases['email'] = 'Email Address'; $transport->setData('rules', $rules); $transport->setData('aliases', $aliases); } }
β
Now your validation dynamically adds the email
rule and alias based on observer logic.
5. π Serialization Support
Convert data objects to array or JSON easily:
$data->toArray(); // β returns an array representation $data->toJson(); // β returns a JSON string
π Notes
- This module is under active development β more features and integrations are coming soon!
- Built for flexibility, testability, and ease of use in Magento 2 backend and frontend service layers.
π¬ Stay Connected
Got feedback or feature requests? PRs and ideas are welcome!