Skip to content

Commit 7a81584

Browse files
committed
Apply fixes from PR
exchange lower case json with JSON in readme. add type hints for decode and decodeMultiple
1 parent 2df738b commit 7a81584

File tree

2 files changed

+16
-14
lines changed

2 files changed

+16
-14
lines changed

readme.md

Lines changed: 12 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,8 +54,8 @@ 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);
@@ -82,7 +84,7 @@ The `JsonDecoder` class accepts two boolean constructor parameters to enable the
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

8486
### Transforming an array of elements
85-
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.
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.
8688

8789
```php
8890
$jsonDecoder = new JsonDecoder();
@@ -103,7 +105,7 @@ The following `Binding` implementations are available
103105
* [CallbackBinding](#callbackbinding)
104106

105107
#### FieldBinding
106-
Defines a json field to property binding for the given type.
108+
Defines a JSON field to property binding for the given type.
107109

108110
**Signature:**
109111
```php
@@ -121,7 +123,7 @@ new ArrayBinding($property, $jsonField, $type);
121123
This defines a field mapping for the property `$property` to an array of class instance of type `$type` with data in `$jsonField`.
122124

123125
#### AliasBinding
124-
Defines a json field to property binding.
126+
Defines a JSON field to property binding.
125127

126128
**Signature:**
127129
```php

src/JsonDecoder.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,14 +35,14 @@ 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);
4141
}
4242

43-
public function decodeMultiple($jsonString, $classType)
43+
public function decodeMultiple(string $json, string $classType)
4444
{
45-
$data = json_decode($jsonString, true);
45+
$data = json_decode($json, true);
4646

4747
return array_map(
4848
function ($element) use ($classType) {

0 commit comments

Comments
 (0)