diff --git a/composer.json b/composer.json index a3402c3..9c37b47 100644 --- a/composer.json +++ b/composer.json @@ -48,7 +48,8 @@ "symfony/http-kernel": "^3.4 || ^4.3 || ^5.0 || ^6.0", "symfony/phpunit-bridge": "^5.4 || ^6.0", "twig/twig": "^2.12.1 || ~3.0", - "laminas/laminas-view": "~2.9" + "laminas/laminas-view": "~2.9", + "laminas/laminas-inputfilter": "~2.30" }, "autoload": { "psr-4": { diff --git a/src/Bridge/Laminas/SlugifyFilter.php b/src/Bridge/Laminas/SlugifyFilter.php new file mode 100644 index 0000000..cd15b14 --- /dev/null +++ b/src/Bridge/Laminas/SlugifyFilter.php @@ -0,0 +1,56 @@ + Slugify::LOWERCASE_NUMBERS_DASHES, + 'separator' => '-', + 'lowercase' => true, + 'lowercase_after_regexp' => false, + 'trim' => true, + 'strip_tags' => false + ]; + + /** + * @param array|null $options + */ + public function __construct(?array $options = null) + { + if (!empty($options)) { + $this->setOptions($options); + } + } + + /** + * Returns the result of filtering $value + * + * @param mixed $value + * + * @return mixed + */ + public function filter($value) + { + if (!empty($value)) { + $slugify = new Slugify($this->options); + return $slugify->slugify((string) $value); + } + + return $value; + } +} diff --git a/tests/Bridge/Laminas/SlugifyFilterTest.php b/tests/Bridge/Laminas/SlugifyFilterTest.php new file mode 100644 index 0000000..a516f69 --- /dev/null +++ b/tests/Bridge/Laminas/SlugifyFilterTest.php @@ -0,0 +1,73 @@ +input = new Input(''); + } + + /** + * @covers \Cocur\Slugify\Bridge\Laminas\SlugifyFilter::filter() + */ + public function testSlugifyFilterWithoutCustomOptions() + { + $chain = new FilterChain( + [ + 'filters' => [ + [ + 'name' => SlugifyFilter::class, + ], + ], + ] + ); + $this->input->setFilterChain($chain); + $this->input->setValue('foo Bar'); + + $this->assertSame('foo-bar', $this->input->getValue()); + + } + + /** + * @covers \Cocur\Slugify\Bridge\Laminas\SlugifyFilter::filter() + */ + public function testSlugifyFilterWithCustomOptions() + { + $chain = new FilterChain( + [ + 'filters' => [ + [ + 'name' => SlugifyFilter::class, + 'options' => [ + 'regexp' => '/([^0-9test\/]|-)+/', + 'strip_tags' => false, + ] + ], + ], + ] + ); + $this->input->setFilterChain($chain); + $this->input->setValue('0123 foo bar '); + + $this->assertSame('0123-test', $this->input->getValue()); + } + +}