-
-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feature #113 Expose Enum static methods to Twig (Tom32i, ogizanagi)
This PR was merged into the 1.x-dev branch. Discussion ---------- Expose Enum static methods to Twig - [x] Twig extension - [x] Remove Twig extension service definition if Twig is not loaded - [x] Documentation - [x] Tests Commits ------- 00f40fd Continue Twig extension feature 25f62a0 Expose Enum static methods to Twig
- Loading branch information
Showing
10 changed files
with
257 additions
and
13 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
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,86 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the "elao/enum" package. | ||
* | ||
* Copyright (C) Elao | ||
* | ||
* @author Elao <[email protected]> | ||
*/ | ||
|
||
namespace Elao\Enum\Bridge\Twig\Extension; | ||
|
||
use Elao\Enum\EnumInterface; | ||
use Elao\Enum\Exception\InvalidArgumentException; | ||
use Elao\Enum\ReadableEnumInterface; | ||
use Twig\Extension\AbstractExtension; | ||
use Twig\TwigFunction; | ||
|
||
class EnumExtension extends AbstractExtension | ||
{ | ||
public function getFunctions() | ||
{ | ||
return [ | ||
new TwigFunction('enum_get', [$this, 'get']), | ||
new TwigFunction('enum_values', [$this, 'values']), | ||
new TwigFunction('enum_accepts', [$this, 'accepts']), | ||
new TwigFunction('enum_instances', [$this, 'instances']), | ||
new TwigFunction('enum_readables', [$this, 'readables']), | ||
new TwigFunction('enum_readable_for', [$this, 'readableFor']), | ||
]; | ||
} | ||
|
||
public function get(string $className, $value): EnumInterface | ||
{ | ||
if (!is_a($className, EnumInterface::class, true)) { | ||
throw new InvalidArgumentException(sprintf('"%s" is not an "%s".', $className, EnumInterface::class)); | ||
} | ||
|
||
return \call_user_func([$className, 'get'], $value); | ||
} | ||
|
||
public function values(string $className): array | ||
{ | ||
if (!is_a($className, EnumInterface::class, true)) { | ||
throw new InvalidArgumentException(sprintf('"%s" is not an "%s".', $className, EnumInterface::class)); | ||
} | ||
|
||
return \call_user_func([$className, 'values']); | ||
} | ||
|
||
public function accepts(string $className, $value): bool | ||
{ | ||
if (!is_a($className, EnumInterface::class, true)) { | ||
throw new InvalidArgumentException(sprintf('"%s" is not an "%s".', $className, EnumInterface::class)); | ||
} | ||
|
||
return \call_user_func([$className, 'accepts'], $value); | ||
} | ||
|
||
public function instances(string $className): array | ||
{ | ||
if (!is_a($className, EnumInterface::class, true)) { | ||
throw new InvalidArgumentException(sprintf('"%s" is not an "%s".', $className, EnumInterface::class)); | ||
} | ||
|
||
return \call_user_func([$className, 'instances']); | ||
} | ||
|
||
public function readables(string $className): array | ||
{ | ||
if (!is_a($className, ReadableEnumInterface::class, true)) { | ||
throw new InvalidArgumentException(sprintf('"%s" is not a "%s".', $className, ReadableEnumInterface::class)); | ||
} | ||
|
||
return \call_user_func([$className, 'readables']); | ||
} | ||
|
||
public function readableFor(string $className, $value): string | ||
{ | ||
if (!is_a($className, ReadableEnumInterface::class, true)) { | ||
throw new InvalidArgumentException(sprintf('"%s" is not a "%s".', $className, ReadableEnumInterface::class)); | ||
} | ||
|
||
return \call_user_func([$className, 'readableFor'], $value); | ||
} | ||
} |
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,121 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the "elao/enum" package. | ||
* | ||
* Copyright (C) Elao | ||
* | ||
* @author Elao <[email protected]> | ||
*/ | ||
|
||
namespace Elao\Enum\Tests\Unit\Bridge\Twig\Extension; | ||
|
||
use Elao\Enum\Bridge\Twig\Extension\EnumExtension; | ||
use Elao\Enum\Exception\InvalidArgumentException; | ||
use Elao\Enum\Tests\Fixtures\Enum\Gender; | ||
use Elao\Enum\Tests\TestCase; | ||
use Twig\TwigFunction; | ||
|
||
class EnumExtensionTest extends TestCase | ||
{ | ||
/** @var EnumExtension */ | ||
private $extension; | ||
|
||
protected function setUp(): void | ||
{ | ||
$this->extension = new EnumExtension(); | ||
} | ||
|
||
public function test enum_get(): void | ||
{ | ||
self::assertSame( | ||
Gender::FEMALE(), | ||
$this->getFunction('enum_get')->getCallable()(Gender::class, Gender::FEMALE), | ||
); | ||
} | ||
|
||
public function test enum_get with invalid class(): void | ||
{ | ||
$this->expectException(InvalidArgumentException::class); | ||
$this->expectExceptionMessage('"stdClass" is not an "Elao\Enum\EnumInterface"'); | ||
|
||
$this->getFunction('enum_get')->getCallable()(\stdClass::class, Gender::FEMALE); | ||
} | ||
|
||
public function test enum_values(): void | ||
{ | ||
self::assertSame(Gender::values(), $this->getFunction('enum_values')->getCallable()(Gender::class)); | ||
} | ||
|
||
public function test enum_values with invalid class(): void | ||
{ | ||
$this->expectException(InvalidArgumentException::class); | ||
$this->expectExceptionMessage('"stdClass" is not an "Elao\Enum\EnumInterface"'); | ||
|
||
self::assertSame(Gender::values(), $this->getFunction('enum_values')->getCallable()(\stdClass::class)); | ||
} | ||
|
||
public function test enum_accepts(): void | ||
{ | ||
self::assertTrue($this->getFunction('enum_accepts')->getCallable()(Gender::class, Gender::FEMALE)); | ||
self::assertFalse($this->getFunction('enum_accepts')->getCallable()(Gender::class, 'not a valid value')); | ||
} | ||
|
||
public function test enum_accepts with invalid class(): void | ||
{ | ||
$this->expectException(InvalidArgumentException::class); | ||
$this->expectExceptionMessage('"stdClass" is not an "Elao\Enum\EnumInterface"'); | ||
|
||
self::assertSame(Gender::values(), $this->getFunction('enum_accepts')->getCallable()(\stdClass::class, Gender::FEMALE)); | ||
} | ||
|
||
public function test enum_instances(): void | ||
{ | ||
self::assertSame(Gender::instances(), $this->getFunction('enum_instances')->getCallable()(Gender::class)); | ||
} | ||
|
||
public function test enum_instances with invalid class(): void | ||
{ | ||
$this->expectException(InvalidArgumentException::class); | ||
$this->expectExceptionMessage('"stdClass" is not an "Elao\Enum\EnumInterface"'); | ||
|
||
self::assertSame(Gender::values(), $this->getFunction('enum_instances')->getCallable()(\stdClass::class)); | ||
} | ||
|
||
public function test enum_readables(): void | ||
{ | ||
self::assertSame(Gender::readables(), $this->getFunction('enum_readables')->getCallable()(Gender::class)); | ||
} | ||
|
||
public function test enum_readables with invalid class(): void | ||
{ | ||
$this->expectException(InvalidArgumentException::class); | ||
$this->expectExceptionMessage('"stdClass" is not a "Elao\Enum\ReadableEnumInterface"'); | ||
|
||
self::assertSame(Gender::values(), $this->getFunction('enum_readables')->getCallable()(\stdClass::class)); | ||
} | ||
|
||
public function test enum_readable_for(): void | ||
{ | ||
self::assertSame(Gender::FEMALE()->getReadable(), $this->getFunction('enum_readable_for')->getCallable()(Gender::class, Gender::FEMALE)); | ||
} | ||
|
||
public function test enum_readable_for with invalid class(): void | ||
{ | ||
$this->expectException(InvalidArgumentException::class); | ||
$this->expectExceptionMessage('"stdClass" is not a "Elao\Enum\ReadableEnumInterface"'); | ||
|
||
self::assertSame(Gender::values(), $this->getFunction('enum_readable_for')->getCallable()(\stdClass::class, Gender::FEMALE)); | ||
} | ||
|
||
private function getFunction(string $method): TwigFunction | ||
{ | ||
foreach ($this->extension->getFunctions() as $function) { | ||
if ($method === $function->getName()) { | ||
return $function; | ||
} | ||
} | ||
|
||
self::fail("Twig function \"$method\" is not registered"); | ||
} | ||
} |