Skip to content

Commit e23c8e7

Browse files
authored
Merge pull request #15 from karriereat/feature/add-decode-multiple
Add decodeMultiple function to JsonDecoder
2 parents f122c1f + 7a81584 commit e23c8e7

File tree

3 files changed

+66
-12
lines changed

3 files changed

+66
-12
lines changed

readme.md

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
# JsonDecoder for PHP
88

9-
This package contains a JsonDecoder implementation that allows you to convert your json data into php class objects other than `stdclass`.
9+
This package contains a JsonDecoder implementation that allows you to convert your JSON data into php class objects other than `stdclass`.
1010

1111
## Installation
1212
You can install the package via composer
@@ -15,18 +15,19 @@ composer require karriere/json-decoder
1515
```
1616

1717
## Usage
18-
By default all public properties of the class will be inspected. For all properties that have a json key with the same name the according value will be set.
18+
By default all public properties of the class will be inspected. For all properties that have a JSON key with the same name the according value will be set.
1919

2020
### A simple example
2121
Assume you have a class `Person` that looks like this:
2222
```php
23-
class Person {
23+
class Person
24+
{
2425
public $id;
2526
public $name;
2627
}
2728
```
2829

29-
The following code will transform the given json data into an instance of `Person`.
30+
The following code will transform the given JSON data into an instance of `Person`.
3031

3132
```php
3233
$jsonDecoder = new JsonDecoder();
@@ -38,7 +39,8 @@ $person = $jsonDecoder->decode($jsonData, Person::class);
3839
### Defining a Transformer
3940
Let's extend the previous example with a property called address. This address field should contain an instance of `Address`.
4041
```php
41-
class Person {
42+
class Person
43+
{
4244
public $id;
4345
public $name;
4446
public $address;
@@ -52,13 +54,13 @@ The transformer interface defines two methods:
5254
* register: here you register your field, array, alias and callback bindings
5355
* transforms: gives you the full qualified class name e.g.: Your\Namespace\Class
5456
```php
55-
class PersonTransformer implements Transformer {
56-
57+
class PersonTransformer implements Transformer
58+
{
5759
public function register(ClassBindings $classBindings)
5860
{
5961
$classBindings->register(new FieldBinding('address', 'address', Address::class);
6062
}
61-
63+
6264
public function transforms()
6365
{
6466
return Person::class;
@@ -81,6 +83,17 @@ The `JsonDecoder` class accepts two boolean constructor parameters to enable the
8183

8284
To do so a so called `PropertyAccessor` will be installed and on property set the proxy will set the property to accessible, set the according value and then will set the property to not accessible again.
8385

86+
### Transforming an array of elements
87+
If your JSON contains an array of elements at the root level you can use the `decodeMultiple` method to transform the JSON data into an array of class type objects.
88+
89+
```php
90+
$jsonDecoder = new JsonDecoder();
91+
92+
$jsonData = '[{"id": 1, "name": "John Doe"}, {"id": 2, "name": "Jane Doe"}]';
93+
94+
$personArray = $jsonDecoder->decodeMultiple($jsonData, Person::class);
95+
```
96+
8497
## Documentation
8598

8699
### Transformer Bindings
@@ -92,7 +105,7 @@ The following `Binding` implementations are available
92105
* [CallbackBinding](#callbackbinding)
93106

94107
#### FieldBinding
95-
Defines a json field to property binding for the given type.
108+
Defines a JSON field to property binding for the given type.
96109

97110
**Signature:**
98111
```php
@@ -110,7 +123,7 @@ new ArrayBinding($property, $jsonField, $type);
110123
This defines a field mapping for the property `$property` to an array of class instance of type `$type` with data in `$jsonField`.
111124

112125
#### AliasBinding
113-
Defines a json field to property binding.
126+
Defines a JSON field to property binding.
114127

115128
**Signature:**
116129
```php

src/JsonDecoder.php

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,21 @@ public function register(Transformer $transformer)
3535
$this->transformers[$transformer->transforms()] = $transformer;
3636
}
3737

38-
public function decode($jsonString, $classType)
38+
public function decode(string $json, string $classType)
3939
{
40-
return $this->decodeArray(json_decode($jsonString, true), $classType);
40+
return $this->decodeArray(json_decode($json, true), $classType);
41+
}
42+
43+
public function decodeMultiple(string $json, string $classType)
44+
{
45+
$data = json_decode($json, true);
46+
47+
return array_map(
48+
function ($element) use ($classType) {
49+
return $this->decodeArray($element, $classType);
50+
},
51+
$data
52+
);
4153
}
4254

4355
/**

tests/specs/JsonDecoderSpec.php

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,35 @@ public function it_should_transform_empty_raw_data_to_null()
6262

6363
$this->decode($jsonString, JsonDecoderSample::class)->shouldReturn(null);
6464
}
65+
66+
public function it_should_be_able_to_transform_an_array_of_objects()
67+
{
68+
$jsonString = json_encode(
69+
[
70+
[
71+
'id' => 1,
72+
'name' => 'John',
73+
],
74+
[
75+
'id' => 2,
76+
'name' => 'Jane',
77+
],
78+
]
79+
);
80+
81+
$response = $this->decodeMultiple($jsonString, JsonDecoderSample::class);
82+
83+
$response->shouldBeArray();
84+
$response->shouldHaveCount(2);
85+
86+
$response[0]->shouldHaveType(JsonDecoderSample::class);
87+
$response[0]->id->shouldBe(1);
88+
$response[0]->name->shouldBe('John');
89+
90+
$response[1]->shouldHaveType(JsonDecoderSample::class);
91+
$response[1]->id->shouldBe(2);
92+
$response[1]->name->shouldBe('Jane');
93+
}
6594
}
6695

6796
class JsonDecoderSample

0 commit comments

Comments
 (0)