tiny-blocks/mapper

Allows mapping data between different formats, such as JSON, arrays, and DTOs, providing flexibility in transforming and serializing information.

1.0.4 2024-12-24 15:31 UTC

This package is auto-updated.

Last update: 2024-12-25 06:21:31 UTC


README

License

Overview

Allows mapping data between different formats, such as JSON, arrays, and DTOs, providing flexibility in transforming and serializing information.

Installation

composer require tiny-blocks/mapper

How to use

Object

The library exposes available behaviors through the ObjectMapper interface, and the implementation of these behaviors through the ObjectMappability trait.

Create an object from an iterable

You can map data from an iterable (such as an array) into an object. Here's how to map a Shipping object from an iterable:

<?php

declare(strict_types=1);

namespace Example;

use TinyBlocks\Mapper\ObjectMappability;
use TinyBlocks\Mapper\ObjectMapper;

final readonly class Shipping implements ObjectMapper
{
    use ObjectMappability;

    public function __construct(public int $id, public ShippingAddresses $addresses)
    {
    }
}

Next, define a ShippingAddresses class that is iterable:

<?php

declare(strict_types=1);

namespace Example;

use ArrayIterator;use IteratorAggregate;use TinyBlocks\Mapper\ObjectMappability;use TinyBlocks\Mapper\ObjectMapper;use Traversable;

final class ShippingAddresses implements ObjectMapper, IteratorAggregate
{
    use ObjectMappability;

    /**
     * @var \TinyBlocks\Mapper\Models\ShippingAddress[] $elements
     */
    private iterable $elements;

    public function __construct(iterable $elements = [])
    {
        $this->elements = is_array($elements) ? $elements : iterator_to_array($elements);
    }

    public function getIterator(): Traversable
    {
        return new ArrayIterator($this->elements);
    }
}

Now you can map data into a Shipping object using fromIterable:

<?php

use Example\Shipping;

$shipping = Shipping::fromIterable(iterable: [
    'id'        => PHP_INT_MAX,
    'addresses' => [
        [
            'city'    => 'New York',
            'state'   => 'NY',
            'street'  => '5th Avenue',
            'number'  => 717,
            'country' => 'US'
        ]
    ]
]);

Map object to array

Once the object is created, you can easily convert it into an array representation.

$shipping->toArray();

This will output the following array:

[
    'id'        => 9223372036854775807,
    'addresses' => [
        [
            'city'    => 'New York',
            'state'   => 'NY',
            'street'  => '5th Avenue',
            'number'  => 717,
            'country' => 'US'
        ]
    ]
]

Map object to JSON

Similarly, you can convert the object into a JSON representation.

$shipping->toJson();

This will produce the following JSON:

{
    "id": 9223372036854775807,
    "addresses": [
        {
            "city": "New York",
            "state": "NY",
            "street": "5th Avenue",
            "number": 717,
            "country": "US"
        }
    ]
}

License

Mapper is licensed under MIT.

Contributing

Please follow the contributing guidelines to contribute to the project.