Skip to content

Commit 20583a8

Browse files
authored
Merge pull request #56 from karriereat/dev/php-82
Add PHP 8.2. Support
2 parents e40e603 + 91f6a20 commit 20583a8

Some content is hidden

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

46 files changed

+1050
-1085
lines changed

.github/workflows/ci.yml

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
name: CI
2+
3+
on:
4+
push:
5+
branches: [ master ]
6+
pull_request:
7+
branches: [ master ]
8+
9+
jobs:
10+
build:
11+
12+
strategy:
13+
fail-fast: true
14+
matrix:
15+
php: [ "8.0", "8.1", "8.2" ]
16+
17+
runs-on: ubuntu-latest
18+
name: PHP@${{ matrix.php }}
19+
20+
steps:
21+
- uses: shivammathur/setup-php@v2
22+
with:
23+
php-version: ${{ matrix.php }}
24+
25+
- uses: actions/checkout@v3
26+
27+
- name: Validate composer.json and composer.lock
28+
run: composer validate
29+
30+
- name: Install dependencies
31+
run: composer install --prefer-dist --no-progress --no-suggest
32+
33+
- name: Lint code
34+
run: composer run-script lint
35+
36+
- name: Analyse code
37+
run: composer run-script analyse
38+
39+
- name: Test code
40+
run: composer run-script test

.github/workflows/lint.yml

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

.github/workflows/test.yml

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

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
vendor/
2+
coverage/
23
composer.lock
34
.phpunit.result.cache
4-
.php-cs-fixer.cache
55
coverage.xml
6+
junit.xml

.php-cs-fixer.php

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

CHANGELOG.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,22 @@ All notable changes to this project will be documented in this file.
44
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
55
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
66

7+
## [5.0.0] - 2023-03-01
8+
### Added
9+
- Support for PHP 8.2.
10+
- Support for typed properties (in addition to the annotated properties)
11+
- [BREAKING] Type hints and return types
12+
13+
### Changed
14+
- [BREAKING] Constructor order of `Bindings`
15+
- [BREAKING] Argument order of `transform` and `transferRaw` in `JsonDecoder`
16+
- Set dynamic properties only if `AllowDynamicProperties` attribute is set (on PHP 8.2. and greater)
17+
- Linting to `pint`
18+
- Unit tests to `pest`
19+
20+
### Removed
21+
- Support for PHP 7.4.
22+
723
## [4.2.0] - 2022-11-11
824
### Added
925
- Support for using array-keys in `ArrayBinding` (#52, #55)

readme.md renamed to README.md

Lines changed: 30 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<a href="https://www.karriere.at/" target="_blank"><img width="200" src="https://raw.githubusercontent.com/karriereat/.github/main/profile/logo.svg"></a>
22
<span>&nbsp;&nbsp;&nbsp;</span>
3-
![](https://github.com/karriereat/json-decoder/workflows/test/badge.svg)
4-
![](https://github.com/karriereat/json-decoder/workflows/lint/badge.svg)
3+
![](https://github.com/karriereat/json-decoder/workflows/CI/badge.svg)
4+
[![Packagist Downloads](https://img.shields.io/packagist/dt/karriere/json-decoder.svg?style=flat-square)](https://packagist.org/packages/karriere/json-decoder)
55

66
# JsonDecoder for PHP
77

@@ -19,7 +19,7 @@ composer require karriere/json-decoder
1919

2020
By default the Decoder will iterate over all JSON fields defined and will try to set this values on the given class type instance. This change in behavior allows the use of `json-decoder` on classes that use the **magic** `__get` and `__set` functions like Laravel's Eloquent models.
2121

22-
If a property equally named like the JSON field is found or a explicit `Binding` is defined for the JSON field it will be decoded into the defined place. Otherwise the property will just be created and assigned.
22+
If a property equally named like the JSON field is found or a explicit `Binding` is defined for the JSON field it will be decoded into the defined place. Otherwise the property will just be created and assigned (you need the `#[AllowDynamicProperties]` attribute if you are on PHP 8.2.).
2323

2424
The `JsonDecoder` class can receive one parameter called `shouldAutoCase`. If set to true it will try to find the camel-case version from either snake-case or kebap-case automatically if no other binding was registered for the field and it will use an `AliasBinding` if one of the variants can be found.
2525

@@ -28,36 +28,47 @@ The `JsonDecoder` class can receive one parameter called `shouldAutoCase`. If se
2828
Assume you have a class `Person` that looks like this:
2929

3030
```php
31+
#[AllowDynamicProperties]
3132
class Person
3233
{
33-
public $id;
34-
public $name;
34+
public int $id;
35+
public string $name;
36+
public ?string $lastname = '';
3537
}
3638
```
3739

3840
The following code will transform the given JSON data into an instance of `Person`.
3941

4042
```php
4143
$jsonDecoder = new JsonDecoder();
42-
$jsonData = '{"id": 1, "name": "John Doe"}';
44+
$jsonData = '{"id": 1, "name": "John Doe", "lastname": null, "dynamicProperty": "foo"}';
4345

4446
$person = $jsonDecoder->decode($jsonData, Person::class);
4547
```
4648

49+
Please be aware that since PHP 8.2. dynamic properties are deprecated. So if you still wish to have the ability to make
50+
use of those dynamic properties you have to add the PHP attribute `AllowDynamicProperties` to your class.
51+
If you are using PHP 8.2. (and greater) and don't use the `AllowDynamicProperties` attribute all dynamic properties will
52+
be ignored.
53+
4754
### More complex use case
4855

49-
Let's extend the previous example with a property called address. This address field should contain an instance of `Address`. In the prior versions of `json-decoder` it was necessary to define a custom `Transformer` to be able to handle this situation. As of version 4 you can use the introduced method `scanAndRegister` to automatically generate the transformer based on class annotations.
56+
Let's extend the previous example with a property called address. This address field should contain an instance of `Address`.
57+
As of version 4 you can use the introduced method `scanAndRegister` to automatically generate the transformer based on class annotations.
58+
Since version 5 you can also make use of the property type instead of a class annotation.
5059

5160
```php
5261
class Person
5362
{
54-
public $id;
55-
public $name;
63+
public int $id;
64+
public string $name;
5665

5766
/**
5867
* @var Address
5968
*/
6069
public $address;
70+
71+
public ?Address $typedAddress = null;
6172
}
6273
```
6374

@@ -67,7 +78,7 @@ For this class definition we can decode JSON data as follows:
6778
$jsonDecoder = new JsonDecoder();
6879
$jsonDecoder->scanAndRegister(Person::class);
6980

70-
$jsonData = '{"id": 1, "name": "John Doe", "address": {"street": "Samplestreet", "city": "Samplecity"}}';
81+
$jsonData = '{"id": 1, "name": "John Doe", "address": {"street": "Samplestreet", "city": "Samplecity"}, , "typedAddress": {"street": "Samplestreet", "city": "Samplecity"}}';
7182

7283
$person = $jsonDecoder->decode($jsonData, Person::class);
7384
```
@@ -79,9 +90,9 @@ If you don't use annotations or need a more flexible `Transformer` you can also
7990
```php
8091
class Person
8192
{
82-
public $id;
83-
public $name;
84-
public $address;
93+
public int $id;
94+
public string $name;
95+
public mixed $address;
8596
}
8697
```
8798

@@ -153,19 +164,19 @@ Defines a JSON field to property binding for the given type.
153164
**Signature:**
154165

155166
```php
156-
new FieldBinding($property, $jsonField, $type);
167+
new FieldBinding(string $property, ?string $jsonField = null, ?string $type = null, bool $isRequired = false);
157168
```
158169

159170
This defines a field mapping for the property `$property` to a class instance of type `$type` with data in `$jsonField`.
160171

161172
#### ArrayBinding
162173

163-
Defines a array field binding for the given type.
174+
Defines an array field binding for the given type.
164175

165176
**Signature:**
166177

167178
```php
168-
new ArrayBinding($property, $jsonField, $type);
179+
new ArrayBinding(string $property, ?string $jsonField = null, ?string $type = null, bool $isRequired = false);
169180
```
170181

171182
This defines a field mapping for the property `$property` to an array of class instance of type `$type` with data in `$jsonField`.
@@ -177,7 +188,7 @@ Defines a JSON field to property binding.
177188
**Signature:**
178189

179190
```php
180-
new AliasBinding($property, $jsonField);
191+
new AliasBinding(string $property, ?string $jsonField = null, bool $isRequired = false);
181192
```
182193

183194
#### DateTimeBinding
@@ -187,7 +198,7 @@ Defines a JSON field to property binding and converts the given string to a `Dat
187198
**Signature:**
188199

189200
```php
190-
new new DateTimeBinding($property, $jsonField, $isRequired = false, $dateTimeFormat = DateTime::ATOM);
201+
new DateTimeBinding(string $property, ?string $jsonField = null, bool $isRequired = false, $dateTimeFormat = DateTime::ATOM);
191202
```
192203

193204
#### CallbackBinding
@@ -197,7 +208,7 @@ Defines a property binding that gets the callback result set as its value.
197208
**Signature:**
198209

199210
```php
200-
new CallbackBinding($property, $callback);
211+
new CallbackBinding(string $property, private Closure $callback);
201212
```
202213

203214
## License

0 commit comments

Comments
 (0)