-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Feat(web-twig): Introduce filter for Boolean props
* converts stringified boolean prop into boolean representation
- Loading branch information
Showing
12 changed files
with
135 additions
and
40 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
12 changes: 6 additions & 6 deletions
12
packages/web-twig/src/Resources/components/Button/Button.twig
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
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,28 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Lmc\SpiritWebTwigBundle\Twig; | ||
|
||
use Twig\Extension\AbstractExtension; | ||
use Twig\TwigFilter; | ||
|
||
class BoolpropExtension extends AbstractExtension | ||
{ | ||
public function getFilters() | ||
{ | ||
return [ | ||
new TwigFilter('boolprop', [$this, 'convert2Boolean'], [ | ||
'is_safe' => ['html'], | ||
]), | ||
]; | ||
} | ||
|
||
/** | ||
* @param mixed $prop | ||
*/ | ||
public function convert2Boolean($prop): bool | ||
{ | ||
return filter_var($prop, FILTER_VALIDATE_BOOLEAN); | ||
} | ||
} |
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,58 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Lmc\SpiritWebTwigBundle\Twig; | ||
|
||
use Mockery as m; | ||
use PHPUnit\Framework\TestCase; | ||
use Psr\Log\LoggerInterface; | ||
use Twig\Environment; | ||
use Twig\Error\LoaderError; | ||
use Twig\Loader\LoaderInterface; | ||
use Twig\Source; | ||
|
||
class BoolpropExtensionTest extends TestCase | ||
{ | ||
private BoolpropExtension $boolpropExtension; | ||
|
||
public function setUp(): void | ||
{ | ||
$this->boolpropExtension = new BoolpropExtension(); | ||
} | ||
|
||
/** | ||
* @param mixed $prop | ||
* @dataProvider boolpropDataProvider | ||
*/ | ||
public function testShouldConvertPropValueIntoBoolean($prop, bool $expectedValue): void | ||
{ | ||
$convertedValue = $this->boolpropExtension->convert2Boolean($prop); | ||
|
||
$this->assertSame($expectedValue, $convertedValue); | ||
} | ||
|
||
/** | ||
* @return array<string, array<int, bool|int|string|null>> | ||
*/ | ||
public function boolpropDataProvider(): array | ||
{ | ||
return [ | ||
'bool true' => [true, true], | ||
'string true' => ['true', true], | ||
'number one' => [1, true], | ||
'number one in string' => ['1', true], | ||
'string `on`' => ['on', true], | ||
'string `yes`' => ['yes', true], | ||
'bool false' => [false, false], | ||
'string false' => ['false', false], | ||
'number zero' => [0, false], | ||
'number zero in string' => ['0', false], | ||
'string `off`' => ['off', false], | ||
'string `no`' => ['no', false], | ||
'some string' => ['asfdsafdsf', false], | ||
'empty prop' => ['', false], | ||
'null prop' => [null, false], | ||
]; | ||
} | ||
} |