Skip to content

Commit 82378dc

Browse files
committed
Fix mapping of class names
1 parent 9bf29f6 commit 82378dc

File tree

6 files changed

+230
-61
lines changed

6 files changed

+230
-61
lines changed

src/JsonMapper.php

Lines changed: 71 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,14 @@ public function map($json, string $className = null, string $collectionClassName
157157
return $json;
158158
}
159159

160+
// Map the original given class names to a custom ones
161+
$className = $this->getMappedClassName($className, $json);
162+
163+
if ($collectionClassName !== null) {
164+
$collectionClassName = $this->getMappedClassName($collectionClassName, $json);
165+
}
166+
167+
// Assert that classes exists
160168
$this->assertClassesExists($className, $collectionClassName);
161169

162170
// Handle collections
@@ -251,10 +259,8 @@ private function makeInstance(string $className, ...$constructorArguments)
251259
/**
252260
* Returns TRUE if the property contains an "ReplaceNullWithDefaultValue" annotation.
253261
*
254-
* @template T
255-
*
256-
* @param class-string<T> $className The class name of the initial element
257-
* @param string $propertyName The name of the property
262+
* @param class-string $className The class name of the initial element
263+
* @param string $propertyName The name of the property
258264
*
259265
* @return bool
260266
*/
@@ -270,9 +276,7 @@ private function isReplaceNullWithDefaultValueAnnotation(string $className, stri
270276
/**
271277
* Returns TRUE if the property contains an "ReplaceProperty" annotation.
272278
*
273-
* @template T of TEntity
274-
*
275-
* @param class-string<T> $className The class name of the initial element
279+
* @param class-string $className The class name of the initial element
276280
*
277281
* @return bool
278282
*/
@@ -287,10 +291,8 @@ private function isReplacePropertyAnnotation(string $className): bool
287291
/**
288292
* Returns the specified reflection property.
289293
*
290-
* @template T
291-
*
292-
* @param class-string<T> $className The class name of the initial element
293-
* @param string $propertyName The name of the property
294+
* @param class-string $className The class name of the initial element
295+
* @param string $propertyName The name of the property
294296
*
295297
* @return null|ReflectionProperty
296298
*/
@@ -306,13 +308,11 @@ private function getReflectionProperty(string $className, string $propertyName):
306308
/**
307309
* Returns the specified reflection class.
308310
*
309-
* @template T of object
310-
*
311-
* @param class-string<T>|T $className The class name of the initial element
311+
* @param class-string $className The class name of the initial element
312312
*
313313
* @return null|ReflectionClass
314314
*/
315-
private function getReflectionClass($className): ?ReflectionClass
315+
private function getReflectionClass(string $className): ?ReflectionClass
316316
{
317317
try {
318318
return new ReflectionClass($className);
@@ -324,10 +324,8 @@ private function getReflectionClass($className): ?ReflectionClass
324324
/**
325325
* Extracts possible property annotations.
326326
*
327-
* @template T
328-
*
329-
* @param class-string<T> $className The class name of the initial element
330-
* @param string $propertyName The name of the property
327+
* @param class-string $className The class name of the initial element
328+
* @param string $propertyName The name of the property
331329
*
332330
* @return Annotation[]
333331
*/
@@ -348,9 +346,7 @@ private function extractPropertyAnnotations(string $className, string $propertyN
348346
/**
349347
* Extracts possible class annotations.
350348
*
351-
* @template T of object
352-
*
353-
* @param class-string<T> $className The class name of the initial element
349+
* @param class-string $className The class name of the initial element
354350
*
355351
* @return Annotation[]
356352
*/
@@ -371,11 +367,9 @@ private function extractClassAnnotations($className): array
371367
/**
372368
* Returns TRUE if the property has the given annotation
373369
*
374-
* @template T
375-
*
376-
* @param class-string<T> $className The class name of the initial element
377-
* @param string $propertyName The name of the property
378-
* @param string $annotationName The name of the property annotation
370+
* @param class-string $className The class name of the initial element
371+
* @param string $propertyName The name of the property
372+
* @param string $annotationName The name of the property annotation
379373
*
380374
* @return bool
381375
*/
@@ -395,10 +389,8 @@ private function hasPropertyAnnotation(string $className, string $propertyName,
395389
/**
396390
* Returns TRUE if the class has the given annotation.
397391
*
398-
* @template T of TEntity
399-
*
400-
* @param class-string<T> $className The class name of the initial element
401-
* @param string $annotationName The name of the class annotation
392+
* @param class-string $className The class name of the initial element
393+
* @param string $annotationName The name of the class annotation
402394
*
403395
* @return bool
404396
*/
@@ -418,10 +410,8 @@ private function hasClassAnnotation(string $className, string $annotationName):
418410
/**
419411
* Extracts the default value of a property.
420412
*
421-
* @template T
422-
*
423-
* @param class-string<T> $className The class name of the initial element
424-
* @param string $propertyName The name of the property
413+
* @param class-string $className The class name of the initial element
414+
* @param string $propertyName The name of the property
425415
*
426416
* @return null|mixed
427417
*/
@@ -500,11 +490,11 @@ private function assertClassesExists(string $className, string $collectionClassN
500490
/**
501491
* Sets a property value.
502492
*
503-
* @param TEntity $entity
504-
* @param string $name
505-
* @param mixed $value
493+
* @param object $entity
494+
* @param string $name
495+
* @param mixed $value
506496
*/
507-
private function setProperty($entity, string $name, $value): void
497+
private function setProperty(object $entity, string $name, $value): void
508498
{
509499
// Handle variadic setters
510500
if (is_array($value)) {
@@ -616,29 +606,14 @@ public function getCollectionValueType(Type $type): Type
616606
*
617607
* @param Type $type
618608
*
619-
* @return null|string
620-
*
621-
* @throws DomainException
622-
*/
623-
private function getClassNameFromType(Type $type): ?string
624-
{
625-
return $type->getClassName();
626-
}
627-
628-
/**
629-
* Returns the class name.
630-
*
631-
* @param mixed $json
632-
* @param Type $type
633-
*
634609
* @return class-string
635610
*
636611
* @throws DomainException
637612
*/
638-
private function getClassName($json, Type $type)
613+
private function getClassNameFromType(Type $type): string
639614
{
640615
/** @var null|class-string $className */
641-
$className = $this->getClassNameFromType($type);
616+
$className = $type->getClassName();
642617

643618
// @codeCoverageIgnoreStart
644619
if ($className === null) {
@@ -647,19 +622,55 @@ private function getClassName($json, Type $type)
647622
}
648623
// @codeCoverageIgnoreEnd
649624

625+
return $className;
626+
}
627+
628+
/**
629+
* Returns the mapped class name.
630+
*
631+
* @param class-string $className The class name to be mapped using the class map
632+
* @param mixed $json The JSON data
633+
*
634+
* @return class-string
635+
*
636+
* @throws DomainException
637+
*/
638+
private function getMappedClassName(string $className, $json): string
639+
{
650640
if (array_key_exists($className, $this->classMap)) {
651641
$classNameOrClosure = $this->classMap[$className];
652642

653-
// Execute closure to get the mapped class name
654-
if ($classNameOrClosure instanceof Closure) {
655-
/** @var class-string $className */
656-
$className = $classNameOrClosure($json);
643+
if (!($classNameOrClosure instanceof Closure)) {
644+
/** @var class-string $classNameOrClosure */
645+
return $classNameOrClosure;
657646
}
647+
648+
// Execute closure to get the mapped class name
649+
$className = $classNameOrClosure($json);
658650
}
659651

652+
/** @var class-string $className */
660653
return $className;
661654
}
662655

656+
/**
657+
* Returns the class name.
658+
*
659+
* @param mixed $json
660+
* @param Type $type
661+
*
662+
* @return class-string
663+
*
664+
* @throws DomainException
665+
*/
666+
private function getClassName($json, Type $type): string
667+
{
668+
return $this->getMappedClassName(
669+
$this->getClassNameFromType($type),
670+
$json
671+
);
672+
}
673+
663674
/**
664675
* Cast node to collection.
665676
*
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
3+
/**
4+
* This file is part of the package magicsunday/jsonmapper.
5+
*
6+
* For the full copyright and license information, please read the
7+
* LICENSE file that was distributed with this source code.
8+
*/
9+
10+
declare(strict_types=1);
11+
12+
namespace MagicSunday\Test\Classes\ClassMap;
13+
14+
use MagicSunday\Test\Classes\Collection;
15+
16+
/**
17+
* Class CollectionSource.
18+
*
19+
* @author Rico Sonntag <[email protected]>
20+
* @license https://opensource.org/licenses/MIT
21+
* @link https://github.com/magicsunday/jsonmapper/
22+
*/
23+
class CollectionSource extends Collection
24+
{
25+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
3+
/**
4+
* This file is part of the package magicsunday/jsonmapper.
5+
*
6+
* For the full copyright and license information, please read the
7+
* LICENSE file that was distributed with this source code.
8+
*/
9+
10+
declare(strict_types=1);
11+
12+
namespace MagicSunday\Test\Classes\ClassMap;
13+
14+
use ArrayObject;
15+
16+
/**
17+
* Class CollectionTarget.
18+
*
19+
* @author Rico Sonntag <[email protected]>
20+
* @license https://opensource.org/licenses/MIT
21+
* @link https://github.com/magicsunday/jsonmapper/
22+
*/
23+
class CollectionTarget extends ArrayObject
24+
{
25+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
3+
/**
4+
* This file is part of the package magicsunday/jsonmapper.
5+
*
6+
* For the full copyright and license information, please read the
7+
* LICENSE file that was distributed with this source code.
8+
*/
9+
10+
declare(strict_types=1);
11+
12+
namespace MagicSunday\Test\Classes\ClassMap;
13+
14+
/**
15+
* Class SourceItem.
16+
*
17+
* @author Rico Sonntag <[email protected]>
18+
* @license https://opensource.org/licenses/MIT
19+
* @link https://github.com/magicsunday/jsonmapper/
20+
*/
21+
class SourceItem
22+
{
23+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
3+
/**
4+
* This file is part of the package magicsunday/jsonmapper.
5+
*
6+
* For the full copyright and license information, please read the
7+
* LICENSE file that was distributed with this source code.
8+
*/
9+
10+
declare(strict_types=1);
11+
12+
namespace MagicSunday\Test\Classes\ClassMap;
13+
14+
/**
15+
* Class TargetItem.
16+
*
17+
* @author Rico Sonntag <[email protected]>
18+
* @license https://opensource.org/licenses/MIT
19+
* @link https://github.com/magicsunday/jsonmapper/
20+
*/
21+
class TargetItem
22+
{
23+
}

0 commit comments

Comments
 (0)