Skip to content

Commit 9c063cd

Browse files
committed
Init
1 parent 38b6772 commit 9c063cd

17 files changed

+499
-143
lines changed

README.md

Lines changed: 80 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# selective/transformer
22

3-
A strictly typed array transformer with dot access and fluent interface.
4-
The mapped result can be used for JSON responses and many other things.
3+
A strictly typed array transformer with dot access and fluent interface. The mapped result can be used for JSON
4+
responses and many other things.
55

66
[![Latest Version on Packagist](https://img.shields.io/github/release/selective-php/transformer.svg)](https://packagist.org/packages/selective/transformer)
77
[![Software License](https://img.shields.io/badge/license-MIT-brightgreen.svg)](LICENSE)
@@ -10,6 +10,21 @@ The mapped result can be used for JSON responses and many other things.
1010
[![Quality Score](https://img.shields.io/scrutinizer/quality/g/selective-php/transformer.svg)](https://scrutinizer-ci.com/g/selective-php/transformer/?branch=master)
1111
[![Total Downloads](https://img.shields.io/packagist/dt/selective/transformer.svg)](https://packagist.org/packages/selective/transformer/stats)
1212

13+
## Table of Contents
14+
15+
* [Requirements](#requirements)
16+
* [Installation](#installation)
17+
* [Usage](#usage)
18+
* [Transforming](#transforming)
19+
* [Transforming list of arrays](#transforming-list-of-arrays)
20+
* [Dot access](#dot-access)
21+
* [Mapping rules](#mapping-rules)
22+
* [Simple mapping rules](#simple-mapping-rules)
23+
* [Complex mapping rules](#complex-mapping-rules)
24+
* [Filter](#filter)
25+
* [Custom Filter](#custom-filter)
26+
* [License](#license)
27+
1328
## Requirements
1429

1530
* PHP 7.2+ or 8.0+
@@ -22,6 +37,8 @@ composer require selective/transformer
2237

2338
## Usage
2439

40+
### Transforming
41+
2542
Sample data:
2643

2744
```php
@@ -32,8 +49,6 @@ $data = [
3249
];
3350
```
3451

35-
### Minimal example
36-
3752
```php
3853
<?php
3954

@@ -57,10 +72,65 @@ The result:
5772
];
5873
```
5974

60-
### Dot access
75+
### Transforming list of arrays
76+
77+
You can use a method "toArrays" to transform a list of arrays.
78+
79+
This can be useful if you want to transform a resultset from a database query, or a response payload from an API.
80+
81+
**Example:**
82+
83+
```php
84+
$transformer = new ArrayTransformer();
85+
86+
$transformer->map('id', 'id', $transformer->rule()->integer())
87+
->map('first_name', 'first_name', $transformer->rule()->string())
88+
->map('last_name', 'last_name', $transformer->rule()->string())
89+
->map('phone', 'phone', $transformer->rule()->string())
90+
->map('enabled', 'enabled', $transformer->rule()->boolean());
91+
92+
$rows = [];
93+
$rows[] = [
94+
'id' => '100',
95+
'first_name' => 'Sally',
96+
'last_name' => '',
97+
'phone' => null,
98+
'enabled' => '1',
99+
];
100+
101+
$rows[] = [
102+
'id' => '101',
103+
'first_name' => 'Max',
104+
'last_name' => 'Doe',
105+
'phone' => '+123456789',
106+
'enabled' => '0',
107+
];
108+
109+
$result = $transformer->toArrays($rows);
110+
```
111+
112+
The result:
61113

62-
You can copy any data from the source array to any sub-element of the destination array
63-
using the dot-syntax.
114+
```php
115+
[
116+
[
117+
'id' => 100,
118+
'first_name' => 'Sally',
119+
'enabled' => true,
120+
],
121+
[
122+
'id' => 101,
123+
'first_name' => 'Max',
124+
'last_name' => 'Doe',
125+
'phone' => '+123456789',
126+
'enabled' => false,
127+
],
128+
]
129+
```
130+
131+
## Dot access
132+
133+
You can copy any data from the source array to any sub-element of the destination array using the dot-syntax.
64134

65135
```php
66136
<?php
@@ -70,6 +140,8 @@ $transformer->map('firstName', 'address.first_name')
70140
->map('invoice.items', 'root.sub1.sub2.items');
71141
```
72142

143+
## Mapping Rules
144+
73145
### Simple mapping rules
74146

75147
Using strings, separated by `|`, to define a filter chain:
@@ -170,7 +242,7 @@ $transformer->rule()->callback(
170242
);
171243
```
172244

173-
## Custom filters
245+
### Custom Filter
174246

175247
You can also add your own custom filter:
176248

src/ArrayTransformer.php

Lines changed: 72 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@
1313
use Selective\Transformer\Filter\NumberFormatFilter;
1414
use Selective\Transformer\Filter\StringFilter;
1515

16+
/**
17+
* Transformer.
18+
*/
1619
final class ArrayTransformer
1720
{
1821
/**
@@ -25,35 +28,52 @@ final class ArrayTransformer
2528
*/
2629
private $converter;
2730

31+
/**
32+
* @var string[]
33+
*/
34+
private $internalFilters = [
35+
'string' => StringFilter::class,
36+
'blank-to-null' => BlankToNullFilter::class,
37+
'boolean' => BooleanFilter::class,
38+
'integer' => IntegerFilter::class,
39+
'float' => FloatFilter::class,
40+
'number' => NumberFormatFilter::class,
41+
'date' => DateTimeFilter::class,
42+
'array' => ArrayFilter::class,
43+
'callback' => CallbackFilter::class,
44+
];
45+
46+
/**
47+
* The constructor.
48+
*/
2849
public function __construct()
2950
{
3051
$this->converter = new ArrayValueConverter();
31-
$this->registerFilter('string', StringFilter::class);
32-
$this->registerFilter('blank-to-null', BlankToNullFilter::class);
33-
$this->registerFilter('boolean', BooleanFilter::class);
34-
$this->registerFilter('integer', IntegerFilter::class);
35-
$this->registerFilter('float', FloatFilter::class);
36-
$this->registerFilter('number', NumberFormatFilter::class);
37-
$this->registerFilter('date', DateTimeFilter::class);
38-
$this->registerFilter('array', ArrayFilter::class);
39-
$this->registerFilter('callback', CallbackFilter::class);
52+
53+
foreach ($this->internalFilters as $name => $class) {
54+
$this->registerFilter($name, new $class());
55+
}
4056
}
4157

4258
/**
43-
* @param string $string
44-
* @param callable|string $filter
59+
* Register custom filter.
60+
*
61+
* @param string $name The name
62+
* @param callable $filter The filter callback
4563
*/
46-
public function registerFilter(string $string, $filter): void
64+
public function registerFilter(string $name, callable $filter): void
4765
{
48-
$this->converter->registerFilter($string, $filter);
66+
$this->converter->registerFilter($name, $filter);
4967
}
5068

5169
/**
52-
* @param string $destination
53-
* @param string $source
54-
* @param ArrayTransformerRule|string|null $rule
70+
* Add mapping rule.
71+
*
72+
* @param string $destination The destination element
73+
* @param string $source The source element
74+
* @param ArrayTransformerRule|string|null $rule The rule
5575
*
56-
* @return $this
76+
* @return $this The transformer
5777
*/
5878
public function map(string $destination, string $source, $rule = null): self
5979
{
@@ -68,11 +88,23 @@ public function map(string $destination, string $source, $rule = null): self
6888
return $this;
6989
}
7090

91+
/**
92+
* Create transformer rule.
93+
*
94+
* @return ArrayTransformerRule The rule
95+
*/
7196
public function rule(): ArrayTransformerRule
7297
{
7398
return new ArrayTransformerRule();
7499
}
75100

101+
/**
102+
* Convert rule string to rule object.
103+
*
104+
* @param string $rules The rules, separated by '|'
105+
*
106+
* @return ArrayTransformerRule The rule object
107+
*/
76108
private function ruleFromString(string $rules): ArrayTransformerRule
77109
{
78110
$rule = $this->rule();
@@ -90,10 +122,12 @@ private function ruleFromString(string $rules): ArrayTransformerRule
90122
}
91123

92124
/**
93-
* @param array<mixed> $source
94-
* @param array<mixed> $target
125+
* Transform array to array.
126+
*
127+
* @param array<mixed> $source The source
128+
* @param array<mixed> $target The target (optional)
95129
*
96-
* @return array<mixed>
130+
* @return array<mixed> The result
97131
*/
98132
public function toArray(array $source, array $target = []): array
99133
{
@@ -105,7 +139,7 @@ public function toArray(array $source, array $target = []): array
105139
$value = $this->converter->convert($value, $rule);
106140

107141
if ($value === null && !$rule->isRequired()) {
108-
// Don't add item to result
142+
// Skip item
109143
continue;
110144
}
111145

@@ -114,4 +148,21 @@ public function toArray(array $source, array $target = []): array
114148

115149
return $targetData->export();
116150
}
151+
152+
/**
153+
* Transform list of arrays to list of arrays.
154+
*
155+
* @param array<mixed> $source The source
156+
* @param array<mixed> $target The target (optional)
157+
*
158+
* @return array<mixed> The result
159+
*/
160+
public function toArrays(array $source, array $target = []): array
161+
{
162+
foreach ($source as $item) {
163+
$target[] = $this->toArray($item);
164+
}
165+
166+
return $target;
167+
}
117168
}

src/ArrayTransformerFilter.php

Lines changed: 0 additions & 44 deletions
This file was deleted.

src/ArrayTransformerFilterItem.php

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
<?php
2+
3+
namespace Selective\Transformer;
4+
5+
/**
6+
* Filter.
7+
*/
8+
final class ArrayTransformerFilterItem
9+
{
10+
/**
11+
* @var string
12+
*/
13+
private $name;
14+
15+
/**
16+
* @var array<mixed>
17+
*/
18+
private $arguments;
19+
20+
/**
21+
* The constructor.
22+
*
23+
* @param string $name The filter to apply
24+
* @param array<mixed> $arguments The parameters for the filter
25+
*/
26+
public function __construct(string $name, array $arguments = [])
27+
{
28+
$this->name = $name;
29+
$this->arguments = $arguments;
30+
}
31+
32+
/**
33+
* The filter to apply.
34+
*
35+
* @return string The name
36+
*/
37+
public function getName(): string
38+
{
39+
return $this->name;
40+
}
41+
42+
/**
43+
* Get filter parameters.
44+
*
45+
* @return array<mixed> The params
46+
*/
47+
public function getArguments(): array
48+
{
49+
return $this->arguments;
50+
}
51+
}

0 commit comments

Comments
 (0)