8000 [Serializer] Fine-tune `ContextBuilder::withContext()` by chalasr · Pull Request #46179 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[Serializer] Fine-tune ContextBuilder::withContext() #46179

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 26, 2022
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\Serializer\Context;

/**
* Common interface for context builders.
*
* @author Mathias Arlaud <mathias.arlaud@gmail.com>
* @author Robin Chalas <robin.chalas@gmail.com>
*/
interface ContextBuilderInterface
{
/**
* @param self|array<string, mixed> $context
*/
public function withContext(self|array $context): static;

/**
* @return array<string, mixed>
*/
public function toArray(): array;
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,14 @@ protected function with(string $key, mixed $value): static
}

/**
* @param array<string, mixed> $context
* @param ContextBuilderInterface|array<string, mixed> $context
*/
public function withContext(array $context): static
public function withContext(ContextBuilderInterface|array $context): static
{
if ($context instanceof ContextBuilderInterface) {
$context = $context->toArray();
}

$instance = new static();
$instance->context = array_merge($this->context, $context);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

namespace Symfony\Component\Serializer\Context\Encoder;

use Symfony\Component\Serializer\Context\ContextBuilderInterface;
use Symfony\Component\Serializer\Context\ContextBuilderTrait;
use Symfony\Component\Serializer\Encoder\CsvEncoder;
use Symfony\Component\Serializer\Exception\InvalidArgumentException;
Expand All @@ -20,7 +21,7 @@
*
* @author Mathias Arlaud <mathias.arlaud@gmail.com>
*/
final class CsvEncoderContextBuilder
final class CsvEncoderContextBuilder implements ContextBuilderInterface
{
use ContextBuilderTrait;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

namespace Symfony\Component\Serializer\Context\Encoder;

use Symfony\Component\Serializer\Context\ContextBuilderInterface;
use Symfony\Component\Serializer\Context\ContextBuilderTrait;
use Symfony\Component\Serializer\Encoder\JsonDecode;
use Symfony\Component\Serializer\Encoder\JsonEncode;
Expand All @@ -20,7 +21,7 @@
*
* @author Mathias Arlaud <mathias.arlaud@gmail.com>
*/
final class JsonEncoderContextBuilder
final class JsonEncoderContextBuilder implements ContextBuilderInterface
{
use ContextBuilderTrait;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

namespace Symfony\Component\Serializer\Context\Encoder;

use Symfony\Component\Serializer\Context\ContextBuilderInterface;
use Symfony\Component\Serializer\Context\ContextBuilderTrait;
use Symfony\Component\Serializer\Encoder\XmlEncoder;

Expand All @@ -19,7 +20,7 @@
*
* @author Mathias Arlaud <mathias.arlaud@gmail.com>
*/
final class XmlEncoderContextBuilder
final class XmlEncoderContextBuilder implements ContextBuilderInterface
{
use ContextBuilderTrait;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

namespace Symfony\Component\Serializer\Context\Encoder;

use Symfony\Component\Serializer\Context\ContextBuilderInterface;
use Symfony\Component\Serializer\Context\ContextBuilderTrait;
use Symfony\Component\Serializer\Encoder\YamlEncoder;

Expand All @@ -19,7 +20,7 @@
*
* @author Mathias Arlaud <mathias.arlaud@gmail.com>
*/
final class YamlEncoderContextBuilder
final class YamlEncoderContextBuilder implements ContextBuilderInterface
{
use ContextBuilderTrait;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

namespace Symfony\Component\Serializer\Context\Normalizer;

use Symfony\Component\Serializer\Context\ContextBuilderInterface;
use Symfony\Component\Serializer\Context\ContextBuilderTrait;
use Symfony\Component\Serializer\Exception\InvalidArgumentException;
use Symfony\Component\Serializer\Normalizer\AbstractNormalizer;
Expand All @@ -20,7 +21,7 @@
*
* @author Mathias Arlaud <mathias.arlaud@gmail.com>
*/
abstract class AbstractNormalizerContextBuilder
abstract class AbstractNormalizerContextBuilder implements ContextBuilderInterface
{
use ContextBuilderTrait;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

namespace Symfony\Component\Serializer\Context\Normalizer;

use Symfony\Component\Serializer\Context\ContextBuilderInterface;
use Symfony\Component\Serializer\Context\ContextBuilderTrait;
use Symfony\Component\Serializer\Normalizer\ConstraintViolationListNormalizer;

Expand All @@ -19,7 +20,7 @@
*
* @author Mathias Arlaud <mathias.arlaud@gmail.com>
*/
final class ConstraintViolationListNormalizerContextBuilder
final class ConstraintViolationListNormalizerContextBuilder implements ContextBuilderInterface
{
use ContextBuilderTrait;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

namespace Symfony\Component\Serializer\Context\Normalizer;

use Symfony\Component\Serializer\Context\ContextBuilderInterface;
use Symfony\Component\Serializer\Context\ContextBuilderTrait;
use Symfony\Component\Serializer\Normalizer\DateIntervalNormalizer;

Expand All @@ -19,7 +20,7 @@
*
* @author Mathias Arlaud <mathias.arlaud@gmail.com>
*/
final class DateIntervalNormalizerContextBuilder
final class DateIntervalNormalizerContextBuilder implements ContextBuilderInterface
{
use ContextBuilderTrait;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

namespace Symfony\Component\Serializer\Context\Normalizer;

use Symfony\Component\Serializer\Context\ContextBuilderInterface;
use Symfony\Component\Serializer\Context\ContextBuilderTrait;
use Symfony\Component\Serializer\Exception\InvalidArgumentException;
use Symfony\Component\Serializer\Normalizer\DateTimeNormalizer;
Expand All @@ -20,7 +21,7 @@
*
* @author Mathias Arlaud <mathias.arlaud@gmail.com>
*/
final class DateTimeNormalizerContextBuilder
final class DateTimeNormalizerContextBuilder implements ContextBuilderInterface
{
use ContextBuilderTrait;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

namespace Symfony\Component\Serializer\Context\Normalizer;

use Symfony\Component\Serializer\Context\ContextBuilderInterface;
use Symfony\Component\Serializer\Context\ContextBuilderTrait;
use Symfony\Component\Serializer\Normalizer\FormErrorNormalizer;

Expand All @@ -19,7 +20,7 @@
*
* @author Mathias Arlaud <mathias.arlaud@gmail.com>
*/
final class FormErrorNormalizerContextBuilder
final class FormErrorNormalizerContextBuilder implements ContextBuilderInterface
{
use ContextBuilderTrait;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

namespace Symfony\Component\Serializer\Context\Normalizer;

use Symfony\Component\Serializer\Context\ContextBuilderInterface;
use Symfony\Component\Serializer\Context\ContextBuilderTrait;
use Symfony\Component\Serializer\Normalizer\ProblemNormalizer;

Expand All @@ -19,7 +20,7 @@
*
* @author Mathias Arlaud <mathias.arlaud@gmail.com>
*/
final class ProblemNormalizerContextBuilder
final class ProblemNormalizerContextBuilder implements ContextBuilderInterface
{
use ContextBuilderTrait;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

namespace Symfony\Component\Serializer\Context\Normalizer;

use Symfony\Component\Serializer\Context\ContextBuilderInterface;
use Symfony\Component\Serializer\Context\ContextBuilderTrait;
use Symfony\Component\Serializer\Exception\InvalidArgumentException;
use Symfony\Component\Serializer\Normalizer\UidNormalizer;
Expand All @@ -20,7 +21,7 @@
*
* @author Mathias Arlaud <mathias.arlaud@gmail.com>
*/
final class UidNormalizerContextBuilder
final class UidNormalizerContextBuilder implements ContextBuilderInterface
{
use ContextBuilderTrait;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

use Symfony\Component\PropertyAccess\Exception\InvalidPropertyPathException;
use Symfony\Component\PropertyAccess\PropertyPath;
use Symfony\Component\Serializer\Context\ContextBuilderInterface;
use Symfony\Component\Serializer\Context\ContextBuilderTrait;
use Symfony\Component\Serializer\Exception\InvalidArgumentException;
use Symfony\Component\Serializer\Normalizer\UnwrappingDenormalizer;
Expand All @@ -22,7 +23,7 @@
*
* @author Mathias Arlaud <mathias.arlaud@gmail.com>
*/
final class UnwrappingDenormalizerContextBuilder
final class UnwrappingDenormalizerContextBuilder implements ContextBuilderInterface
{
use ContextBuilderTrait;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
*
* @author Mathias Arlaud <mathias.arlaud@gmail.com>
*/
final class SerializerContextBuilder
final class SerializerContextBuilder implements ContextBuilderInterface
{
use ContextBuilderTrait;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
namespace Symfony\Component\Serializer\Tests\Context;

use PHPUnit\Framework\TestCase;
use Symfony\Component\Serializer\Context\ContextBuilderInterface;
use Symfony\Component\Serializer\Context\ContextBuilderTrait;

/**
Expand All @@ -21,13 +22,17 @@ class ContextBuilderTraitTest extends TestCase
{
public function testWithContext()
{
$contextBuilder = new class() {
$contextBuilder = new class() implements ContextBuilderInterface {
use ContextBuilderTrait;
};

$context = $contextBuilder->withContext(['foo' => 'bar'])->toArray();

$this->assertSame(['foo' => 'bar'], $context);

$withContextBuilderObject = $contextBuilder->withContext($contextBuilder->withContext(['foo' => 'bar']))->toArray();

$this->assertSame(['foo' => 'bar'], $withContextBuilderObject);
}

public function testWith()
Expand Down
0