Skip to content

Commit dabd68a

Browse files
authored
Release/1.0.5 (#13)
1 parent 8a90734 commit dabd68a

27 files changed

Lines changed: 475 additions & 206 deletions

composer.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
{
22
"name": "tiny-blocks/mapper",
33
"type": "library",
4-
"version": "1.0.4",
54
"license": "MIT",
65
"homepage": "https://github.com/tiny-blocks/mapper",
76
"description": "Allows mapping data between different formats, such as JSON, arrays, and DTOs, providing flexibility in transforming and serializing information.",
@@ -44,7 +43,7 @@
4443
},
4544
"require": {
4645
"php": "^8.3",
47-
"tiny-blocks/collection": "1.9.0"
46+
"tiny-blocks/collection": "^1"
4847
},
4948
"require-dev": {
5049
"phpmd/phpmd": "^2.15",

phpmd.xml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,12 @@
3636
<rule ref="rulesets/design.xml/EvalExpression"/>
3737
<rule ref="rulesets/design.xml/NumberOfChildren"/>
3838
<rule ref="rulesets/design.xml/DepthOfInheritance"/>
39-
<rule ref="rulesets/design.xml/CouplingBetweenObjects"/>
4039
<rule ref="rulesets/design.xml/DevelopmentCodeFragment"/>
40+
<rule ref="rulesets/design.xml/CouplingBetweenObjects">
41+
<properties>
42+
<property name="maximum" value="15"/>
43+
</properties>
44+
</rule>
4145

4246
<rule ref="rulesets/naming.xml/LongVariable"/>
4347
<rule ref="rulesets/naming.xml/ShortVariable">

phpstan.neon.dist

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ parameters:
77
- '#method#'
88
- '#expects#'
99
- '#should return#'
10+
- '#type mixed supplied#'
1011
- '#not specify its types#'
1112
- '#no value type specified#'
1213
reportUnmatchedIgnoredErrors: false
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace TinyBlocks\Mapper\Internal\Mappers\Object\Casters;
6+
7+
use ArrayIterator;
8+
9+
final readonly class ArrayIteratorCaster implements Caster
10+
{
11+
public function castValue(mixed $value): ArrayIterator
12+
{
13+
return new ArrayIterator($value);
14+
}
15+
}

src/Internal/Mappers/Object/Casters/TypeCaster.php renamed to src/Internal/Mappers/Object/Casters/Caster.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,14 @@
99
/**
1010
* Responsible for applying a cast to values, based on a specific type.
1111
*/
12-
interface TypeCaster
12+
interface Caster
1313
{
1414
/**
15-
* Applies a cast to the provided value.
15+
* Casts the given value to a specific type.
1616
*
1717
* @param mixed $value The value to be cast.
1818
* @return mixed The cast value.
19-
* @throws InvalidCast Thrown when the value cannot be cast to the expected type.
19+
* @throws InvalidCast If the value cannot be cast to the expected type.
2020
*/
21-
public function applyCast(mixed $value): mixed;
21+
public function castValue(mixed $value): mixed;
2222
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace TinyBlocks\Mapper\Internal\Mappers\Object\Casters;
6+
7+
use ArrayIterator;
8+
use DateTimeInterface;
9+
use Generator;
10+
use ReflectionParameter;
11+
use TinyBlocks\Collection\Collectible;
12+
use UnitEnum;
13+
14+
final readonly class CasterHandler
15+
{
16+
public function __construct(private ReflectionParameter $parameter)
17+
{
18+
}
19+
20+
public function castValue(mixed $value): mixed
21+
{
22+
$class = $this->parameter->getType()->getName();
23+
24+
$caster = match (true) {
25+
$class === Generator::class, => new GeneratorCaster(),
26+
$class === ArrayIterator::class, => new ArrayIteratorCaster(),
27+
is_subclass_of($class, UnitEnum::class) => new EnumCaster(class: $class),
28+
is_subclass_of($class, Collectible::class) => new CollectionCaster(class: $class),
29+
is_subclass_of($class, DateTimeInterface::class) => new DateTimeCaster(),
30+
default => new DefaultCaster(class: $class)
31+
};
32+
33+
return $caster->castValue(value: $value);
34+
}
35+
}

src/Internal/Mappers/Object/Casters/CastingHandler.php

Lines changed: 0 additions & 39 deletions
This file was deleted.
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace TinyBlocks\Mapper\Internal\Mappers\Object\Casters;
6+
7+
use TinyBlocks\Collection\Collectible;
8+
use TinyBlocks\Mapper\Internal\Mappers\Object\ObjectMapper;
9+
use TinyBlocks\Mapper\Internal\Mappers\Object\Reflector;
10+
use TinyBlocks\Mapper\IterableMapper;
11+
12+
final readonly class CollectionCaster implements Caster
13+
{
14+
public function __construct(private string $class)
15+
{
16+
}
17+
18+
public function castValue(mixed $value): Collectible
19+
{
20+
$reflectionClass = Reflector::reflectFrom(class: $this->class);
21+
/** @var IterableMapper & Collectible $instance */
22+
$instance = $reflectionClass->newInstanceWithoutConstructor();
23+
24+
$type = $instance->getType();
25+
26+
if ($type === $this->class) {
27+
return $instance->createFrom(elements: $value);
28+
}
29+
30+
$mapped = [];
31+
32+
foreach ($value as $item) {
33+
$mapped[] = (new ObjectMapper())->map(iterable: $item, class: $type);
34+
}
35+
36+
return $instance->createFrom(elements: $mapped);
37+
}
38+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace TinyBlocks\Mapper\Internal\Mappers\Object\Casters;
6+
7+
use DateTimeImmutable;
8+
9+
final readonly class DateTimeCaster implements Caster
10+
{
11+
public function castValue(mixed $value): DateTimeImmutable
12+
{
13+
return new DateTimeImmutable($value);
14+
}
15+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace TinyBlocks\Mapper\Internal\Mappers\Object\Casters;
6+
7+
use TinyBlocks\Mapper\Internal\Mappers\Object\ObjectMapper;
8+
9+
final readonly class DefaultCaster implements Caster
10+
{
11+
public function __construct(private string $class)
12+
{
13+
}
14+
15+
public function castValue(mixed $value): mixed
16+
{
17+
if (!class_exists($this->class)) {
18+
return $value;
19+
}
20+
21+
return (new ObjectMapper())->map(iterable: $value, class: $this->class);
22+
}
23+
}

0 commit comments

Comments
 (0)