Skip to content

Commit

Permalink
Merge e3dc20c into 089a67b
Browse files Browse the repository at this point in the history
  • Loading branch information
DannyvdSluijs committed May 2, 2024
2 parents 089a67b + e3dc20c commit b9541d7
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 4 deletions.
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@
"psr/log": "^1.1 || ^2.0 || ^3.0",
"psr/simple-cache": " ^1.0 || ^2.0 || ^3.0",
"symfony/cache": "^4.4 || ^5.0 || ^6.0",
"symfony/polyfill-php73": "^1.18"
"symfony/polyfill-php73": "^1.18",
"symfony/polyfill-php81": "^1.29"
},
"require-dev": {
"guzzlehttp/guzzle": "^6.5 || ^7.0",
Expand Down
9 changes: 8 additions & 1 deletion src/JsonMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -89,12 +89,19 @@ static function (NamedMiddleware $namedMiddleware) use ($remove) {
return $this;
}

public function mapToClass(\stdClass $json, string $class)
public function mapToClass($json, string $class)
{
if (!$json instanceof \stdClass && (\is_array($json) && array_is_list($json))) {
throw TypeError::forArgument(__METHOD__, '\stdClass|array<string, mixed>', $json, 1, '$json');
}

if (! \class_exists($class)) {
throw TypeError::forArgument(__METHOD__, 'class-string', $class, 2, '$class');
}

if (is_array($json)) {
$json = (object) $json;
}
$propertyMap = new PropertyMap();

$handler = $this->resolve();
Expand Down
3 changes: 2 additions & 1 deletion src/JsonMapperInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,11 @@ public function mapObject(\stdClass $json, $object);

/**
* @template T of object
* @param \stdClass|array<string, mixed> $json
* @psalm-param class-string<T> $class
* @return T
*/
public function mapToClass(\stdClass $json, string $class);
public function mapToClass($json, string $class);

/**
* @template T of object
Expand Down
5 changes: 4 additions & 1 deletion tests/Implementation/JsonMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,11 @@ static function (NamedMiddleware $namedMiddleware) use ($remove) {
return $this;
}

public function mapToClass(\stdClass $json, string $class)
public function mapToClass($json, string $class)
{
if (!$json instanceof \stdClass && (\is_array($json) && array_is_list($json))) {
throw TypeError::forArgument(__METHOD__, '\stdClass|array<string, mixed>', $json, 1, '$json');
}
if (! \class_exists($class)) {
throw new \Exception(); // @todo proper exception message
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

declare(strict_types=1);

namespace JsonMapper\Tests\Integration;

use JsonMapper\JsonMapperFactory;
use JsonMapper\Tests\Implementation\Popo;
use PHPUnit\Framework\TestCase;

/**
* @coversNothing
*/
class FeatureSupportsMapToClassWithAssociativeArray extends TestCase
{
public function testItCanMapToClass(): void
{
// Arrange
$mapper = (new JsonMapperFactory())->bestFit();
$json = json_decode('{"name": "one"}', true);

// Act
$object = $mapper->mapToClass($json, Popo::class);

// Assert
self::assertSame('one', $object->name);
}
}

0 comments on commit b9541d7

Please sign in to comment.