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
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ jobs:

strategy:
matrix:
php: ['8.1']
php: ['8.3']

steps:
- name: Checkout Code
Expand Down
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
=====

* (feature) Add `SimpleNormalizer::normalizeMap()`.
* (feature) Add helper VO `ValueWithContext`.


1.1.1
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
],
"homepage": "https://github.com/21TORR/simple-normalizer",
"require": {
"php": ">= 8.1",
"php": ">= 8.3",
"21torr/bundle-helpers": "^2.1",
"symfony/dependency-injection": "^6.1",
"symfony/http-kernel": "^6.1"
Expand Down
13 changes: 13 additions & 0 deletions src/Data/ValueWithContext.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php declare(strict_types=1);

namespace Torr\SimpleNormalizer\Data;

final readonly class ValueWithContext
{
/**
*/
public function __construct (
public mixed $value,
public array $context = [],
) {}
}
32 changes: 32 additions & 0 deletions src/Normalizer/ObjectNormalizer/ValueWithContextNormalizer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php declare(strict_types=1);

namespace Torr\SimpleNormalizer\Normalizer\ObjectNormalizer;

use Torr\SimpleNormalizer\Data\ValueWithContext;
use Torr\SimpleNormalizer\Normalizer\SimpleNormalizer;
use Torr\SimpleNormalizer\Normalizer\SimpleObjectNormalizerInterface;

final class ValueWithContextNormalizer implements SimpleObjectNormalizerInterface
{
/**
* @inheritDoc
*/
public function normalize (object $value, array $context, SimpleNormalizer $normalizer) : mixed
{
\assert($value instanceof ValueWithContext);

return $normalizer->normalize(
$value->value,
\array_replace($context, $value->context),
);
}


/**
* @inheritDoc
*/
public static function getNormalizedType () : string
{
return ValueWithContext::class;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php declare(strict_types=1);

namespace Tests\Torr\SimpleNormalizer\Normalizer\ObjectNormalizer;

use PHPUnit\Framework\TestCase;
use Symfony\Component\DependencyInjection\ServiceLocator;
use Tests\Torr\SimpleNormalizer\Fixture\DummyVO;
use Torr\SimpleNormalizer\Data\ValueWithContext;
use Torr\SimpleNormalizer\Normalizer\ObjectNormalizer\ValueWithContextNormalizer;
use Torr\SimpleNormalizer\Normalizer\SimpleNormalizer;
use Torr\SimpleNormalizer\Normalizer\SimpleObjectNormalizerInterface;

final class ValueWithContextNormalizerTest extends TestCase
{
/**
*
*/
public function testContextPassing () : void
{
$value = new ValueWithContext(
new DummyVO(5),
[
"o" => "hai",
],
);

$dummyNormalizer = $this->createMock(SimpleObjectNormalizerInterface::class);
$dummyNormalizer
->expects(self::once())
->method("normalize")
->with(
$value->value,
[
"test" => 123,
"o" => "hai",
],
);

$normalizer = new SimpleNormalizer(new ServiceLocator([
ValueWithContext::class => fn () => new ValueWithContextNormalizer(),
DummyVO::class => fn () => $dummyNormalizer,
]));

$normalizer->normalize($value, [
"test" => 123,
"o" => 5,
]);
}
}