-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Change how data is passed to the component hookables
- Loading branch information
1 parent
f2dd5d5
commit 2d1c684
Showing
7 changed files
with
105 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Sylius\TwigHooks\Provider; | ||
|
||
use Sylius\TwigHooks\Hookable\AbstractHookable; | ||
use Symfony\Component\ExpressionLanguage\ExpressionLanguage; | ||
|
||
final class ComponentDataProvider implements DataProviderInterface | ||
{ | ||
public function __construct ( | ||
private ExpressionLanguage $expressionLanguage, | ||
) { | ||
} | ||
|
||
public function provide(AbstractHookable $hookable, array $hookData): array | ||
{ | ||
return $this->mapArrayRecursively(function (mixed $value) use ($hookData): mixed { | ||
if (is_string($value) && str_starts_with($value, '@=')) { | ||
return $this->expressionLanguage->evaluate(substr($value, 2), ['data' => $hookData]); | ||
} | ||
|
||
return $value; | ||
}, $hookable->getData()); | ||
} | ||
|
||
/** | ||
* @param array<array-key, mixed> $array | ||
* @return array<array-key, mixed> | ||
*/ | ||
private function mapArrayRecursively(callable $callback, array $array): array | ||
{ | ||
$result = []; | ||
foreach ($array as $key => $value) { | ||
$result[$key] = is_array($value) | ||
? $this->mapArrayRecursively($callback, $value) | ||
: $callback($value); | ||
} | ||
|
||
return $result; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
50 changes: 50 additions & 0 deletions
50
src/TwigHooks/tests/Unit/Provider/ComponentDataProviderTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Tests\Sylius\TwigHooks\Unit\Provider; | ||
|
||
use PHPUnit\Framework\TestCase; | ||
use Sylius\TwigHooks\Provider\ComponentDataProvider; | ||
use Sylius\TwigHooks\Provider\DataProviderInterface; | ||
use Symfony\Component\ExpressionLanguage\ExpressionLanguage; | ||
use Tests\Sylius\TwigHooks\Utils\MotherObject\BaseHookableMotherObject; | ||
|
||
final class ComponentDataProviderTest extends TestCase | ||
{ | ||
public function testItReturnsEmptyArrayWhenNoDataIsProvidedOnHookable(): void | ||
{ | ||
$hookable = BaseHookableMotherObject::some(); | ||
|
||
$dataProvider = $this->createTestSubject(); | ||
|
||
$this->assertSame([], $dataProvider->provide($hookable, [])); | ||
$this->assertSame([], $dataProvider->provide($hookable, ['some' => 'data'])); | ||
} | ||
|
||
public function testItReturnsDataFromHookable(): void | ||
{ | ||
$hookable = BaseHookableMotherObject::withData(['some' => 'data']); | ||
|
||
$dataProvider = $this->createTestSubject(); | ||
|
||
$this->assertSame(['some' => 'data'], $dataProvider->provide($hookable, [])); | ||
$this->assertSame(['some' => 'data'], $dataProvider->provide($hookable, ['more' => 'data'])); | ||
} | ||
|
||
public function testItPassesTemplateLevelDataToExpressionLanguage(): void | ||
{ | ||
$hookable = BaseHookableMotherObject::withData(['some_key' => '@=data["some"]']); | ||
|
||
$dataProvider = $this->createTestSubject(); | ||
|
||
$this->assertSame(['some_key' => 'data'], $dataProvider->provide($hookable, ['some' => 'data'])); | ||
} | ||
|
||
private function createTestSubject(): DataProviderInterface | ||
{ | ||
return new ComponentDataProvider( | ||
new ExpressionLanguage(), | ||
); | ||
} | ||
} |