Skip to content

Commit

Permalink
Merge branch 'release/2.14.2'
Browse files Browse the repository at this point in the history
  • Loading branch information
DannyvdSluijs committed Jan 7, 2023
2 parents 081568d + 148c22c commit 1a22134
Show file tree
Hide file tree
Showing 7 changed files with 94 additions and 3 deletions.
9 changes: 9 additions & 0 deletions .github/workflows/build.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,15 @@ jobs:
composer_update_flags: '--prefer-lowest --prefer-stable'
php_ini: 'xdebug.coverage_enable=On'
name: 'PHP 8.1 with lowest stable deps'
- php: 8.2
allow_fail: false
php_ini: 'xdebug.coverage_enable=On'
name: 'PHP 8.2 with latest deps'
- php: 8.2
allow_fail: false
composer_update_flags: '--prefer-lowest --prefer-stable'
php_ini: 'xdebug.coverage_enable=On'
name: 'PHP 8.2 with lowest stable deps'

runs-on: ubuntu-latest

Expand Down
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

## [2.14.2] - 2023-01-07
### Added
- Add PHP 8.2 to build matrix [PR#149](https://github.com/JsonMapper/JsonMapper/pull/149).
### Fixed
- Undefined array key 0 when using object construction middleware [PR#148](https://github.com/JsonMapper/JsonMapper/pull/148). Thanks to [template-provider](https://github.com/template-provider) for reporting the issue.

## [2.14.1] - 2022-11-15
### Fixed
- Cannot map to native php types using constructor middleware [PR#144](https://github.com/JsonMapper/JsonMapper/pull/144). Thanks to [template-provider](https://github.com/template-provider) for reporting the issue.
Expand Down
2 changes: 1 addition & 1 deletion Version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
2.14.1
2.14.2
2 changes: 1 addition & 1 deletion src/Middleware/Constructor/DefaultFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ public function __invoke(\stdClass $json)
$value = $this->mapper->mapToClass($value, $type);
}

if (is_array($value) && $value[0] instanceof \stdClass && class_exists($type)) {
if (is_array($value) && !empty($value) && $value[0] instanceof \stdClass && class_exists($type)) {
$value = $this->mapper->mapToClassArray($value, $type);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

declare(strict_types=1);

namespace JsonMapper\Tests\Implementation\Php81;

class WithConstructorReadOnlyPropertyCollection
{
/**
* @param WithConstructorReadOnlyPropertySimple[] $simples
*/
public function __construct(public readonly array $simples)
{
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

declare(strict_types=1);

namespace JsonMapper\Tests\Implementation\Php81;

class WithConstructorReadOnlyPropertySimple
{
public function __construct(public readonly ?int $status = null)
{
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@
use JsonMapper\JsonMapperBuilder;
use JsonMapper\Tests\Implementation\Php81\WithConstructorPropertyPromotion;
use JsonMapper\Tests\Implementation\Php81\WithConstructorReadOnlyDateTimePropertyPromotion;
use JsonMapper\Tests\Implementation\Php81\WithConstructorReadOnlyPropertyCollection;
use JsonMapper\Tests\Implementation\Php81\WithConstructorReadOnlyPropertyPromotion;
use JsonMapper\Tests\Implementation\Php81\WithConstructorReadOnlyPropertySimple;
use JsonMapper\Tests\Implementation\Php81\WrapperWithConstructorReadOnlyPropertyPromotion;
use JsonMapper\Tests\Implementation\PopoWrapperWithConstructor;
use PHPUnit\Framework\TestCase;
Expand Down Expand Up @@ -110,7 +112,6 @@ public function testCanHandleCustomConstructorsWithNestedCustomConstructorReadon

/**
* @requires PHP >= 8.1
* @throws \JsonException
*/
public function testCanHandleCustomConstructorsWithReadonlyDateTimePropertyPromotion(): void
{
Expand All @@ -129,4 +130,52 @@ public function testCanHandleCustomConstructorsWithReadonlyDateTimePropertyPromo
self::assertInstanceOf(WithConstructorReadOnlyDateTimePropertyPromotion::class, $result);
self::assertInstanceOf(\DateTimeImmutable::class, $result->date);
}

/**
* @requires PHP >= 8.1
*/
public function testCanHandleCustomConstructorsWithEmptyArray(): void
{
$factoryRegistry = new FactoryRegistry();
$mapper = JsonMapperBuilder::new()
->withDocBlockAnnotationsMiddleware()
->withObjectConstructorMiddleware($factoryRegistry)
->withPropertyMapper(new PropertyMapper($factoryRegistry))
->build();

$json = (object) [
'simples' => [],
];

$result = $mapper->mapToClass($json, WithConstructorReadOnlyPropertyCollection::class);

self::assertInstanceOf(WithConstructorReadOnlyPropertyCollection::class, $result);
self::assertIsArray($result->simples);
self::assertEmpty($result->simples);
}

/**
* @requires PHP >= 8.1
*/
public function testCanHandleCustomConstructorsWithArray(): void
{
$factoryRegistry = new FactoryRegistry();
$mapper = JsonMapperBuilder::new()
->withDocBlockAnnotationsMiddleware()
->withObjectConstructorMiddleware($factoryRegistry)
->withPropertyMapper(new PropertyMapper($factoryRegistry))
->build();

$status = 5;
$json = (object) [
'simples' => [(object) ['status' => $status]],
];

$result = $mapper->mapToClass($json, WithConstructorReadOnlyPropertyCollection::class);

self::assertInstanceOf(WithConstructorReadOnlyPropertyCollection::class, $result);
self::assertIsArray($result->simples);
self::assertInstanceOf(WithConstructorReadOnlyPropertySimple::class, $result->simples[0]);
self::assertEquals($status, $result->simples[0]->status);
}
}

0 comments on commit 1a22134

Please sign in to comment.