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: 2 additions & 1 deletion src/Dto/AbstractDto.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace PhpCollective\Dto\Dto;

use PhpCollective\Dto\Transformer\TransformerRegistry;
use PhpCollective\Dto\Utility\Json;
use RuntimeException;

Expand All @@ -18,7 +19,7 @@ abstract class AbstractDto extends Dto
*/
public function fromArray(array $data, bool $ignoreMissing = false, ?string $type = null): static
{
if ($type === null && static::HAS_FAST_PATH) {
if ($type === null && static::HAS_FAST_PATH && !TransformerRegistry::hasAnyCaster()) {
if (!$ignoreMissing) {
$this->validateFieldNames($data);
}
Expand Down
4 changes: 2 additions & 2 deletions src/Dto/Dto.php
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,7 @@ public static function create(?array $data = null, bool $ignoreMissing = false,
public function __construct(?array $data = null, bool $ignoreMissing = false, ?string $type = null)
{
if ($data) {
if ($type === null && static::HAS_FAST_PATH && !TransformerRegistry::hasAny()) {
if ($type === null && static::HAS_FAST_PATH && !TransformerRegistry::hasAnyCaster()) {
if (!$ignoreMissing) {
$this->validateFieldNames($data);
}
Expand Down Expand Up @@ -340,7 +340,7 @@ protected function _toArrayInternal(?string $type = null, ?array $fields = null,
if (
!$touched && $fields === null && static::HAS_FAST_PATH
&& ($type === null || $type === static::TYPE_CAMEL || $type === static::TYPE_DEFAULT)
&& !TransformerRegistry::hasAny()
&& !TransformerRegistry::hasAnySerializer()
) {
return $this->toArrayFast();
}
Expand Down
22 changes: 22 additions & 0 deletions src/Transformer/TransformerRegistry.php
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,28 @@ public static function hasAny(): bool
return self::$casters !== [] || self::$serializers !== [];
}

/**
* Check whether any caster is currently registered.
* Used to bypass generated fast paths on input (hydration).
*
* @return bool
*/
public static function hasAnyCaster(): bool
{
return self::$casters !== [];
}

/**
* Check whether any serializer is currently registered.
* Used to bypass generated fast paths on output (serialization).
*
* @return bool
*/
public static function hasAnySerializer(): bool
{
return self::$serializers !== [];
}

/**
* @param string $type
*
Expand Down
55 changes: 55 additions & 0 deletions tests/Dto/DtoTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
use PhpCollective\Dto\Test\TestDto\AssociativeCollectionDto;
use PhpCollective\Dto\Test\TestDto\CollectionDto;
use PhpCollective\Dto\Test\TestDto\CustomCollectionDto;
use PhpCollective\Dto\Test\TestDto\FastPathCasterDto;
use PhpCollective\Dto\Test\TestDto\ImmutableCollectionDto;
use PhpCollective\Dto\Test\TestDto\ImmutableDto;
use PhpCollective\Dto\Test\TestDto\MapFromDto;
Expand Down Expand Up @@ -1755,6 +1756,60 @@ public function testMultipleRequiredFieldsErrorMessageFormat(): void
}
}

public function testGlobalCasterAppliesToClassFieldOnFastPathFromArray(): void
{
TransformerRegistry::addCaster(
PlainClass::class,
fn (mixed $value): PlainClass => new PlainClass('cast:' . (string)$value),
);

$dto = (new FastPathCasterDto())->fromArray(['plainData' => 'hello']);

$plain = $dto->getPlainData();
$this->assertInstanceOf(PlainClass::class, $plain);
$this->assertSame('cast:hello', $plain->value);
}

public function testGlobalCasterAppliesToClassFieldOnFastPathConstructor(): void
{
TransformerRegistry::addCaster(
PlainClass::class,
fn (mixed $value): PlainClass => new PlainClass('cast:' . (string)$value),
);

$dto = new FastPathCasterDto(['plainData' => 'hello']);

$plain = $dto->getPlainData();
$this->assertInstanceOf(PlainClass::class, $plain);
$this->assertSame('cast:hello', $plain->value);
}

public function testGlobalSerializerAppliesToFastPathToArray(): void
{
TransformerRegistry::addSerializer(
PlainClass::class,
fn (PlainClass $value): string => 'serialized:' . $value->value,
);

$dto = (new FastPathCasterDto())->setPlainData(new PlainClass('abc'));

$this->assertSame(['plainData' => 'serialized:abc'], $dto->toArray());
}

public function testGlobalSerializerDoesNotDisableFastPathHydration(): void
{
TransformerRegistry::addSerializer(
PlainClass::class,
fn (PlainClass $value): string => 'serialized:' . $value->value,
);

$dto = (new FastPathCasterDto())->fromArray(['plainData' => 'hello']);

$plain = $dto->getPlainData();
$this->assertInstanceOf(PlainClass::class, $plain);
$this->assertSame('hello', $plain->value);
}

public function testGlobalCasterAppliesToClassField(): void
{
TransformerRegistry::addCaster(
Expand Down
122 changes: 122 additions & 0 deletions tests/TestDto/FastPathCasterDto.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
<?php

declare(strict_types=1);

namespace PhpCollective\Dto\Test\TestDto;

use PhpCollective\Dto\Dto\AbstractDto;
use PhpCollective\Dto\Test\Generator\Fixtures\PlainClass;

/**
* Mimics generated output for a DTO with a class-typed field and fast-path methods.
*/
class FastPathCasterDto extends AbstractDto
{
/**
* @var bool
*/
protected const HAS_FAST_PATH = true;

protected ?PlainClass $plainData = null;

/**
* @var array<string, array<string, mixed>>
*/
protected array $_metadata = [
'plainData' => [
'type' => PlainClass::class,
'required' => false,
'defaultValue' => null,
'dto' => false,
'collectionType' => null,
'singularType' => null,
'associative' => false,
'key' => null,
'serialize' => null,
'factory' => null,
'isClass' => true,
'enum' => null,
],
];

/**
* @var array<string, array<string, string>>
*/
protected array $_keyMap = [
'underscored' => [
'plain_data' => 'plainData',
],
'dashed' => [
'plain-data' => 'plainData',
],
];

public function getPlainData(): ?PlainClass
{
return $this->plainData;
}

public function setPlainData(?PlainClass $plainData): self
{
$this->plainData = $plainData;
$this->_touchedFields['plainData'] = true;

return $this;
}

public function hasPlainData(): bool
{
return $this->plainData !== null;
}

/**
* @param array<string, mixed> $data
*
* @return void
*/
protected function setFromArrayFast(array $data): void
{
if (isset($data['plainData'])) {
$value = $data['plainData'];
if (!$value instanceof PlainClass) {
$value = new PlainClass($value);
}
$this->plainData = $value;
$this->_touchedFields['plainData'] = true;
}
}

/**
* @return array<string, mixed>
*/
protected function toArrayFast(): array
{
return [
'plainData' => $this->plainData !== null ? $this->plainData->value : null,
];
}

/**
* @param string|null $type
* @param array<string>|null $fields
* @param bool $touched
*
* @return array<string, mixed>
*/
public function toArray(?string $type = null, ?array $fields = null, bool $touched = false): array
{
return $this->_toArrayInternal($type, $fields, $touched);
}

/**
* @param array<string, mixed> $data
* @param bool $ignoreMissing
* @param string|null $type
*
* @return static
*/
public static function createFromArray(array $data, bool $ignoreMissing = false, ?string $type = null): static
{
return static::_createFromArrayInternal($data, $ignoreMissing, $type);
}
}
Loading