Skip to content

Commit

Permalink
[Twig Hooks] Introduce the ScalarDataBag object for the HookableMetad…
Browse files Browse the repository at this point in the history
…ata configuration data
  • Loading branch information
jakubtobiasz committed Apr 29, 2024
1 parent 4f00f19 commit 0ab8b3b
Show file tree
Hide file tree
Showing 10 changed files with 75 additions and 12 deletions.
5 changes: 5 additions & 0 deletions src/TwigHooks/src/Bag/DataBag.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@ class DataBag implements DataBagInterface
*/
public function __construct(private array $container = [])
{
foreach ($container as $key => $value) {
if (!is_string($key)) {
throw new \InvalidArgumentException('The key must be a string.');
}
}
}

public function offsetExists(mixed $offset): bool
Expand Down
38 changes: 38 additions & 0 deletions src/TwigHooks/src/Bag/ScalarDataBag.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

declare(strict_types=1);

namespace Sylius\TwigHooks\Bag;

class ScalarDataBag extends DataBag implements ScalarDataBagInterface
{
public function __construct(array $container = [])
{
$this->validateValues($container);

parent::__construct($container);
}

public function offsetSet(mixed $offset, mixed $value): void
{
if (!is_scalar($value)) {
throw new \InvalidArgumentException('The value must be a scalar.');
}

parent::offsetSet($offset, $value);
}

private function validateValues(array $values): void

Check failure on line 25 in src/TwigHooks/src/Bag/ScalarDataBag.php

View workflow job for this annotation

GitHub Actions / Continuous Integration (PHP 8.1 / Symfony ^5.4)

Method Sylius\TwigHooks\Bag\ScalarDataBag::validateValues() has parameter $values with no value type specified in iterable type array.

Check failure on line 25 in src/TwigHooks/src/Bag/ScalarDataBag.php

View workflow job for this annotation

GitHub Actions / Continuous Integration (PHP 8.1 / Symfony ^6.4)

Method Sylius\TwigHooks\Bag\ScalarDataBag::validateValues() has parameter $values with no value type specified in iterable type array.

Check failure on line 25 in src/TwigHooks/src/Bag/ScalarDataBag.php

View workflow job for this annotation

GitHub Actions / Continuous Integration (PHP 8.2 / Symfony ^5.4)

Method Sylius\TwigHooks\Bag\ScalarDataBag::validateValues() has parameter $values with no value type specified in iterable type array.

Check failure on line 25 in src/TwigHooks/src/Bag/ScalarDataBag.php

View workflow job for this annotation

GitHub Actions / Continuous Integration (PHP 8.2 / Symfony ^6.4)

Method Sylius\TwigHooks\Bag\ScalarDataBag::validateValues() has parameter $values with no value type specified in iterable type array.

Check failure on line 25 in src/TwigHooks/src/Bag/ScalarDataBag.php

View workflow job for this annotation

GitHub Actions / Continuous Integration (PHP 8.3 / Symfony ^5.4)

Method Sylius\TwigHooks\Bag\ScalarDataBag::validateValues() has parameter $values with no value type specified in iterable type array.

Check failure on line 25 in src/TwigHooks/src/Bag/ScalarDataBag.php

View workflow job for this annotation

GitHub Actions / Continuous Integration (PHP 8.3 / Symfony ^6.4)

Method Sylius\TwigHooks\Bag\ScalarDataBag::validateValues() has parameter $values with no value type specified in iterable type array.
{
foreach ($values as $value) {
if (is_array($value)) {
$this->validateValues($value);
continue;
}

if (!is_scalar($value)) {
throw new \InvalidArgumentException('The value must be a scalar.');
}
}
}
}
12 changes: 12 additions & 0 deletions src/TwigHooks/src/Bag/ScalarDataBagInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

declare(strict_types=1);

namespace Sylius\TwigHooks\Bag;

/**
* @extends \ArrayAccess<string, scalar>
*/
interface ScalarDataBagInterface extends \ArrayAccess
{
}
3 changes: 2 additions & 1 deletion src/TwigHooks/src/Hook/Renderer/HookRenderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace Sylius\TwigHooks\Hook\Renderer;

use Sylius\TwigHooks\Bag\DataBag;
use Sylius\TwigHooks\Bag\ScalarDataBag;
use Sylius\TwigHooks\Hook\Metadata\HookMetadata;
use Sylius\TwigHooks\Hookable\Metadata\HookableMetadata;
use Sylius\TwigHooks\Hookable\Renderer\HookableRendererInterface;
Expand Down Expand Up @@ -37,7 +38,7 @@ public function render(array $hookNames, array $hookContext = []): string
$context = $this->contextProvider->provide($hookable, $hookContext);
$configuration = $this->configurationProvider->provide($hookable);

$hookableMetadata = new HookableMetadata($hookMetadata, new DataBag($context), new DataBag($configuration), $hookNames);
$hookableMetadata = new HookableMetadata($hookMetadata, new DataBag($context), new ScalarDataBag($configuration), $hookNames);

$renderedHookables[] = $this->compositeHookableRenderer->render($hookable, $hookableMetadata);
}
Expand Down
3 changes: 2 additions & 1 deletion src/TwigHooks/src/Hookable/Metadata/HookableMetadata.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace Sylius\TwigHooks\Hookable\Metadata;

use Sylius\TwigHooks\Bag\DataBagInterface;
use Sylius\TwigHooks\Bag\ScalarDataBagInterface;
use Sylius\TwigHooks\Hook\Metadata\HookMetadata;

class HookableMetadata
Expand All @@ -15,7 +16,7 @@ class HookableMetadata
public function __construct(
public readonly HookMetadata $renderedBy,
public readonly DataBagInterface $context,
public readonly DataBagInterface $configuration,
public readonly ScalarDataBagInterface $configuration,
public readonly array $prefixes = [],
) {
foreach ($prefixes as $prefix) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace Sylius\TwigHooks\LiveComponent;

use Sylius\TwigHooks\Bag\DataBag;
use Sylius\TwigHooks\Bag\ScalarDataBag;
use Sylius\TwigHooks\Hook\Metadata\HookMetadata;
use Sylius\TwigHooks\Hookable\Metadata\HookableMetadata;
use Symfony\UX\LiveComponent\Attribute\LiveProp;
Expand All @@ -21,7 +22,7 @@ public function hydrateHookableMetadata($data): HookableMetadata
return new HookableMetadata(
new HookMetadata($data['renderedBy'], new DataBag()),
new DataBag(),
new DataBag(),
new ScalarDataBag(json_decode($data['configuration'], true)),
$data['prefixes'] ?? [],
);
}
Expand All @@ -30,6 +31,7 @@ public function dehydrateHookableMetadata(HookableMetadata $metadata): array
{
return [
'renderedBy' => $metadata->renderedBy->name,
'configuration' => json_encode($metadata->configuration->all()),
'prefixes' => $metadata->prefixes,
];
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public function testItReturnsDefaultConfiguration(): void
'component' => HookableComponent::class,
'disabled' => DisabledHookable::class,
],
'hook_name_section_separator' => '#',
'hook_name_section_separator' => false,
],
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace Tests\Sylius\TwigHooks\Integration\Twig\Runtime;

use Sylius\TwigHooks\Bag\DataBag;
use Sylius\TwigHooks\Bag\ScalarDataBag;
use Sylius\TwigHooks\Twig\Runtime\HooksRuntime;
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
use Tests\Sylius\TwigHooks\Utils\MotherObject\HookableMetadataMotherObject;
Expand Down Expand Up @@ -47,7 +48,7 @@ public function testItReturnsHookableContext(): void
public function testItReturnsHookableConfiguration(): void
{
$runtime = $this->getTestSubject();
$configuration = new DataBag();
$configuration = new ScalarDataBag();
$metadata = HookableMetadataMotherObject::withConfiguration($configuration);

$this->assertSame($configuration, $runtime->getHookableConfiguration(['hookable_metadata' => $metadata]));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;
use Sylius\TwigHooks\Bag\DataBag;
use Sylius\TwigHooks\Bag\ScalarDataBag;
use Sylius\TwigHooks\Hookable\Metadata\HookableMetadata;
use Sylius\TwigHooks\Hookable\Renderer\HookableComponentRenderer;
use Sylius\TwigHooks\Provider\PropsProviderInterface;
Expand Down Expand Up @@ -54,7 +55,7 @@ public function testItRendersHookableComponent(): void
$hookable = HookableComponentMotherObject::withTargetAndProps('some-component', ['some' => 'data']);
$metadata = HookableMetadataMotherObject::withContextAndConfiguration(
context: new DataBag(['some' => 'data']),
configuration: new DataBag(['some' => 'configuration']),
configuration: new ScalarDataBag(['some' => 'configuration']),
);

$this->propsProvider->expects($this->once())->method('provide')->with($hookable, $metadata)->willReturn(['some' => 'props']);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@

use Sylius\TwigHooks\Bag\DataBag;
use Sylius\TwigHooks\Bag\DataBagInterface;
use Sylius\TwigHooks\Bag\ScalarDataBag;
use Sylius\TwigHooks\Bag\ScalarDataBagInterface;
use Sylius\TwigHooks\Hookable\Metadata\HookableMetadata;

final class HookableMetadataMotherObject
Expand All @@ -15,7 +17,7 @@ public static function some(): HookableMetadata
return new HookableMetadata(
HookMetadataMotherObject::some(),
new DataBag([]),
new DataBag([]),
new ScalarDataBag([]),
[]
);
}
Expand All @@ -25,29 +27,29 @@ public static function withContext(DataBagInterface|array $context): HookableMet
return new HookableMetadata(
HookMetadataMotherObject::some(),
is_array($context) ? new DataBag($context) : $context,
new DataBag([]),
new ScalarDataBag([]),
[]
);
}

public static function withConfiguration(DataBagInterface|array $configuration): HookableMetadata
public static function withConfiguration(ScalarDataBagInterface|array $configuration): HookableMetadata
{
return new HookableMetadata(
HookMetadataMotherObject::some(),
new DataBag([]),
is_array($configuration) ? new DataBag($configuration) : $configuration,
is_array($configuration) ? new ScalarDataBag($configuration) : $configuration,
[]
);
}

public static function withContextAndConfiguration(
DataBagInterface|array $context,
DataBagInterface|array $configuration,
ScalarDataBagInterface|array $configuration,
): HookableMetadata {
return new HookableMetadata(
HookMetadataMotherObject::some(),
is_array($context) ? new DataBag($context) : $context,
is_array($configuration) ? new DataBag($configuration) : $configuration,
is_array($configuration) ? new ScalarDataBag($configuration) : $configuration,
[]
);
}
Expand Down

0 comments on commit 0ab8b3b

Please sign in to comment.