@@ -27,15 +27,11 @@ composer require tiny-blocks/mapper
2727
2828## How to use
2929
30- ### Object
30+ The examples demonstrate how to create objects from iterables, map objects to arrays, and convert objects to JSON.
3131
32- The library exposes available behaviors through the ` ObjectMapper ` interface, and the implementation of these behaviors
33- through the ` ObjectMappability ` trait.
32+ ### Create an object from an iterable
3433
35- #### Create an object from an iterable
36-
37- You can map data from an iterable (such as an array) into an object. Here's how to map a ` Shipping ` object from an
38- iterable:
34+ First, define your classes using the ` ObjectMapper ` interface and ` ObjectMappability ` trait:
3935
4036``` php
4137<?php
@@ -47,17 +43,22 @@ namespace Example;
4743use TinyBlocks\Mapper\ObjectMappability;
4844use TinyBlocks\Mapper\ObjectMapper;
4945
50- final readonly class Shipping implements ObjectMapper
46+ final readonly class ShippingAddress implements ObjectMapper
5147{
5248 use ObjectMappability;
5349
54- public function __construct(public int $id, public ShippingAddresses $addresses)
55- {
50+ public function __construct(
51+ private string $city,
52+ private ShippingState $state,
53+ private string $street,
54+ private int $number,
55+ private ShippingCountry $country
56+ ) {
5657 }
5758}
5859```
5960
60- Next, define a ` ShippingAddresses ` class that is iterable :
61+ Next, define a collection class implementing ` IterableMapper ` :
6162
6263``` php
6364<?php
@@ -66,28 +67,41 @@ declare(strict_types=1);
6667
6768namespace Example;
6869
69- use ArrayIterator;use IteratorAggregate;use TinyBlocks\Mapper\ObjectMappability;use TinyBlocks\Mapper\ObjectMapper;use Traversable;
70+ use TinyBlocks\Collection\Collection;
71+ use TinyBlocks\Mapper\IterableMappability;
72+ use TinyBlocks\Mapper\IterableMapper;
7073
71- final class ShippingAddresses implements ObjectMapper, IteratorAggregate
74+ final class ShippingAddresses extends Collection implements IterableMapper
7275{
73- use ObjectMappability;
74-
75- /**
76- * @var \TinyBlocks\Mapper\Models\ShippingAddress[] $elements
77- */
78- private iterable $elements;
76+ use IterableMappability;
7977
80- public function __construct(iterable $elements = [])
78+ public function getType(): string
8179 {
82- $this->elements = is_array($elements) ? $elements : iterator_to_array($elements) ;
80+ return ShippingAddress::class ;
8381 }
82+ }
83+ ```
84+
85+ Finally, create a class that uses the collection:
86+
87+ ``` php
88+ <?php
8489
85- public function getIterator(): Traversable
90+ declare(strict_types=1);
91+
92+ namespace Example;
93+
94+ use TinyBlocks\Mapper\ObjectMappability;
95+ use TinyBlocks\Mapper\ObjectMapper;
96+
97+ final readonly class Shipping implements ObjectMapper
98+ {
99+ use ObjectMappability;
100+
101+ public function __construct(public int $id, public ShippingAddresses $addresses)
86102 {
87- return new ArrayIterator($this->elements);
88103 }
89104}
90-
91105```
92106
93107Now you can map data into a ` Shipping ` object using ` fromIterable ` :
@@ -111,7 +125,7 @@ $shipping = Shipping::fromIterable(iterable: [
111125]);
112126```
113127
114- #### Map object to array
128+ ### Map object to array
115129
116130Once the object is created, you can easily convert it into an array representation.
117131
@@ -136,7 +150,7 @@ This will output the following array:
136150]
137151```
138152
139- #### Map object to JSON
153+ ### Map object to JSON
140154
141155Similarly, you can convert the object into a JSON representation.
142156
0 commit comments