From 1e90b49847743fd1289b008ae0b1c81c6d431446 Mon Sep 17 00:00:00 2001 From: Fabio Capucci Date: Tue, 20 Aug 2024 16:24:53 +0200 Subject: [PATCH 1/2] ternary filter --- src/Filters/TernaryFilter.php | 18 ++++++++++++++++++ tests/Integration/CustomFiltersTest.php | 19 +++++++++++++++++++ 2 files changed, 37 insertions(+) create mode 100644 src/Filters/TernaryFilter.php create mode 100644 tests/Integration/CustomFiltersTest.php diff --git a/src/Filters/TernaryFilter.php b/src/Filters/TernaryFilter.php new file mode 100644 index 0000000..17382a4 --- /dev/null +++ b/src/Filters/TernaryFilter.php @@ -0,0 +1,18 @@ +toLiquidValue() : $input; + + return match (true) { + $inputValue === null, $inputValue === '', $inputValue === [], $inputValue === false => $falseValue, + default => $trueValue, + }; + } +} diff --git a/tests/Integration/CustomFiltersTest.php b/tests/Integration/CustomFiltersTest.php new file mode 100644 index 0000000..3565fc3 --- /dev/null +++ b/tests/Integration/CustomFiltersTest.php @@ -0,0 +1,19 @@ +factory = \Keepsuit\Liquid\TemplateFactory::new() + ->setStrictVariables(true) + ->registerFilter(\Keepsuit\Liquid\Filters\TernaryFilter::class); +}); + +test('ternary', function () { + expect(renderTemplate('{{ true | ternary: "yes", "no" }}', factory: $this->factory))->toBe('yes'); + expect(renderTemplate('{{ test | ternary: "yes", "no" }}', factory: $this->factory, assigns: ['test' => ['a']]))->toBe('yes'); + expect(renderTemplate('{{ test | ternary: "yes", "no" }}', factory: $this->factory, assigns: ['test' => 'a']))->toBe('yes'); + expect(renderTemplate('{{ test | ternary: "yes", "no" }}', factory: $this->factory, assigns: ['test' => new \Keepsuit\Liquid\Tests\Stubs\IntegerDrop('1')]))->toBe('yes'); + + expect(renderTemplate('{{ false | ternary: "yes", "no" }}', factory: $this->factory))->toBe('no'); + expect(renderTemplate('{{ test | ternary: "yes", "no" }}', factory: $this->factory, assigns: ['test' => []]))->toBe('no'); + expect(renderTemplate('{{ test | ternary: "yes", "no" }}', factory: $this->factory, assigns: ['test' => '']))->toBe('no'); + expect(renderTemplate('{{ test | ternary: "yes", "no" }}', factory: $this->factory, assigns: ['test' => null]))->toBe('no'); +}); From b1dde977810e3d6233b4d3f8dba9e2c0acc81cd5 Mon Sep 17 00:00:00 2001 From: Fabio Capucci Date: Mon, 30 Dec 2024 14:26:41 +0100 Subject: [PATCH 2/2] fix --- src/Filters/{ => Custom}/TernaryFilter.php | 3 ++- tests/Integration/CustomFiltersTest.php | 4 ++-- 2 files changed, 4 insertions(+), 3 deletions(-) rename src/Filters/{ => Custom}/TernaryFilter.php (84%) diff --git a/src/Filters/TernaryFilter.php b/src/Filters/Custom/TernaryFilter.php similarity index 84% rename from src/Filters/TernaryFilter.php rename to src/Filters/Custom/TernaryFilter.php index 17382a4..45854c4 100644 --- a/src/Filters/TernaryFilter.php +++ b/src/Filters/Custom/TernaryFilter.php @@ -1,8 +1,9 @@ factory = \Keepsuit\Liquid\TemplateFactory::new() + $this->factory = \Keepsuit\Liquid\EnvironmentFactory::new() ->setStrictVariables(true) - ->registerFilter(\Keepsuit\Liquid\Filters\TernaryFilter::class); + ->registerFilters(\Keepsuit\Liquid\Filters\Custom\TernaryFilter::class); }); test('ternary', function () {