From 25fa0c46b1aed48ce9ea896d9689bf9ae63218a0 Mon Sep 17 00:00:00 2001 From: JustInTime Date: Sun, 8 Mar 2020 00:53:30 +0100 Subject: [PATCH 01/21] Added first version of Laminas integration --- README.md | 47 ++++++++++++++++ composer.json | 3 + src/Bridge/Laminas/Module.php | 55 +++++++++++++++++++ src/Bridge/Laminas/SlugifyService.php | 34 ++++++++++++ src/Bridge/Laminas/SlugifyViewHelper.php | 41 ++++++++++++++ .../Laminas/SlugifyViewHelperFactory.php | 30 ++++++++++ 6 files changed, 210 insertions(+) create mode 100644 src/Bridge/Laminas/Module.php create mode 100644 src/Bridge/Laminas/SlugifyService.php create mode 100644 src/Bridge/Laminas/SlugifyViewHelper.php create mode 100644 src/Bridge/Laminas/SlugifyViewHelperFactory.php diff --git a/README.md b/README.md index b68300fd..5db0641a 100644 --- a/README.md +++ b/README.md @@ -335,6 +335,53 @@ You can then use the `Slugify::slugify()` method in your controllers: $url = Slugify::slugify('welcome to the homepage'); ``` +### Laminas + +Slugify can be easely used in Laminas applications. Included bridge provides a service and a view helper +already registered for you. + +Just enable the module in your configuration like this. + +```php +return array( + //... + + 'modules' => array( + 'Application', + 'Cocur\Slugify\Bridge\Laminas' // <- Add this line + //... + ) + + //... +); +``` + +After that you can retrieve the `Cocur\Slugify\Slugify` service (or the `slugify` alias) and generate a slug. + +```php +/** @var \Laminas\ServiceManager\ServiceManager $sm */ +$slugify = $sm->get('Cocur\Slugify\Slugify'); +$slug = $slugify->slugify('Hällo Wörld'); +$anotherSlug = $slugify->slugify('Hällo Wörld', '_'); +``` + +In your view templates use the `slugify` helper to generate slugs. + +```php +slugify('Hällo Wörld') ?> +slugify('Hällo Wörld', '_') ?> +``` + +The service (which is also used in the view helper) can be customized by defining this configuration key. + +```php +return array( + 'cocur_slugify' => array( + 'reg_exp' => '/([^a-zA-Z0-9]|-)+/' + ) +); +``` + ### Zend Framework 2 Slugify can be easely used in Zend Framework 2 applications. Included bridge provides a service and a view helper diff --git a/composer.json b/composer.json index 8913fc9d..dc466d42 100644 --- a/composer.json +++ b/composer.json @@ -29,6 +29,9 @@ "twig/twig": "<2.12.1" }, "require-dev": { + "laminas/laminas-modulemanager": "~2.8", + "laminas/laminas-servicemanager": "~3.3", + "laminas/laminas-view": "~2.9", "laravel/framework": "~5.1", "latte/latte": "~2.2", "league/container": "^2.2.0", diff --git a/src/Bridge/Laminas/Module.php b/src/Bridge/Laminas/Module.php new file mode 100644 index 00000000..0eaf7646 --- /dev/null +++ b/src/Bridge/Laminas/Module.php @@ -0,0 +1,55 @@ +> + */ + public function getServiceConfig() + { + return [ + 'factories' => [ + Slugify::class => SlugifyService::class, + ], + 'aliases' => [ + 'slugify' => Slugify::class, + ] + ]; + } + + /** + * Expected to return \Laminas\ServiceManager\Config object or array to + * seed such an object. + * + * @return array>|Config + */ + public function getViewHelperConfig() + { + return [ + 'aliases' => [ + 'slugify' => SlugifyViewHelper::class + ], + 'factories' => [ + SlugifyViewHelper::class => SlugifyViewHelperFactory::class + ] + ]; + } +} diff --git a/src/Bridge/Laminas/SlugifyService.php b/src/Bridge/Laminas/SlugifyService.php new file mode 100644 index 00000000..16d4e7a5 --- /dev/null +++ b/src/Bridge/Laminas/SlugifyService.php @@ -0,0 +1,34 @@ +get('Config'); + + $slugifyOptions = isset($config[Module::CONFIG_KEY]['options']) ? $config[Module::CONFIG_KEY]['options'] : []; + $provider = isset($config[Module::CONFIG_KEY]['provider']) ? $config[Module::CONFIG_KEY]['provider'] : null; + + return new Slugify($slugifyOptions, $provider); + } + +} diff --git a/src/Bridge/Laminas/SlugifyViewHelper.php b/src/Bridge/Laminas/SlugifyViewHelper.php new file mode 100644 index 00000000..d6ddff93 --- /dev/null +++ b/src/Bridge/Laminas/SlugifyViewHelper.php @@ -0,0 +1,41 @@ +slugify = $slugify; + } + + /** + * @param string $string + * @param string|null $separator + * + * @return string + */ + public function __invoke($string, $separator = null) + { + return $this->slugify->slugify($string, $separator); + } +} diff --git a/src/Bridge/Laminas/SlugifyViewHelperFactory.php b/src/Bridge/Laminas/SlugifyViewHelperFactory.php new file mode 100644 index 00000000..b3d9b1d3 --- /dev/null +++ b/src/Bridge/Laminas/SlugifyViewHelperFactory.php @@ -0,0 +1,30 @@ +get('Cocur\Slugify\Slugify'); + return new SlugifyViewHelper($slugify); + } + +} From 6fde9b39be9afc4b49017a29ebc5d481ad6a9ef9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=20H=C3=A4hnel?= Date: Thu, 12 Sep 2024 15:31:05 +0200 Subject: [PATCH 02/21] FIX wording in Readme --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 5cbf3db3..79593eeb 100644 --- a/README.md +++ b/README.md @@ -339,7 +339,7 @@ $url = Slugify::slugify("welcome to the homepage"); ### Laminas -Slugify can be easely used in Laminas applications. Included bridge provides a service and a view helper +Slugify can be easily used in Laminas applications. Included bridge provides a service and a view helper already registered for you. Just enable the module in your configuration like this. From f726a3cfe5fbc4dc2c6a56dc053c31f337b6fb3a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=20H=C3=A4hnel?= Date: Thu, 12 Sep 2024 16:30:13 +0200 Subject: [PATCH 03/21] Adjust tests --- src/Bridge/Laminas/SlugifyService.php | 11 ++-- src/Bridge/ZF2/Module.php | 50 ------------------- src/Bridge/ZF2/SlugifyService.php | 30 ----------- src/Bridge/ZF2/SlugifyViewHelper.php | 41 --------------- src/Bridge/ZF2/SlugifyViewHelperFactory.php | 28 ----------- tests/Bridge/{ZF2 => Laminas}/ModuleTest.php | 11 ++-- .../{ZF2 => Laminas}/SlugifyServiceTest.php | 21 +++++--- .../SlugifyViewHelperFactoryTest.php | 13 ++--- .../SlugifyViewHelperTest.php | 8 +-- 9 files changed, 37 insertions(+), 176 deletions(-) delete mode 100644 src/Bridge/ZF2/Module.php delete mode 100644 src/Bridge/ZF2/SlugifyService.php delete mode 100644 src/Bridge/ZF2/SlugifyViewHelper.php delete mode 100644 src/Bridge/ZF2/SlugifyViewHelperFactory.php rename tests/Bridge/{ZF2 => Laminas}/ModuleTest.php (72%) rename tests/Bridge/{ZF2 => Laminas}/SlugifyServiceTest.php (74%) rename tests/Bridge/{ZF2 => Laminas}/SlugifyViewHelperFactoryTest.php (66%) rename tests/Bridge/{ZF2 => Laminas}/SlugifyViewHelperTest.php (79%) diff --git a/src/Bridge/Laminas/SlugifyService.php b/src/Bridge/Laminas/SlugifyService.php index 16d4e7a5..cf45b1ed 100644 --- a/src/Bridge/Laminas/SlugifyService.php +++ b/src/Bridge/Laminas/SlugifyService.php @@ -16,13 +16,14 @@ class SlugifyService implements FactoryInterface { /** - * * @param ContainerInterface $container - * @param string $requestedName - * @param array $options - * @return object + * @param $requestedName + * @param array|null $options + * @return Slugify + * @throws \Psr\Container\ContainerExceptionInterface + * @throws \Psr\Container\NotFoundExceptionInterface */ - public function __invoke(ContainerInterface $container, $requestedName, array $options = null): Slugify { + public function __invoke(ContainerInterface $container, $requestedName, ?array $options = null): Slugify { $config = $container->get('Config'); $slugifyOptions = isset($config[Module::CONFIG_KEY]['options']) ? $config[Module::CONFIG_KEY]['options'] : []; diff --git a/src/Bridge/ZF2/Module.php b/src/Bridge/ZF2/Module.php deleted file mode 100644 index 5babfc2a..00000000 --- a/src/Bridge/ZF2/Module.php +++ /dev/null @@ -1,50 +0,0 @@ -> - */ - public function getServiceConfig(): array - { - return [ - 'factories' => [ - 'Cocur\Slugify\Slugify' => 'Cocur\Slugify\Bridge\ZF2\SlugifyService' - ], - 'aliases' => [ - 'slugify' => 'Cocur\Slugify\Slugify' - ] - ]; - } - - /** - * Expected to return \Zend\ServiceManager\Config object or array to - * seed such an object. - * - * @return array>|\Zend\ServiceManager\Config - */ - public function getViewHelperConfig(): array - { - return [ - 'factories' => [ - 'slugify' => 'Cocur\Slugify\Bridge\ZF2\SlugifyViewHelperFactory' - ] - ]; - } -} diff --git a/src/Bridge/ZF2/SlugifyService.php b/src/Bridge/ZF2/SlugifyService.php deleted file mode 100644 index 9d7d45aa..00000000 --- a/src/Bridge/ZF2/SlugifyService.php +++ /dev/null @@ -1,30 +0,0 @@ -get('Config'); - - $options = isset($config[Module::CONFIG_KEY]['options']) ? $config[Module::CONFIG_KEY]['options'] : []; - $provider = isset($config[Module::CONFIG_KEY]['provider']) ? $config[Module::CONFIG_KEY]['provider'] : null; - - return new Slugify($options, $provider); - } -} diff --git a/src/Bridge/ZF2/SlugifyViewHelper.php b/src/Bridge/ZF2/SlugifyViewHelper.php deleted file mode 100644 index ef51a879..00000000 --- a/src/Bridge/ZF2/SlugifyViewHelper.php +++ /dev/null @@ -1,41 +0,0 @@ -slugify = $slugify; - } - - /** - * @param string $string - * @param string|null $separator - * - * @return string - */ - public function __invoke(string $string, string $separator = null) - { - return $this->slugify->slugify($string, $separator); - } -} diff --git a/src/Bridge/ZF2/SlugifyViewHelperFactory.php b/src/Bridge/ZF2/SlugifyViewHelperFactory.php deleted file mode 100644 index 878fec19..00000000 --- a/src/Bridge/ZF2/SlugifyViewHelperFactory.php +++ /dev/null @@ -1,28 +0,0 @@ -getServiceLocator()->get(Slugify::class); - - return new SlugifyViewHelper($slugify); - } -} diff --git a/tests/Bridge/ZF2/ModuleTest.php b/tests/Bridge/Laminas/ModuleTest.php similarity index 72% rename from tests/Bridge/ZF2/ModuleTest.php rename to tests/Bridge/Laminas/ModuleTest.php index f2ebfbff..2fe9d771 100644 --- a/tests/Bridge/ZF2/ModuleTest.php +++ b/tests/Bridge/Laminas/ModuleTest.php @@ -1,7 +1,8 @@ module->getViewHelperConfig(); $this->assertIsArray($vhConfig); $this->assertArrayHasKey('factories', $vhConfig); - $this->assertArrayHasKey('slugify', $vhConfig['factories']); + $this->assertArrayHasKey(SlugifyViewHelper::class, $vhConfig['factories']); + $this->assertArrayHasKey('aliases', $vhConfig); + $this->assertArrayHasKey('slugify', $vhConfig['aliases']); } } diff --git a/tests/Bridge/ZF2/SlugifyServiceTest.php b/tests/Bridge/Laminas/SlugifyServiceTest.php similarity index 74% rename from tests/Bridge/ZF2/SlugifyServiceTest.php rename to tests/Bridge/Laminas/SlugifyServiceTest.php index 13cbfbc6..d4cbf766 100644 --- a/tests/Bridge/ZF2/SlugifyServiceTest.php +++ b/tests/Bridge/Laminas/SlugifyServiceTest.php @@ -1,9 +1,11 @@ slugifyService = new SlugifyService(); } /** - * @covers \Cocur\Slugify\Bridge\ZF2\SlugifyService::__invoke() + * @covers \Cocur\Slugify\Bridge\Laminas\SlugifyService::__invoke() */ public function testInvokeWithoutCustomConfig() { + $this->markTestSkipped(); $sm = $this->createServiceManagerMock(); $slugify = call_user_func($this->slugifyService, $sm); $this->assertInstanceOf('Cocur\Slugify\Slugify', $slugify); @@ -40,10 +44,11 @@ public function testInvokeWithoutCustomConfig() } /** - * @covers \Cocur\Slugify\Bridge\ZF2\SlugifyService::__invoke() + * @covers \Cocur\Slugify\Bridge\Laminas\SlugifyService::__invoke() */ public function testInvokeWithCustomConfig() { + $this->markTestSkipped(); $sm = $this->createServiceManagerMock([ Module::CONFIG_KEY => [ 'options' => ['regexp' => '/([^a-z0-9.]|-)+/'] @@ -60,8 +65,8 @@ public function testInvokeWithCustomConfig() protected function createServiceManagerMock(array $config = []) { - $sm = new ServiceManager(); - $sm->setService('Config', $config); + $sm = new ServiceManager($config); + //$sm->setService('Config', $config); return $sm; } diff --git a/tests/Bridge/ZF2/SlugifyViewHelperFactoryTest.php b/tests/Bridge/Laminas/SlugifyViewHelperFactoryTest.php similarity index 66% rename from tests/Bridge/ZF2/SlugifyViewHelperFactoryTest.php rename to tests/Bridge/Laminas/SlugifyViewHelperFactoryTest.php index 54356a47..3ba06c07 100644 --- a/tests/Bridge/ZF2/SlugifyViewHelperFactoryTest.php +++ b/tests/Bridge/Laminas/SlugifyViewHelperFactoryTest.php @@ -1,10 +1,10 @@ markTestSkipped(); $sm = new ServiceManager(); $sm->setService('Cocur\Slugify\Slugify', new Slugify()); $vhm = new HelperPluginManager(); $vhm->setServiceLocator($sm); $viewHelper = call_user_func($this->factory, $vhm); - $this->assertInstanceOf('Cocur\Slugify\Bridge\ZF2\SlugifyViewHelper', $viewHelper); + $this->assertInstanceOf('Cocur\Slugify\Bridge\Laminas\SlugifyViewHelper', $viewHelper); } } diff --git a/tests/Bridge/ZF2/SlugifyViewHelperTest.php b/tests/Bridge/Laminas/SlugifyViewHelperTest.php similarity index 79% rename from tests/Bridge/ZF2/SlugifyViewHelperTest.php rename to tests/Bridge/Laminas/SlugifyViewHelperTest.php index a037dee4..fa3e376f 100644 --- a/tests/Bridge/ZF2/SlugifyViewHelperTest.php +++ b/tests/Bridge/Laminas/SlugifyViewHelperTest.php @@ -1,7 +1,7 @@ Date: Sat, 14 Sep 2024 16:34:34 +0200 Subject: [PATCH 04/21] Fix and Enable skipped tests --- src/Bridge/Laminas/Module.php | 4 ++-- src/Bridge/Laminas/SlugifyService.php | 8 ++++---- src/Bridge/Laminas/SlugifyViewHelperFactory.php | 10 ++++++---- tests/Bridge/Laminas/SlugifyServiceTest.php | 11 ++++------- tests/Bridge/Laminas/SlugifyViewHelperFactoryTest.php | 5 +---- 5 files changed, 17 insertions(+), 21 deletions(-) diff --git a/src/Bridge/Laminas/Module.php b/src/Bridge/Laminas/Module.php index 0eaf7646..abc8f775 100644 --- a/src/Bridge/Laminas/Module.php +++ b/src/Bridge/Laminas/Module.php @@ -5,7 +5,7 @@ use Cocur\Slugify\Slugify; use Laminas\ModuleManager\Feature\ServiceProviderInterface; use Laminas\ModuleManager\Feature\ViewHelperProviderInterface; -use Zend\ServiceManager\Config; +use Laminas\ServiceManager\Config; /** * Class Module @@ -15,7 +15,7 @@ */ class Module implements ServiceProviderInterface, ViewHelperProviderInterface { - const CONFIG_KEY = 'cocur_slugify'; + public const CONFIG_KEY = 'cocur_slugify'; /** * Expected to return \Laminas\ServiceManager\Config object or array to diff --git a/src/Bridge/Laminas/SlugifyService.php b/src/Bridge/Laminas/SlugifyService.php index cf45b1ed..7ba8510d 100644 --- a/src/Bridge/Laminas/SlugifyService.php +++ b/src/Bridge/Laminas/SlugifyService.php @@ -14,7 +14,6 @@ */ class SlugifyService implements FactoryInterface { - /** * @param ContainerInterface $container * @param $requestedName @@ -23,11 +22,12 @@ class SlugifyService implements FactoryInterface * @throws \Psr\Container\ContainerExceptionInterface * @throws \Psr\Container\NotFoundExceptionInterface */ - public function __invoke(ContainerInterface $container, $requestedName, ?array $options = null): Slugify { + public function __invoke(ContainerInterface $container, $requestedName, ?array $options = null): Slugify + { $config = $container->get('Config'); - $slugifyOptions = isset($config[Module::CONFIG_KEY]['options']) ? $config[Module::CONFIG_KEY]['options'] : []; - $provider = isset($config[Module::CONFIG_KEY]['provider']) ? $config[Module::CONFIG_KEY]['provider'] : null; + $slugifyOptions = $config[Module::CONFIG_KEY]['options'] ?? []; + $provider = $config[Module::CONFIG_KEY]['provider'] ?? null; return new Slugify($slugifyOptions, $provider); } diff --git a/src/Bridge/Laminas/SlugifyViewHelperFactory.php b/src/Bridge/Laminas/SlugifyViewHelperFactory.php index b3d9b1d3..8b1be830 100644 --- a/src/Bridge/Laminas/SlugifyViewHelperFactory.php +++ b/src/Bridge/Laminas/SlugifyViewHelperFactory.php @@ -15,15 +15,17 @@ class SlugifyViewHelperFactory implements FactoryInterface { /** - * * @param ContainerInterface $container - * @param string $requestedName - * @param array $options + * @param $requestedName + * @param array|null $options * @return SlugifyViewHelper + * @throws \Psr\Container\ContainerExceptionInterface + * @throws \Psr\Container\NotFoundExceptionInterface */ - public function __invoke(ContainerInterface $container, $requestedName, array $options = null): SlugifyViewHelper + public function __invoke(ContainerInterface $container, $requestedName, ?array $options = null): SlugifyViewHelper { $slugify = $container->get('Cocur\Slugify\Slugify'); + return new SlugifyViewHelper($slugify); } diff --git a/tests/Bridge/Laminas/SlugifyServiceTest.php b/tests/Bridge/Laminas/SlugifyServiceTest.php index d4cbf766..423b4a0c 100644 --- a/tests/Bridge/Laminas/SlugifyServiceTest.php +++ b/tests/Bridge/Laminas/SlugifyServiceTest.php @@ -3,7 +3,6 @@ use Cocur\Slugify\Bridge\Laminas\Module; use Cocur\Slugify\Bridge\Laminas\SlugifyService; -use Laminas\ServiceManager\ServiceLocatorInterface; use Laminas\ServiceManager\ServiceManager; use Mockery\Adapter\Phpunit\MockeryTestCase; @@ -32,9 +31,8 @@ protected function setUp(): void */ public function testInvokeWithoutCustomConfig() { - $this->markTestSkipped(); $sm = $this->createServiceManagerMock(); - $slugify = call_user_func($this->slugifyService, $sm); + $slugify = call_user_func($this->slugifyService, $sm, 'slugify'); $this->assertInstanceOf('Cocur\Slugify\Slugify', $slugify); // Make sure reg exp is default one @@ -48,16 +46,15 @@ public function testInvokeWithoutCustomConfig() */ public function testInvokeWithCustomConfig() { - $this->markTestSkipped(); $sm = $this->createServiceManagerMock([ Module::CONFIG_KEY => [ 'options' => ['regexp' => '/([^a-z0-9.]|-)+/'] ] ]); - $slugify = call_user_func($this->slugifyService, $sm); + $slugify = call_user_func($this->slugifyService, $sm, 'slugify'); $this->assertInstanceOf('Cocur\Slugify\Slugify', $slugify); - // Make sure reg exp is the one provided and dots are kept + // Make sure regexp is the one provided and dots are kept $actual = 'Hello My Friend.zip'; $expected = 'hello-my-friend.zip'; $this->assertSame($expected, $slugify->slugify($actual)); @@ -66,7 +63,7 @@ public function testInvokeWithCustomConfig() protected function createServiceManagerMock(array $config = []) { $sm = new ServiceManager($config); - //$sm->setService('Config', $config); + $sm->setService('Config', $config); return $sm; } diff --git a/tests/Bridge/Laminas/SlugifyViewHelperFactoryTest.php b/tests/Bridge/Laminas/SlugifyViewHelperFactoryTest.php index 3ba06c07..d19ef522 100644 --- a/tests/Bridge/Laminas/SlugifyViewHelperFactoryTest.php +++ b/tests/Bridge/Laminas/SlugifyViewHelperFactoryTest.php @@ -30,13 +30,10 @@ protected function setUp(): void */ public function testCreateService() { - $this->markTestSkipped(); $sm = new ServiceManager(); $sm->setService('Cocur\Slugify\Slugify', new Slugify()); - $vhm = new HelperPluginManager(); - $vhm->setServiceLocator($sm); - $viewHelper = call_user_func($this->factory, $vhm); + $viewHelper = call_user_func($this->factory, $sm, 'slugify'); $this->assertInstanceOf('Cocur\Slugify\Bridge\Laminas\SlugifyViewHelper', $viewHelper); } } From 7ce0d69926e777c39d078be1105363dfdc97fec4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=20H=C3=A4hnel?= Date: Sat, 14 Sep 2024 17:33:33 +0200 Subject: [PATCH 05/21] Rework laminas module config --- src/Bridge/Laminas/ConfigProvider.php | 55 +++++++++++++++++++++++++++ src/Bridge/Laminas/Module.php | 37 +++--------------- tests/Bridge/Laminas/ModuleTest.php | 24 ++++++------ 3 files changed, 73 insertions(+), 43 deletions(-) create mode 100644 src/Bridge/Laminas/ConfigProvider.php diff --git a/src/Bridge/Laminas/ConfigProvider.php b/src/Bridge/Laminas/ConfigProvider.php new file mode 100644 index 00000000..821445b5 --- /dev/null +++ b/src/Bridge/Laminas/ConfigProvider.php @@ -0,0 +1,55 @@ + $this->getDependencyConfig(), + 'view_helpers' => $this->getViewHelperConfig(), + ]; + } + + /** + * Retrieve laminas default dependency configuration. + * + * @return array + */ + public function getDependencyConfig(): array + { + return [ + 'factories' => [ + Slugify::class => SlugifyService::class, + ], + 'aliases' => [ + 'slugify' => Slugify::class, + ] + ]; + } + + /** + * Retrieve laminas view helper dependency configuration. + * + * @return array + */ + public function getViewHelperConfig(): array + { + return [ + 'aliases' => [ + 'slugify' => SlugifyViewHelper::class + ], + 'factories' => [ + SlugifyViewHelper::class => SlugifyViewHelperFactory::class + ] + ]; + } +} diff --git a/src/Bridge/Laminas/Module.php b/src/Bridge/Laminas/Module.php index abc8f775..56ff5ec4 100644 --- a/src/Bridge/Laminas/Module.php +++ b/src/Bridge/Laminas/Module.php @@ -13,43 +13,16 @@ * @subpackage bridge * @license http://www.opensource.org/licenses/MIT The MIT License */ -class Module implements ServiceProviderInterface, ViewHelperProviderInterface +class Module { public const CONFIG_KEY = 'cocur_slugify'; - /** - * Expected to return \Laminas\ServiceManager\Config object or array to - * seed such an object. - * - * @return array> - */ - public function getServiceConfig() + public function getConfig() { + $provider = new ConfigProvider(); return [ - 'factories' => [ - Slugify::class => SlugifyService::class, - ], - 'aliases' => [ - 'slugify' => Slugify::class, - ] - ]; - } - - /** - * Expected to return \Laminas\ServiceManager\Config object or array to - * seed such an object. - * - * @return array>|Config - */ - public function getViewHelperConfig() - { - return [ - 'aliases' => [ - 'slugify' => SlugifyViewHelper::class - ], - 'factories' => [ - SlugifyViewHelper::class => SlugifyViewHelperFactory::class - ] + 'dependencies' => $provider->getDependencyConfig(), + 'view_helpers' => $provider->getViewHelperConfig(), ]; } } diff --git a/tests/Bridge/Laminas/ModuleTest.php b/tests/Bridge/Laminas/ModuleTest.php index 2fe9d771..b8b5f2b5 100644 --- a/tests/Bridge/Laminas/ModuleTest.php +++ b/tests/Bridge/Laminas/ModuleTest.php @@ -24,16 +24,17 @@ protected function setUp(): void } /** - * @covers \Cocur\Slugify\Bridge\Laminas\Module::getServiceConfig() + * @covers \Cocur\Slugify\Bridge\Laminas\Module::getDependencyConfig() */ public function testGetServiceConfig() { - $smConfig = $this->module->getServiceConfig(); + $smConfig = $this->module->getConfig(); $this->assertIsArray($smConfig); - $this->assertArrayHasKey('factories', $smConfig); - $this->assertArrayHasKey('Cocur\Slugify\Slugify', $smConfig['factories']); - $this->assertArrayHasKey('aliases', $smConfig); - $this->assertArrayHasKey('slugify', $smConfig['aliases']); + $this->assertArrayHasKey('dependencies', $smConfig); + $this->assertArrayHasKey('factories', $smConfig['dependencies']); + $this->assertArrayHasKey('Cocur\Slugify\Slugify', $smConfig['dependencies']['factories']); + $this->assertArrayHasKey('aliases', $smConfig['dependencies']); + $this->assertArrayHasKey('slugify', $smConfig['dependencies']['aliases']); } /** @@ -41,11 +42,12 @@ public function testGetServiceConfig() */ public function testGetViewHelperConfig() { - $vhConfig = $this->module->getViewHelperConfig(); + $vhConfig = $this->module->getConfig(); $this->assertIsArray($vhConfig); - $this->assertArrayHasKey('factories', $vhConfig); - $this->assertArrayHasKey(SlugifyViewHelper::class, $vhConfig['factories']); - $this->assertArrayHasKey('aliases', $vhConfig); - $this->assertArrayHasKey('slugify', $vhConfig['aliases']); + $this->assertArrayHasKey('view_helpers', $vhConfig); + $this->assertArrayHasKey('factories', $vhConfig['view_helpers']); + $this->assertArrayHasKey(SlugifyViewHelper::class, $vhConfig['view_helpers']['factories']); + $this->assertArrayHasKey('aliases', $vhConfig['view_helpers']); + $this->assertArrayHasKey('slugify', $vhConfig['view_helpers']['aliases']); } } From 8f1417c093ca18827fc8a2c853b07d63d1c84680 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=20H=C3=A4hnel?= Date: Sat, 14 Sep 2024 19:29:28 +0200 Subject: [PATCH 06/21] Improve laminas config --- src/Bridge/Laminas/Module.php | 18 +++++++----------- tests/Bridge/Laminas/ModuleTest.php | 10 +++++----- 2 files changed, 12 insertions(+), 16 deletions(-) diff --git a/src/Bridge/Laminas/Module.php b/src/Bridge/Laminas/Module.php index 56ff5ec4..5940a9ed 100644 --- a/src/Bridge/Laminas/Module.php +++ b/src/Bridge/Laminas/Module.php @@ -2,11 +2,6 @@ namespace Cocur\Slugify\Bridge\Laminas; -use Cocur\Slugify\Slugify; -use Laminas\ModuleManager\Feature\ServiceProviderInterface; -use Laminas\ModuleManager\Feature\ViewHelperProviderInterface; -use Laminas\ServiceManager\Config; - /** * Class Module * @package cocur/slugify @@ -17,12 +12,13 @@ class Module { public const CONFIG_KEY = 'cocur_slugify'; - public function getConfig() + public function getConfig(): array { - $provider = new ConfigProvider(); - return [ - 'dependencies' => $provider->getDependencyConfig(), - 'view_helpers' => $provider->getViewHelperConfig(), - ]; + $provider = new ConfigProvider(); + $config = $provider(); + $config['service_manager'] = $config['dependencies']; + unset($config['dependencies']); + + return $config; } } diff --git a/tests/Bridge/Laminas/ModuleTest.php b/tests/Bridge/Laminas/ModuleTest.php index b8b5f2b5..00682117 100644 --- a/tests/Bridge/Laminas/ModuleTest.php +++ b/tests/Bridge/Laminas/ModuleTest.php @@ -30,11 +30,11 @@ public function testGetServiceConfig() { $smConfig = $this->module->getConfig(); $this->assertIsArray($smConfig); - $this->assertArrayHasKey('dependencies', $smConfig); - $this->assertArrayHasKey('factories', $smConfig['dependencies']); - $this->assertArrayHasKey('Cocur\Slugify\Slugify', $smConfig['dependencies']['factories']); - $this->assertArrayHasKey('aliases', $smConfig['dependencies']); - $this->assertArrayHasKey('slugify', $smConfig['dependencies']['aliases']); + $this->assertArrayHasKey('service_manager', $smConfig); + $this->assertArrayHasKey('factories', $smConfig['service_manager']); + $this->assertArrayHasKey('Cocur\Slugify\Slugify', $smConfig['service_manager']['factories']); + $this->assertArrayHasKey('aliases', $smConfig['service_manager']); + $this->assertArrayHasKey('slugify', $smConfig['service_manager']['aliases']); } /** From 4714effe282ac8b1fed8669934acf18ff27ef85f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=20H=C3=A4hnel?= Date: Mon, 16 Sep 2024 10:03:02 +0200 Subject: [PATCH 07/21] Fix phpunit cover annotations --- tests/Bridge/Laminas/ModuleTest.php | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/tests/Bridge/Laminas/ModuleTest.php b/tests/Bridge/Laminas/ModuleTest.php index 00682117..8e3a20d4 100644 --- a/tests/Bridge/Laminas/ModuleTest.php +++ b/tests/Bridge/Laminas/ModuleTest.php @@ -24,7 +24,8 @@ protected function setUp(): void } /** - * @covers \Cocur\Slugify\Bridge\Laminas\Module::getDependencyConfig() + * @covers \Cocur\Slugify\Bridge\Laminas\Module::getConfig() + * @covers \Cocur\Slugify\Bridge\Laminas\ConfigProvider::getDependencyConfig() */ public function testGetServiceConfig() { @@ -38,7 +39,8 @@ public function testGetServiceConfig() } /** - * @covers \Cocur\Slugify\Bridge\ZF2\Module::getViewHelperConfig() + * @covers \Cocur\Slugify\Bridge\Laminas\Module::getConfig() + * @covers \Cocur\Slugify\Bridge\Laminas\ConfigProvider::getViewHelperConfig() */ public function testGetViewHelperConfig() { From 0851ae4d4028420634d90ac4d4621676d8b44c7c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=20H=C3=A4hnel?= Date: Mon, 16 Sep 2024 10:11:03 +0200 Subject: [PATCH 08/21] Remove Zend Framework 2 integration from Readme --- README.md | 47 ----------------------------------------------- 1 file changed, 47 deletions(-) diff --git a/README.md b/README.md index 79593eeb..c752cbbc 100644 --- a/README.md +++ b/README.md @@ -384,53 +384,6 @@ return array( ); ``` -### Zend Framework 2 - -Slugify can be easely used in Zend Framework 2 applications. Included bridge provides a service and a view helper -already registered for you. - -Just enable the module in your configuration like this. - -```php -return [ - //... - - "modules" => [ - "Application", - "ZfcBase", - "Cocur\Slugify\Bridge\ZF2", // <- Add this line - //... - ], - - //... -]; -``` - -After that you can retrieve the `Cocur\Slugify\Slugify` service (or the `slugify` alias) and generate a slug. - -```php -/** @var \Zend\ServiceManager\ServiceManager $sm */ -$slugify = $sm->get("Cocur\Slugify\Slugify"); -$slug = $slugify->slugify("Hällo Wörld"); -$anotherSlug = $slugify->slugify("Hällo Wörld", "_"); -``` - -In your view templates use the `slugify` helper to generate slugs. - -```php -slugify("Hällo Wörld"); ?> -slugify("Hällo Wörld", "_"); ?> -``` - -The service (which is also used in the view helper) can be customized by defining this configuration key. - -```php -return [ - "cocur_slugify" => [ - "reg_exp" => "/([^a-zA-Z0-9]|-)+/", - ], -]; -``` ### Nette Framework From 0069f5008700a8ee9507a5f31d4076749b14b2d0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=20H=C3=A4hnel?= Date: Mon, 16 Sep 2024 12:05:52 +0200 Subject: [PATCH 09/21] Remove superflous dev dependencies from composer.json --- composer.json | 2 -- 1 file changed, 2 deletions(-) diff --git a/composer.json b/composer.json index dbb0eb98..ed53f17e 100644 --- a/composer.json +++ b/composer.json @@ -42,8 +42,6 @@ "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-modulemanager": "~2.8", - "laminas/laminas-servicemanager": "~3.3", "laminas/laminas-view": "~2.9" }, "autoload": { From 5ca45b9d73cfb5ce636a43378e9b12eee678d995 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=20H=C3=A4hnel?= Date: Mon, 16 Sep 2024 12:25:02 +0200 Subject: [PATCH 10/21] Remove FactoryInterface, use Psr\Container\ContainerInterface --- src/Bridge/Laminas/SlugifyService.php | 12 +++++++----- src/Bridge/Laminas/SlugifyViewHelperFactory.php | 12 +++++++----- 2 files changed, 14 insertions(+), 10 deletions(-) diff --git a/src/Bridge/Laminas/SlugifyService.php b/src/Bridge/Laminas/SlugifyService.php index 7ba8510d..9adc2ee3 100644 --- a/src/Bridge/Laminas/SlugifyService.php +++ b/src/Bridge/Laminas/SlugifyService.php @@ -3,8 +3,9 @@ namespace Cocur\Slugify\Bridge\Laminas; use Cocur\Slugify\Slugify; -use Interop\Container\ContainerInterface; -use Laminas\ServiceManager\Factory\FactoryInterface; +use Psr\Container\ContainerInterface; +use Psr\Container\ContainerExceptionInterface; +use Psr\Container\NotFoundExceptionInterface; /** * Class SlugifyService @@ -12,15 +13,16 @@ * @subpackage bridge * @license http://www.opensource.org/licenses/MIT The MIT License */ -class SlugifyService implements FactoryInterface +class SlugifyService { /** * @param ContainerInterface $container * @param $requestedName * @param array|null $options + * * @return Slugify - * @throws \Psr\Container\ContainerExceptionInterface - * @throws \Psr\Container\NotFoundExceptionInterface + * @throws ContainerExceptionInterface + * @throws NotFoundExceptionInterface */ public function __invoke(ContainerInterface $container, $requestedName, ?array $options = null): Slugify { diff --git a/src/Bridge/Laminas/SlugifyViewHelperFactory.php b/src/Bridge/Laminas/SlugifyViewHelperFactory.php index 8b1be830..b7db6c08 100644 --- a/src/Bridge/Laminas/SlugifyViewHelperFactory.php +++ b/src/Bridge/Laminas/SlugifyViewHelperFactory.php @@ -2,8 +2,9 @@ namespace Cocur\Slugify\Bridge\Laminas; -use Interop\Container\ContainerInterface; -use Laminas\ServiceManager\Factory\FactoryInterface; +use Psr\Container\ContainerInterface; +use Psr\Container\ContainerExceptionInterface; +use Psr\Container\NotFoundExceptionInterface; /** * Class SlugifyViewHelperFactory @@ -11,16 +12,17 @@ * @subpackage bridge * @license http://www.opensource.org/licenses/MIT The MIT License */ -class SlugifyViewHelperFactory implements FactoryInterface +class SlugifyViewHelperFactory { /** * @param ContainerInterface $container * @param $requestedName * @param array|null $options + * * @return SlugifyViewHelper - * @throws \Psr\Container\ContainerExceptionInterface - * @throws \Psr\Container\NotFoundExceptionInterface + * @throws ContainerExceptionInterface + * @throws NotFoundExceptionInterface */ public function __invoke(ContainerInterface $container, $requestedName, ?array $options = null): SlugifyViewHelper { From f3995af9982de0533bdb2b46b3c1e120092ef438 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=20H=C3=A4hnel?= Date: Mon, 16 Sep 2024 12:48:40 +0200 Subject: [PATCH 11/21] Add extra laminas config to composer.json --- composer.json | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/composer.json b/composer.json index ed53f17e..a3402c3d 100644 --- a/composer.json +++ b/composer.json @@ -22,6 +22,12 @@ "php": "~8.0.0 || ~8.1.0 || ~8.2.0 || ~8.3.0 || ~8.4.0", "ext-mbstring": "*" }, + "extra": { + "laminas": { + "config-provider": "Cocur\\Slugify\\Bridge\\Laminas\\ConfigProvider", + "module": "Cocur\\Slugify\\Bridge\\Laminas" + } + }, "conflict": { "symfony/config": "<3.4 || >=4,<4.3", "symfony/dependency-injection": "<3.4 || >=4,<4.3", From bf3a7c011aa64aa3e2e66e900ae00b6136b84bcc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=20H=C3=A4hnel?= Date: Mon, 16 Sep 2024 13:26:34 +0200 Subject: [PATCH 12/21] Remove superflous parameter from __invoke methods --- src/Bridge/Laminas/SlugifyService.php | 4 +--- src/Bridge/Laminas/SlugifyViewHelperFactory.php | 4 +--- tests/Bridge/Laminas/SlugifyServiceTest.php | 11 ++++++++--- tests/Bridge/Laminas/SlugifyViewHelperFactoryTest.php | 3 +-- 4 files changed, 11 insertions(+), 11 deletions(-) diff --git a/src/Bridge/Laminas/SlugifyService.php b/src/Bridge/Laminas/SlugifyService.php index 9adc2ee3..b6972e2b 100644 --- a/src/Bridge/Laminas/SlugifyService.php +++ b/src/Bridge/Laminas/SlugifyService.php @@ -17,14 +17,12 @@ class SlugifyService { /** * @param ContainerInterface $container - * @param $requestedName - * @param array|null $options * * @return Slugify * @throws ContainerExceptionInterface * @throws NotFoundExceptionInterface */ - public function __invoke(ContainerInterface $container, $requestedName, ?array $options = null): Slugify + public function __invoke(ContainerInterface $container): Slugify { $config = $container->get('Config'); diff --git a/src/Bridge/Laminas/SlugifyViewHelperFactory.php b/src/Bridge/Laminas/SlugifyViewHelperFactory.php index b7db6c08..6f6b65f4 100644 --- a/src/Bridge/Laminas/SlugifyViewHelperFactory.php +++ b/src/Bridge/Laminas/SlugifyViewHelperFactory.php @@ -17,14 +17,12 @@ class SlugifyViewHelperFactory /** * @param ContainerInterface $container - * @param $requestedName - * @param array|null $options * * @return SlugifyViewHelper * @throws ContainerExceptionInterface * @throws NotFoundExceptionInterface */ - public function __invoke(ContainerInterface $container, $requestedName, ?array $options = null): SlugifyViewHelper + public function __invoke(ContainerInterface $container): SlugifyViewHelper { $slugify = $container->get('Cocur\Slugify\Slugify'); diff --git a/tests/Bridge/Laminas/SlugifyServiceTest.php b/tests/Bridge/Laminas/SlugifyServiceTest.php index 423b4a0c..7999d350 100644 --- a/tests/Bridge/Laminas/SlugifyServiceTest.php +++ b/tests/Bridge/Laminas/SlugifyServiceTest.php @@ -32,7 +32,7 @@ protected function setUp(): void public function testInvokeWithoutCustomConfig() { $sm = $this->createServiceManagerMock(); - $slugify = call_user_func($this->slugifyService, $sm, 'slugify'); + $slugify = call_user_func($this->slugifyService, $sm); $this->assertInstanceOf('Cocur\Slugify\Slugify', $slugify); // Make sure reg exp is default one @@ -51,7 +51,7 @@ public function testInvokeWithCustomConfig() 'options' => ['regexp' => '/([^a-z0-9.]|-)+/'] ] ]); - $slugify = call_user_func($this->slugifyService, $sm, 'slugify'); + $slugify = call_user_func($this->slugifyService, $sm); $this->assertInstanceOf('Cocur\Slugify\Slugify', $slugify); // Make sure regexp is the one provided and dots are kept @@ -60,7 +60,12 @@ public function testInvokeWithCustomConfig() $this->assertSame($expected, $slugify->slugify($actual)); } - protected function createServiceManagerMock(array $config = []) + /** + * @param array $config + * + * @return ServiceManager + */ + protected function createServiceManagerMock(array $config = []): ServiceManager { $sm = new ServiceManager($config); $sm->setService('Config', $config); diff --git a/tests/Bridge/Laminas/SlugifyViewHelperFactoryTest.php b/tests/Bridge/Laminas/SlugifyViewHelperFactoryTest.php index d19ef522..c0244ab2 100644 --- a/tests/Bridge/Laminas/SlugifyViewHelperFactoryTest.php +++ b/tests/Bridge/Laminas/SlugifyViewHelperFactoryTest.php @@ -4,7 +4,6 @@ use Cocur\Slugify\Bridge\Laminas\SlugifyViewHelperFactory; use Cocur\Slugify\Slugify; use Laminas\ServiceManager\ServiceManager; -use Laminas\View\HelperPluginManager; use Mockery\Adapter\Phpunit\MockeryTestCase; /** @@ -33,7 +32,7 @@ public function testCreateService() $sm = new ServiceManager(); $sm->setService('Cocur\Slugify\Slugify', new Slugify()); - $viewHelper = call_user_func($this->factory, $sm, 'slugify'); + $viewHelper = call_user_func($this->factory, $sm); $this->assertInstanceOf('Cocur\Slugify\Bridge\Laminas\SlugifyViewHelper', $viewHelper); } } From c8205db1b53df401df4ee7d6936990f226c0b428 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=20H=C3=A4hnel?= Date: Mon, 16 Sep 2024 15:54:18 +0200 Subject: [PATCH 13/21] Add laminas input filter --- composer.json | 3 +- src/Bridge/Laminas/SlugifyFilter.php | 56 +++++++++++++++++ tests/Bridge/Laminas/SlugifyFilterTest.php | 73 ++++++++++++++++++++++ 3 files changed, 131 insertions(+), 1 deletion(-) create mode 100644 src/Bridge/Laminas/SlugifyFilter.php create mode 100644 tests/Bridge/Laminas/SlugifyFilterTest.php diff --git a/composer.json b/composer.json index a3402c3d..9c37b470 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 00000000..cd15b144 --- /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 00000000..a516f692 --- /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()); + } + +} From 7a2432ef66ba48a1e00bf150b802ab156717af2c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=20H=C3=A4hnel?= Date: Mon, 16 Sep 2024 20:37:37 +0200 Subject: [PATCH 14/21] Decrease laminas/laminas-inputfilter version, because of php version 8.0.x for test --- composer.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/composer.json b/composer.json index 9c37b470..0a76a689 100644 --- a/composer.json +++ b/composer.json @@ -49,7 +49,7 @@ "symfony/phpunit-bridge": "^5.4 || ^6.0", "twig/twig": "^2.12.1 || ~3.0", "laminas/laminas-view": "~2.9", - "laminas/laminas-inputfilter": "~2.30" + "laminas/laminas-inputfilter": "~2.24" }, "autoload": { "psr-4": { From a2815929bff158fd8fb7c9b76725928b4961dd42 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=20H=C3=A4hnel?= Date: Thu, 7 Nov 2024 11:09:02 +0100 Subject: [PATCH 15/21] Update README.md MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Frank Brückner --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index c752cbbc..497a7727 100644 --- a/README.md +++ b/README.md @@ -339,7 +339,7 @@ $url = Slugify::slugify("welcome to the homepage"); ### Laminas -Slugify can be easily used in Laminas applications. Included bridge provides a service and a view helper +Slugify can be easily used in Mezzio or laminas-mvc applications. Included bridge provides a filter and a view helper already registered for you. Just enable the module in your configuration like this. From d534c6d439da93a2867a191f8d15c3c30f3f8afd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=20H=C3=A4hnel?= Date: Thu, 7 Nov 2024 11:09:40 +0100 Subject: [PATCH 16/21] Update README.md MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Frank Brückner --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 497a7727..13f9f15e 100644 --- a/README.md +++ b/README.md @@ -337,7 +337,7 @@ You can then use the `Slugify::slugify()` method in your controllers: $url = Slugify::slugify("welcome to the homepage"); ``` -### Laminas +### Mezzio and laminas-mvc Slugify can be easily used in Mezzio or laminas-mvc applications. Included bridge provides a filter and a view helper already registered for you. From 21cebf03a59e4a5b75a955ec2de2a224c2f18b84 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=20H=C3=A4hnel?= Date: Thu, 7 Nov 2024 11:21:11 +0100 Subject: [PATCH 17/21] Update README.md --- README.md | 20 ++++++++------------ 1 file changed, 8 insertions(+), 12 deletions(-) diff --git a/README.md b/README.md index 13f9f15e..e04a1c48 100644 --- a/README.md +++ b/README.md @@ -345,17 +345,13 @@ already registered for you. Just enable the module in your configuration like this. ```php -return array( - //... - - 'modules' => array( +return [ + 'modules' => [ 'Application', 'Cocur\Slugify\Bridge\Laminas' // <- Add this line //... - ) - - //... -); + ] +]; ``` After that you can retrieve the `Cocur\Slugify\Slugify` service (or the `slugify` alias) and generate a slug. @@ -377,11 +373,11 @@ In your view templates use the `slugify` helper to generate slugs. The service (which is also used in the view helper) can be customized by defining this configuration key. ```php -return array( - 'cocur_slugify' => array( +return [ + 'cocur_slugify' => [ 'reg_exp' => '/([^a-zA-Z0-9]|-)+/' - ) -); + ] +]; ``` From 671b42d98427edc0b9df551552eb3abec645ba75 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=20H=C3=A4hnel?= Date: Thu, 7 Nov 2024 11:50:32 +0100 Subject: [PATCH 18/21] Implement filter interface instead extend from abstract filter --- src/Bridge/Laminas/SlugifyFilter.php | 24 +++++++++++++++++++----- 1 file changed, 19 insertions(+), 5 deletions(-) diff --git a/src/Bridge/Laminas/SlugifyFilter.php b/src/Bridge/Laminas/SlugifyFilter.php index cd15b144..ddc292a3 100644 --- a/src/Bridge/Laminas/SlugifyFilter.php +++ b/src/Bridge/Laminas/SlugifyFilter.php @@ -3,7 +3,7 @@ namespace Cocur\Slugify\Bridge\Laminas; use Cocur\Slugify\Slugify; -use Laminas\Filter\AbstractFilter; +use Laminas\Filter\FilterInterface; /** * Class SlugifyFilter @@ -12,13 +12,13 @@ * @subpackage bridge * @license http://www.opensource.org/licenses/MIT The MIT License */ -class SlugifyFilter extends AbstractFilter +class SlugifyFilter implements FilterInterface { /** * @var array * @see Slugify::$options */ - protected $options = [ + protected array $options = [ 'regexp' => Slugify::LOWERCASE_NUMBERS_DASHES, 'separator' => '-', 'lowercase' => true, @@ -28,9 +28,9 @@ class SlugifyFilter extends AbstractFilter ]; /** - * @param array|null $options + * @param array $options */ - public function __construct(?array $options = null) + public function __construct(array $options = []) { if (!empty($options)) { $this->setOptions($options); @@ -53,4 +53,18 @@ public function filter($value) return $value; } + + /** + * @param array $options + * + * @return void + */ + protected function setOptions(array $options) + { + foreach ($options as $key => $option) { + if (array_key_exists($key, $this->options)) { + $this->options[$key] = $option; + } + } + } } From c86dc0ff45164873d7f7c79f13731089fe23661a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=20H=C3=A4hnel?= Date: Thu, 7 Nov 2024 12:15:42 +0100 Subject: [PATCH 19/21] Remove laminas-inputfilter, use laminas-filter instead --- composer.json | 2 +- tests/Bridge/Laminas/SlugifyFilterTest.php | 23 ++++++++-------------- 2 files changed, 9 insertions(+), 16 deletions(-) diff --git a/composer.json b/composer.json index 0a76a689..a11a36a8 100644 --- a/composer.json +++ b/composer.json @@ -49,7 +49,7 @@ "symfony/phpunit-bridge": "^5.4 || ^6.0", "twig/twig": "^2.12.1 || ~3.0", "laminas/laminas-view": "~2.9", - "laminas/laminas-inputfilter": "~2.24" + "laminas/laminas-filter": "^2.31" }, "autoload": { "psr-4": { diff --git a/tests/Bridge/Laminas/SlugifyFilterTest.php b/tests/Bridge/Laminas/SlugifyFilterTest.php index a516f692..10946a49 100644 --- a/tests/Bridge/Laminas/SlugifyFilterTest.php +++ b/tests/Bridge/Laminas/SlugifyFilterTest.php @@ -5,7 +5,6 @@ use Cocur\Slugify\Bridge\Laminas\SlugifyFilter; use Laminas\Filter\FilterChain; -use Laminas\InputFilter\Input; use Mockery\Adapter\Phpunit\MockeryTestCase; /** @@ -17,14 +16,6 @@ */ class SlugifyFilterTest extends MockeryTestCase { - /** @var Input */ - protected Input $input; - - protected function setUp(): void - { - $this->input = new Input(''); - } - /** * @covers \Cocur\Slugify\Bridge\Laminas\SlugifyFilter::filter() */ @@ -39,10 +30,11 @@ public function testSlugifyFilterWithoutCustomOptions() ], ] ); - $this->input->setFilterChain($chain); - $this->input->setValue('foo Bar'); - $this->assertSame('foo-bar', $this->input->getValue()); + $value = 'foo Bar'; + $expected = 'foo-bar'; + + $this->assertSame($expected, $chain->filter($value)); } @@ -64,10 +56,11 @@ public function testSlugifyFilterWithCustomOptions() ], ] ); - $this->input->setFilterChain($chain); - $this->input->setValue('0123 foo bar '); - $this->assertSame('0123-test', $this->input->getValue()); + $value = '0123 foo bar '; + $expected = '0123-test'; + + $this->assertSame($expected, $chain->filter($value)); } } From 714a400bd8427249fdc523022fa3bd302e98dd1a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=20H=C3=A4hnel?= Date: Thu, 7 Nov 2024 12:34:43 +0100 Subject: [PATCH 20/21] Add filter config --- src/Bridge/Laminas/ConfigProvider.php | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/src/Bridge/Laminas/ConfigProvider.php b/src/Bridge/Laminas/ConfigProvider.php index 821445b5..cd75ade0 100644 --- a/src/Bridge/Laminas/ConfigProvider.php +++ b/src/Bridge/Laminas/ConfigProvider.php @@ -3,6 +3,7 @@ namespace Cocur\Slugify\Bridge\Laminas; use Cocur\Slugify\Slugify; +use Laminas\ServiceManager\Factory\InvokableFactory; class ConfigProvider { @@ -15,6 +16,7 @@ public function __invoke(): array { return [ 'dependencies' => $this->getDependencyConfig(), + 'filters' => $this->filterConfig(), 'view_helpers' => $this->getViewHelperConfig(), ]; } @@ -52,4 +54,19 @@ public function getViewHelperConfig(): array ] ]; } + + /** + * @return array + */ + private function filterConfig(): array + { + return [ + 'factories' => [ + SlugifyFilter::class => InvokableFactory::class, + ], + 'aliases' => [ + 'slugify' => SlugifyFilter::class, + ], + ]; + } } From efdbbaf78aa3798377269dccadb58373f291ef29 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=20H=C3=A4hnel?= Date: Thu, 7 Nov 2024 13:42:01 +0100 Subject: [PATCH 21/21] Adjust readme, add laminas usage --- README.md | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index e04a1c48..2e06b327 100644 --- a/README.md +++ b/README.md @@ -353,7 +353,7 @@ return [ ] ]; ``` - +It will automatically inject the config-provider or the module in the configuration during the installation process. After that you can retrieve the `Cocur\Slugify\Slugify` service (or the `slugify` alias) and generate a slug. ```php @@ -363,6 +363,25 @@ $slug = $slugify->slugify('Hällo Wörld'); $anotherSlug = $slugify->slugify('Hällo Wörld', '_'); ``` +It can be used in form filters as follows. + +```php +'my_form_input' => [ + 'filters' => [ + [ + 'name' => SlugifyFilter::class, + 'options' => [ + 'regexp' => Slugify::LOWERCASE_NUMBERS_DASHES, + 'strip_tags' => true, + //... + ] + ], + ], + //... +], +//... +``` + In your view templates use the `slugify` helper to generate slugs. ```php