PHP code example of rkt / magento-data

1. Go to this page and download the library: Download rkt/magento-data 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/ */

    

rkt / magento-data example snippets


use Rkt\MageData\Data;

class ProductImageData extends Data
{
    public function __construct(
        public string $src,
        public ?string $alt = null,
    ) {}
}

$image = ProductImageData::from([
    'src' => 'https://example.com/image.jpg',
    'alt' => 'Awesome image'
]);

$image = ProductImageData::create([
    'src' => 'https://example.com/image.jpg',
    'alt' => 'Awesome image'
]);

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|

public function messages(): array
{
    return [
        'email:email' => __('Customer email is invalid.'),
        'firstname:

public function aliases(): array
{
    return [
        'email' => __('Email Address'),
    ];
}

class Person extends Data
{
    public function __construct(public string $firstname) {}

    public function rules(): array
    {
        return ['firstname' => 'n = [],
    ) {}

    public function rules(): array
    {
        return [
            'father' => '

public function rules(): array
{
    return [
        'email' => '     'company_email' => function ($value) {
            return str_ends_with($value, '@example.com');
        },
    ];
}

use Somnambulist\Components\Validation\Rule;

class CompanyEmailRule extends Rule
{
    protected $message = ':attribute must be a company email (e.g. @example.com).';

    public function check($value): bool
    {
        return str_ends_with($value, '@example.com');
    }
}

public function rules(): array
{
    return [
        'email' => '     'company_email' => CompanyEmailRule::class,
    ];
}

use Rkt\MageData\Data;
use Rkt\MageData\Model\Validation\Rule\FormKeyRule;

class ContactFormData extends Data
{
    public function __construct(
        public string $form_key,
        public string $email,
    ) {}

    public function rules(): array
    {
        return [
            'form_key' => '

namespace Rkt\Example\Data;

class MyData extends Data
{
    public function __construct(public string $email) {}

    public function rules(): array
    {
        return ['email' => '

class UpdateMyDataValidation implements ObserverInterface
{
    public function execute(Observer $observer)
    {
        $transport = $observer->getData('transport');

        $rules = $transport->getData('rules');
        $rules['email'] = '

$data->toArray(); // → returns an array representation

$data->toJson(); // → returns a JSON string

class Family extends Data
{
    public function __construct(
        public Person $father,
        public Person $mother,
        public ?array $children = [],
    ) {
    }

    public function rules(): array
    {
        return [
            'father' => ' public string $email,
    ) {
    }

    public function rules(): array
    {
        return [
            'email' => '

$rules = Family::getValidationRules([
    'father' => ['firstname' => 'John', 'lastname' => 'Doe', 'email' => '[email protected]'],
    'mother' => ['firstname' => 'Jane', 'lastname' => 'Doe', 'email' => '[email protected]'],
    'children' => [
        ['firstname' => 'Jimmy', 'lastname' => 'Doe', 'email' => '[email protected]'],
    ],
]);

$rules = [
    'father' => 'equired',
    'father.email' => 'red',
    'mother.lastname' => '