Skip to content

Commit 6e23f69

Browse files
authored
Release/1.0.0 (#2)
* feat: Implements Mapper library for flexible data transformations.
1 parent de70fc6 commit 6e23f69

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

51 files changed

+1952
-2
lines changed

.gitattributes

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
/tests export-ignore
2+
/vendor export-ignore
3+
4+
/README.md export-ignore
5+
/LICENSE export-ignore
6+
/Makefile export-ignore
7+
/phpmd.xml export-ignore
8+
/phpunit.xml export-ignore
9+
/phpstan.neon.dist export-ignore
10+
/infection.json.dist export-ignore
11+
12+
/.github export-ignore
13+
/.gitignore export-ignore
14+
/.gitattributes export-ignore

.github/workflows/auto-assign.yml

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
name: Auto assign issues and pull requests
2+
3+
on:
4+
issues:
5+
types:
6+
- opened
7+
pull_request:
8+
types:
9+
- opened
10+
11+
jobs:
12+
run:
13+
runs-on: ubuntu-latest
14+
permissions:
15+
issues: write
16+
pull-requests: write
17+
steps:
18+
- name: Assign issues and pull requests
19+
uses: gustavofreze/[email protected]
20+
with:
21+
assignees: '${{ secrets.ASSIGNEES }}'
22+
github_token: '${{ secrets.GITHUB_TOKEN }}'
23+
allow_self_assign: 'true'
24+
allow_no_assignees: 'true'
25+
assignment_options: 'ISSUE,PULL_REQUEST'

.github/workflows/ci.yml

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
name: CI
2+
3+
on:
4+
pull_request:
5+
6+
permissions:
7+
contents: read
8+
9+
env:
10+
PHP_VERSION: '8.3'
11+
12+
jobs:
13+
auto-review:
14+
name: Auto review
15+
runs-on: ubuntu-latest
16+
17+
steps:
18+
- name: Checkout
19+
uses: actions/checkout@v4
20+
21+
- name: Configure PHP
22+
uses: shivammathur/setup-php@v2
23+
with:
24+
php-version: ${{ env.PHP_VERSION }}
25+
26+
- name: Install dependencies
27+
run: composer update --no-progress --optimize-autoloader
28+
29+
- name: Run review
30+
run: composer review
31+
32+
tests:
33+
name: Tests
34+
runs-on: ubuntu-latest
35+
36+
steps:
37+
- name: Checkout
38+
uses: actions/checkout@v4
39+
40+
- name: Use PHP ${{ env.PHP_VERSION }}
41+
uses: shivammathur/setup-php@v2
42+
with:
43+
php-version: ${{ env.PHP_VERSION }}
44+
45+
- name: Install dependencies
46+
run: composer update --no-progress --optimize-autoloader
47+
48+
- name: Run tests
49+
run: composer tests

.gitignore

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
*.lock
2+
.phpunit.*
3+
.idea/
4+
5+
/vendor/
6+
/report

Makefile

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
ifeq ($(OS),Windows_NT)
2+
PWD := $(shell cd)
3+
else
4+
PWD := $(shell pwd -L)
5+
endif
6+
7+
ARCH := $(shell uname -m)
8+
PLATFORM :=
9+
10+
ifeq ($(ARCH),arm64)
11+
PLATFORM := --platform=linux/amd64
12+
endif
13+
14+
DOCKER_RUN = docker run ${PLATFORM} --rm -it --net=host -v ${PWD}:/app -w /app gustavofreze/php:8.3
15+
16+
.PHONY: configure test test-file test-no-coverage review show-reports clean
17+
18+
configure:
19+
@${DOCKER_RUN} composer update --optimize-autoloader
20+
21+
test:
22+
@${DOCKER_RUN} composer tests
23+
24+
test-file:
25+
@${DOCKER_RUN} composer test-file ${FILE}
26+
27+
test-no-coverage:
28+
@${DOCKER_RUN} composer tests-no-coverage
29+
30+
review:
31+
@${DOCKER_RUN} composer review
32+
33+
show-reports:
34+
@sensible-browser report/coverage/coverage-html/index.html report/coverage/mutation-report.html
35+
36+
clean:
37+
@sudo chown -R ${USER}:${USER} ${PWD}
38+
@rm -rf report vendor .phpunit.cache .lock

README.md

Lines changed: 175 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,175 @@
1-
# mapper
2-
Allows mapping data between different formats, such as JSON, arrays, and DTOs, providing flexibility in transforming and serializing information.
1+
# Mapper
2+
3+
[![License](https://img.shields.io/badge/license-MIT-green)](LICENSE)
4+
5+
* [Overview](#overview)
6+
* [Installation](#installation)
7+
* [How to use](#how-to-use)
8+
* [License](#license)
9+
* [Contributing](#contributing)
10+
11+
<div id='overview'></div>
12+
13+
## Overview
14+
15+
Allows mapping data between different formats, such as JSON, arrays, and DTOs, providing flexibility in transforming and
16+
serializing information.
17+
18+
<div id='installation'></div>
19+
20+
## Installation
21+
22+
```bash
23+
composer require tiny-blocks/mapper
24+
```
25+
26+
<div id='how-to-use'></div>
27+
28+
## How to use
29+
30+
### Object
31+
32+
The library exposes available behaviors through the `ObjectMapper` interface, and the implementation of these behaviors
33+
through the `ObjectMappability` trait.
34+
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:
39+
40+
```php
41+
<?php
42+
43+
declare(strict_types=1);
44+
45+
namespace Example;
46+
47+
use TinyBlocks\Mapper\ObjectMappability;
48+
use TinyBlocks\Mapper\ObjectMapper;
49+
50+
final readonly class Shipping implements ObjectMapper
51+
{
52+
use ObjectMappability;
53+
54+
public function __construct(public int $id, public ShippingAddresses $addresses)
55+
{
56+
}
57+
}
58+
```
59+
60+
Next, define a `ShippingAddresses` class that is iterable:
61+
62+
```php
63+
<?php
64+
65+
declare(strict_types=1);
66+
67+
namespace Example;
68+
69+
use ArrayIterator;use IteratorAggregate;use TinyBlocks\Mapper\ObjectMappability;use TinyBlocks\Mapper\ObjectMapper;use Traversable;
70+
71+
final class ShippingAddresses implements ObjectMapper, IteratorAggregate
72+
{
73+
use ObjectMappability;
74+
75+
/**
76+
* @var \TinyBlocks\Mapper\Models\ShippingAddress[] $elements
77+
*/
78+
private iterable $elements;
79+
80+
public function __construct(iterable $elements = [])
81+
{
82+
$this->elements = is_array($elements) ? $elements : iterator_to_array($elements);
83+
}
84+
85+
public function getIterator(): Traversable
86+
{
87+
return new ArrayIterator($this->elements);
88+
}
89+
}
90+
91+
```
92+
93+
Now you can map data into a `Shipping` object using `fromIterable`:
94+
95+
```php
96+
<?php
97+
98+
use Example\Shipping;
99+
100+
$shipping = Shipping::fromIterable(iterable: [
101+
'id' => PHP_INT_MAX,
102+
'addresses' => [
103+
[
104+
'city' => 'New York',
105+
'state' => 'NY',
106+
'street' => '5th Avenue',
107+
'number' => 717,
108+
'country' => 'US'
109+
]
110+
]
111+
]);
112+
```
113+
114+
#### Map object to array
115+
116+
Once the object is created, you can easily convert it into an array representation.
117+
118+
```php
119+
$shipping->toArray();
120+
```
121+
122+
This will output the following array:
123+
124+
```php
125+
[
126+
'id' => 9223372036854775807,
127+
'addresses' => [
128+
[
129+
'city' => 'New York',
130+
'state' => 'NY',
131+
'street' => '5th Avenue',
132+
'number' => 717,
133+
'country' => 'US'
134+
]
135+
]
136+
]
137+
```
138+
139+
#### Map object to JSON
140+
141+
Similarly, you can convert the object into a JSON representation.
142+
143+
```php
144+
$shipping->toJson();
145+
```
146+
147+
This will produce the following JSON:
148+
149+
```json
150+
{
151+
"id": 9223372036854775807,
152+
"addresses": [
153+
{
154+
"city": "New York",
155+
"state": "NY",
156+
"street": "5th Avenue",
157+
"number": 717,
158+
"country": "US"
159+
}
160+
]
161+
}
162+
```
163+
164+
<div id='license'></div>
165+
166+
## License
167+
168+
Mapper is licensed under [MIT](LICENSE).
169+
170+
<div id='contributing'></div>
171+
172+
## Contributing
173+
174+
Please follow the [contributing guidelines](https://github.com/tiny-blocks/tiny-blocks/blob/main/CONTRIBUTING.md) to
175+
contribute to the project.

composer.json

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
{
2+
"name": "tiny-blocks/mapper",
3+
"type": "library",
4+
"license": "MIT",
5+
"homepage": "https://github.com/tiny-blocks/mapper",
6+
"description": "Allows mapping data between different formats, such as JSON, arrays, and DTOs, providing flexibility in transforming and serializing information.",
7+
"prefer-stable": true,
8+
"minimum-stability": "stable",
9+
"keywords": [
10+
"psr",
11+
"dto",
12+
"json",
13+
"array",
14+
"object",
15+
"mapper",
16+
"tiny-blocks"
17+
],
18+
"authors": [
19+
{
20+
"name": "Gustavo Freze de Araujo Santos",
21+
"homepage": "https://github.com/gustavofreze"
22+
}
23+
],
24+
"support": {
25+
"issues": "https://github.com/tiny-blocks/mapper/issues",
26+
"source": "https://github.com/tiny-blocks/mapper"
27+
},
28+
"config": {
29+
"sort-packages": true,
30+
"allow-plugins": {
31+
"infection/extension-installer": true
32+
}
33+
},
34+
"autoload": {
35+
"psr-4": {
36+
"TinyBlocks\\Mapper\\": "src/"
37+
}
38+
},
39+
"autoload-dev": {
40+
"psr-4": {
41+
"TinyBlocks\\Mapper\\": "tests/"
42+
}
43+
},
44+
"require": {
45+
"php": "^8.3",
46+
"tiny-blocks/collection": "^1.8"
47+
},
48+
"require-dev": {
49+
"phpmd/phpmd": "^2.15",
50+
"phpstan/phpstan": "^1",
51+
"phpunit/phpunit": "^11",
52+
"infection/infection": "^0",
53+
"squizlabs/php_codesniffer": "^3.11"
54+
},
55+
"scripts": {
56+
"test": "phpunit --configuration phpunit.xml tests",
57+
"phpcs": "phpcs --standard=PSR12 --extensions=php ./src",
58+
"phpmd": "phpmd ./src text phpmd.xml --suffixes php --ignore-violations-on-exit",
59+
"phpstan": "phpstan analyse -c phpstan.neon.dist --quiet --no-progress",
60+
"test-file": "phpunit --configuration phpunit.xml --no-coverage --filter",
61+
"mutation-test": "infection --only-covered --threads=max --logger-html=report/coverage/mutation-report.html --coverage=report/coverage",
62+
"test-no-coverage": "phpunit --configuration phpunit.xml --no-coverage tests",
63+
"review": [
64+
"@phpcs",
65+
"@phpmd",
66+
"@phpstan"
67+
],
68+
"tests": [
69+
"@test",
70+
"@mutation-test"
71+
],
72+
"tests-no-coverage": [
73+
"@test-no-coverage"
74+
]
75+
}
76+
}

0 commit comments

Comments
 (0)