Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
{
"name": "tiny-blocks/mapper",
"type": "library",
"version": "1.0.4",
"license": "MIT",
"homepage": "https://github.com/tiny-blocks/mapper",
"description": "Allows mapping data between different formats, such as JSON, arrays, and DTOs, providing flexibility in transforming and serializing information.",
Expand Down Expand Up @@ -44,7 +43,7 @@
},
"require": {
"php": "^8.3",
"tiny-blocks/collection": "1.9.0"
"tiny-blocks/collection": "^1"
},
"require-dev": {
"phpmd/phpmd": "^2.15",
Expand Down
6 changes: 5 additions & 1 deletion phpmd.xml
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,12 @@
<rule ref="rulesets/design.xml/EvalExpression"/>
<rule ref="rulesets/design.xml/NumberOfChildren"/>
<rule ref="rulesets/design.xml/DepthOfInheritance"/>
<rule ref="rulesets/design.xml/CouplingBetweenObjects"/>
<rule ref="rulesets/design.xml/DevelopmentCodeFragment"/>
<rule ref="rulesets/design.xml/CouplingBetweenObjects">
<properties>
<property name="maximum" value="15"/>
</properties>
</rule>

<rule ref="rulesets/naming.xml/LongVariable"/>
<rule ref="rulesets/naming.xml/ShortVariable">
Expand Down
1 change: 1 addition & 0 deletions phpstan.neon.dist
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ parameters:
- '#method#'
- '#expects#'
- '#should return#'
- '#type mixed supplied#'
- '#not specify its types#'
- '#no value type specified#'
reportUnmatchedIgnoredErrors: false
15 changes: 15 additions & 0 deletions src/Internal/Mappers/Object/Casters/ArrayIteratorCaster.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

declare(strict_types=1);

namespace TinyBlocks\Mapper\Internal\Mappers\Object\Casters;

use ArrayIterator;

final readonly class ArrayIteratorCaster implements Caster
{
public function castValue(mixed $value): ArrayIterator
{
return new ArrayIterator($value);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,14 @@
/**
* Responsible for applying a cast to values, based on a specific type.
*/
interface TypeCaster
interface Caster
{
/**
* Applies a cast to the provided value.
* Casts the given value to a specific type.
*
* @param mixed $value The value to be cast.
* @return mixed The cast value.
* @throws InvalidCast Thrown when the value cannot be cast to the expected type.
* @throws InvalidCast If the value cannot be cast to the expected type.
*/
public function applyCast(mixed $value): mixed;
public function castValue(mixed $value): mixed;
}
35 changes: 35 additions & 0 deletions src/Internal/Mappers/Object/Casters/CasterHandler.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

declare(strict_types=1);

namespace TinyBlocks\Mapper\Internal\Mappers\Object\Casters;

use ArrayIterator;
use DateTimeInterface;
use Generator;
use ReflectionParameter;
use TinyBlocks\Collection\Collectible;
use UnitEnum;

final readonly class CasterHandler
{
public function __construct(private ReflectionParameter $parameter)
{
}

public function castValue(mixed $value): mixed
{
$class = $this->parameter->getType()->getName();

$caster = match (true) {
$class === Generator::class, => new GeneratorCaster(),
$class === ArrayIterator::class, => new ArrayIteratorCaster(),
is_subclass_of($class, UnitEnum::class) => new EnumCaster(class: $class),
is_subclass_of($class, Collectible::class) => new CollectionCaster(class: $class),
is_subclass_of($class, DateTimeInterface::class) => new DateTimeCaster(),
default => new DefaultCaster(class: $class)
};

return $caster->castValue(value: $value);
}
}
39 changes: 0 additions & 39 deletions src/Internal/Mappers/Object/Casters/CastingHandler.php

This file was deleted.

38 changes: 38 additions & 0 deletions src/Internal/Mappers/Object/Casters/CollectionCaster.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

declare(strict_types=1);

namespace TinyBlocks\Mapper\Internal\Mappers\Object\Casters;

use TinyBlocks\Collection\Collectible;
use TinyBlocks\Mapper\Internal\Mappers\Object\ObjectMapper;
use TinyBlocks\Mapper\Internal\Mappers\Object\Reflector;
use TinyBlocks\Mapper\IterableMapper;

final readonly class CollectionCaster implements Caster
{
public function __construct(private string $class)
{
}

public function castValue(mixed $value): Collectible
{
$reflectionClass = Reflector::reflectFrom(class: $this->class);
/** @var IterableMapper & Collectible $instance */
$instance = $reflectionClass->newInstanceWithoutConstructor();

$type = $instance->getType();

if ($type === $this->class) {
return $instance->createFrom(elements: $value);
}

$mapped = [];

foreach ($value as $item) {
$mapped[] = (new ObjectMapper())->map(iterable: $item, class: $type);
}

return $instance->createFrom(elements: $mapped);
}
}
15 changes: 15 additions & 0 deletions src/Internal/Mappers/Object/Casters/DateTimeCaster.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

declare(strict_types=1);

namespace TinyBlocks\Mapper\Internal\Mappers\Object\Casters;

use DateTimeImmutable;

final readonly class DateTimeCaster implements Caster
{
public function castValue(mixed $value): DateTimeImmutable
{
return new DateTimeImmutable($value);
}
}
23 changes: 23 additions & 0 deletions src/Internal/Mappers/Object/Casters/DefaultCaster.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

declare(strict_types=1);

namespace TinyBlocks\Mapper\Internal\Mappers\Object\Casters;

use TinyBlocks\Mapper\Internal\Mappers\Object\ObjectMapper;

final readonly class DefaultCaster implements Caster
{
public function __construct(private string $class)
{
}

public function castValue(mixed $value): mixed
{
if (!class_exists($this->class)) {
return $value;
}

return (new ObjectMapper())->map(iterable: $value, class: $this->class);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,19 @@

declare(strict_types=1);

namespace TinyBlocks\Mapper\Internal\Mappers\Object\Casters\Types;
namespace TinyBlocks\Mapper\Internal\Mappers\Object\Casters;

use ReflectionEnum;
use TinyBlocks\Mapper\Internal\Exceptions\InvalidCast;
use TinyBlocks\Mapper\Internal\Mappers\Object\Casters\TypeCaster;
use UnitEnum;

final readonly class EnumCaster implements TypeCaster
final readonly class EnumCaster implements Caster
{
public function __construct(public string $class)
{
}

public function applyCast(mixed $value): UnitEnum
public function castValue(mixed $value): UnitEnum
{
$reflectionEnum = new ReflectionEnum($this->class);

Expand Down
23 changes: 23 additions & 0 deletions src/Internal/Mappers/Object/Casters/GeneratorCaster.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

declare(strict_types=1);

namespace TinyBlocks\Mapper\Internal\Mappers\Object\Casters;

use Generator;

final readonly class GeneratorCaster implements Caster
{
public function castValue(mixed $value): Generator
{
if (is_iterable($value)) {
foreach ($value as $item) {
yield $item;
}

return;
}

yield $value;
}
}
16 changes: 0 additions & 16 deletions src/Internal/Mappers/Object/Casters/Types/ArrayIteratorCaster.php

This file was deleted.

16 changes: 0 additions & 16 deletions src/Internal/Mappers/Object/Casters/Types/DateTimeCaster.php

This file was deleted.

48 changes: 0 additions & 48 deletions src/Internal/Mappers/Object/Casters/Types/DefaultCaster.php

This file was deleted.

24 changes: 0 additions & 24 deletions src/Internal/Mappers/Object/Casters/Types/GeneratorCaster.php

This file was deleted.

Loading
Loading