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 CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
# Cycle Typecast Change Log

## 2.2.1 under development
## 3.0.0 under development

- Chg #7, #8: Change PHP constraint in `composer.json` to `8.1 - 8.4`
- Chg #8: Bump minimal PHP version to 8.1
- New #9: Add context to `TypeInterface` convert methods

## 2.2.0 June 17, 2024

Expand Down
4 changes: 2 additions & 2 deletions src/ArrayToStringType.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public function __construct(string $delimiter)
$this->delimiter = $delimiter;
}

public function convertToDatabaseValue(mixed $value): mixed
public function convertToDatabaseValue(mixed $value, UncastContext $context): mixed
{
if ($value === null) {
return null;
Expand All @@ -37,7 +37,7 @@ public function convertToDatabaseValue(mixed $value): mixed
return implode($this->delimiter, $value);
}

public function convertToPhpValue(mixed $value): mixed
public function convertToPhpValue(mixed $value, CastContext $context): mixed
{
if ($value === null) {
return null;
Expand Down
10 changes: 8 additions & 2 deletions src/AttributeTypecastHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
foreach ($reflection->getProperties() as $property) {
$attributes = $property->getAttributes(TypeInterface::class, ReflectionAttribute::IS_INSTANCEOF);
if (empty($attributes)) {
continue;

Check warning on line 31 in src/AttributeTypecastHandler.php

View workflow job for this annotation

GitHub Actions / PHP 8.1-ubuntu-latest

Escaped Mutant for Mutator "Continue_": --- Original +++ New @@ @@ foreach ($reflection->getProperties() as $property) { $attributes = $property->getAttributes(TypeInterface::class, ReflectionAttribute::IS_INSTANCEOF); if (empty($attributes)) { - continue; + break; } $this->types[$property->getName()] = $attributes[0]->newInstance(); }
}
$this->types[$property->getName()] = $attributes[0]->newInstance();
}
Expand All @@ -42,7 +42,7 @@
unset($rules[$key]);
}
}
return $rules;

Check warning on line 45 in src/AttributeTypecastHandler.php

View workflow job for this annotation

GitHub Actions / PHP 8.1-ubuntu-latest

Escaped Mutant for Mutator "ArrayOneItem": --- Original +++ New @@ @@ unset($rules[$key]); } } - return $rules; + return count($rules) > 1 ? array_slice($rules, 0, 1, true) : $rules; } public function cast(array $data) : array {
}

public function cast(array $data): array
Expand All @@ -50,7 +50,10 @@
/** @psalm-var array<non-empty-string, mixed> $data */
foreach ($data as $key => $value) {
if (isset($this->types[$key])) {
$data[$key] = $this->types[$key]->convertToPhpValue($value);
$data[$key] = $this->types[$key]->convertToPhpValue(
$value,
new CastContext($key, $data),
);
}
}
return $data;
Expand All @@ -61,7 +64,10 @@
/** @psalm-var array<non-empty-string, mixed> $data */
foreach ($data as $key => $value) {
if (isset($this->types[$key])) {
$data[$key] = $this->types[$key]->convertToDatabaseValue($value);
$data[$key] = $this->types[$key]->convertToDatabaseValue(
$value,
new UncastContext($key, $data),
);
}
}
return $data;
Expand Down
13 changes: 13 additions & 0 deletions src/CastContext.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

declare(strict_types=1);

namespace Vjik\CycleTypecast;

final class CastContext
{
public function __construct(
public readonly string $property,
public readonly array $data,
) {}
}
6 changes: 4 additions & 2 deletions src/DateTimeImmutable/DateTimeImmutableType.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,13 @@

use DateTimeImmutable;
use InvalidArgumentException;
use Vjik\CycleTypecast\CastContext;
use Vjik\CycleTypecast\TypeInterface;
use Vjik\CycleTypecast\UncastContext;

abstract class DateTimeImmutableType implements TypeInterface
{
public function convertToDatabaseValue(mixed $value): mixed
public function convertToDatabaseValue(mixed $value, UncastContext $context): mixed
{
if ($value === null) {
return null;
Expand All @@ -25,7 +27,7 @@ public function convertToDatabaseValue(mixed $value): mixed

abstract protected function toDatabaseValue(DateTimeImmutable $value): mixed;

public function convertToPhpValue(mixed $value): mixed
public function convertToPhpValue(mixed $value, CastContext $context): mixed
{
if ($value === null) {
return null;
Expand Down
4 changes: 2 additions & 2 deletions src/IntegerEnumType.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public function __construct(private string $enumClass)
{
}

public function convertToDatabaseValue(mixed $value): mixed
public function convertToDatabaseValue(mixed $value, UncastContext $context): mixed
{
if ($value === null) {
return null;
Expand All @@ -31,7 +31,7 @@ public function convertToDatabaseValue(mixed $value): mixed
return $value->value;
}

public function convertToPhpValue(mixed $value): mixed
public function convertToPhpValue(mixed $value, CastContext $context): mixed
{
if ($value === null) {
return null;
Expand Down
4 changes: 2 additions & 2 deletions src/StringEnumType.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public function __construct(private string $enumClass)
{
}

public function convertToDatabaseValue(mixed $value): mixed
public function convertToDatabaseValue(mixed $value, UncastContext $context): mixed
{
if ($value === null) {
return null;
Expand All @@ -31,7 +31,7 @@ public function convertToDatabaseValue(mixed $value): mixed
return $value->value;
}

public function convertToPhpValue(mixed $value): mixed
public function convertToPhpValue(mixed $value, CastContext $context): mixed
{
if ($value === null) {
return null;
Expand Down
4 changes: 2 additions & 2 deletions src/TypeInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

interface TypeInterface
{
public function convertToDatabaseValue(mixed $value): mixed;
public function convertToDatabaseValue(mixed $value, UncastContext $context): mixed;

public function convertToPhpValue(mixed $value): mixed;
public function convertToPhpValue(mixed $value, CastContext $context): mixed;
}
14 changes: 10 additions & 4 deletions src/Typecaster.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

namespace Vjik\CycleTypecast;

use function array_key_exists;

final class Typecaster
{
/**
Expand All @@ -23,8 +25,10 @@ public function prepareAfterExtract(array $data): array
{
foreach ($this->config as $column => $type) {
if (array_key_exists($column, $data)) {
/** @var mixed */
$data[$column] = $type->convertToDatabaseValue($data[$column]);
$data[$column] = $type->convertToDatabaseValue(
$data[$column],
new UncastContext($column, $data),
);
}
}
return $data;
Expand All @@ -34,8 +38,10 @@ public function prepareBeforeHydrate(array $data): array
{
foreach ($this->config as $column => $type) {
if (array_key_exists($column, $data)) {
/** @var mixed */
$data[$column] = $type->convertToPhpValue($data[$column]);
$data[$column] = $type->convertToPhpValue(
$data[$column],
new CastContext($column, $data),
);
}
}
return $data;
Expand Down
13 changes: 13 additions & 0 deletions src/UncastContext.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

declare(strict_types=1);

namespace Vjik\CycleTypecast;

final class UncastContext
{
public function __construct(
public readonly string $property,
public readonly array $data,
) {}
}
6 changes: 4 additions & 2 deletions src/UuidString/UuidStringType.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,13 @@
use InvalidArgumentException;
use Ramsey\Uuid\Uuid;
use Ramsey\Uuid\UuidInterface;
use Vjik\CycleTypecast\CastContext;
use Vjik\CycleTypecast\TypeInterface;
use Vjik\CycleTypecast\UncastContext;

abstract class UuidStringType implements TypeInterface
{
public function convertToDatabaseValue(mixed $value): mixed
public function convertToDatabaseValue(mixed $value, UncastContext $context): mixed
{
if ($value === null) {
return null;
Expand All @@ -33,7 +35,7 @@ public function convertToDatabaseValue(mixed $value): mixed

abstract protected function toDatabaseValue(UuidInterface $value): mixed;

public function convertToPhpValue(mixed $value): mixed
public function convertToPhpValue(mixed $value, CastContext $context): mixed
{
if ($value === null) {
return null;
Expand Down
13 changes: 8 additions & 5 deletions tests/ArrayToStringTypeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\TestCase;
use Vjik\CycleTypecast\ArrayToStringType;
use Vjik\CycleTypecast\Tests\Support\ContextFactory;

final class ArrayToStringTypeTest extends TestCase
{
Expand All @@ -34,23 +35,25 @@ public function testBase(?string $databaseValue, ?array $entityValue): void
{
$type = new ArrayToStringType(',');

$this->assertSame($databaseValue, $type->convertToDatabaseValue($entityValue));
$this->assertSame($entityValue, $type->convertToPhpValue($databaseValue));
$this->assertSame($databaseValue, $type->convertToDatabaseValue($entityValue, ContextFactory::uncastContext()));
$this->assertSame($entityValue, $type->convertToPhpValue($databaseValue, ContextFactory::castContext()));
}

public function testConvertToDatabaseValueIncorrectValue(): void
{
$type = new ArrayToStringType(',');
$context = ContextFactory::uncastContext();

$this->expectException(InvalidArgumentException::class);
$type->convertToDatabaseValue(42);
$type->convertToDatabaseValue(42, $context);
}

public function testConertToPhpValueWithIntegerValue(): void
public function testConvertToPhpValueWithIntegerValue(): void
{
$type = new ArrayToStringType(',');
$context = ContextFactory::castContext();

$this->expectException(InvalidArgumentException::class);
$type->convertToPhpValue(42);
$type->convertToPhpValue(42, $context);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

declare(strict_types=1);

namespace Vjik\CycleTypecast\Tests;
namespace Vjik\CycleTypecast\Tests\AttributeTypecastHandler;

use Cycle\ORM\SchemaInterface;
use PHPUnit\Framework\TestCase;
Expand Down
57 changes: 57 additions & 0 deletions tests/AttributeTypecastHandler/Context/ContextTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?php

declare(strict_types=1);

namespace Vjik\CycleTypecast\Tests\AttributeTypecastHandler\Context;

use Cycle\ORM\SchemaInterface;
use PHPUnit\Framework\TestCase;
use Vjik\CycleTypecast\AttributeTypecastHandler;

use Vjik\CycleTypecast\CastContext;
use Vjik\CycleTypecast\UncastContext;

use function PHPUnit\Framework\assertInstanceOf;
use function PHPUnit\Framework\assertSame;

final class ContextTest extends TestCase
{
public function testBase(): void
{
$schema = $this->createMock(SchemaInterface::class);
$schema
->method('define')
->with('role_user', SchemaInterface::ENTITY)
->willReturn(Entity::class);

$typecastHandler = new AttributeTypecastHandler($schema, 'role_user');

$context = $typecastHandler->uncast([
'id' => '1f2d3897-a226-4eec-bd2c-d0145ef25df9',
'name' => 'John',
])['id'];
assertInstanceOf(UncastContext::class, $context);
assertSame('id', $context->property);
assertSame(
[
'id' => '1f2d3897-a226-4eec-bd2c-d0145ef25df9',
'name' => 'John',
],
$context->data
);

$context = $typecastHandler->cast([
'id' => '2f2d3897-a226-4eec-bd2c-d0145ef25df9',
'name' => 'Doe',
])['id'];
assertInstanceOf(CastContext::class, $context);
assertSame('id', $context->property);
assertSame(
[
'id' => '2f2d3897-a226-4eec-bd2c-d0145ef25df9',
'name' => 'Doe',
],
$context->data
);
}
}
24 changes: 24 additions & 0 deletions tests/AttributeTypecastHandler/Context/ContextType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

declare(strict_types=1);

namespace Vjik\CycleTypecast\Tests\AttributeTypecastHandler\Context;

use Attribute;
use Vjik\CycleTypecast\CastContext;
use Vjik\CycleTypecast\TypeInterface;
use Vjik\CycleTypecast\UncastContext;

#[Attribute(Attribute::TARGET_PROPERTY)]
final class ContextType implements TypeInterface
{
public function convertToDatabaseValue(mixed $value, UncastContext $context): mixed
{
return $context;
}

public function convertToPhpValue(mixed $value, CastContext $context): mixed
{
return $context;
}
}
13 changes: 13 additions & 0 deletions tests/AttributeTypecastHandler/Context/Entity.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

declare(strict_types=1);

namespace Vjik\CycleTypecast\Tests\AttributeTypecastHandler\Context;

final class Entity
{
#[ContextType]
public string $id = '';

public string $name = '';
}
Loading