Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added first version of Laminas integration #261

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 47 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

easily...

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
<?php echo $this->slugify('Hällo Wörld') ?>
<?php echo $this->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
Expand Down
3 changes: 3 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
55 changes: 55 additions & 0 deletions src/Bridge/Laminas/Module.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?php

namespace Cocur\Slugify\Bridge\Laminas;

use Cocur\Slugify\Slugify;
use Laminas\ModuleManager\Feature\ServiceProviderInterface;
use Laminas\ModuleManager\Feature\ViewHelperProviderInterface;
use Zend\ServiceManager\Config;

/**
* Class Module
* @package cocur/slugify
* @subpackage bridge
* @license http://www.opensource.org/licenses/MIT The MIT License
*/
class Module implements ServiceProviderInterface, ViewHelperProviderInterface

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you want to make an update of the ZF / Laminas bridge then the support should also be extended for Mezzio (formerly Expressive).
This means to create a module class to support a MVC based application and to create a config-provider class to support a Mezzio based application.

Examples can be found in laminas-db:

As you can see, the module class consumes the configuration from config-provider class.

{
const CONFIG_KEY = 'cocur_slugify';

/**
* Expected to return \Laminas\ServiceManager\Config object or array to
* seed such an object.
*
* @return array<string,array<string,string>>
*/
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<string,array<string,string>>|Config
*/
public function getViewHelperConfig()
{
return [
'aliases' => [
'slugify' => SlugifyViewHelper::class
],
'factories' => [
SlugifyViewHelper::class => SlugifyViewHelperFactory::class
]
];
}
}
34 changes: 34 additions & 0 deletions src/Bridge/Laminas/SlugifyService.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

namespace Cocur\Slugify\Bridge\Laminas;

use Cocur\Slugify\Slugify;
use Interop\Container\ContainerInterface;
use Laminas\ServiceManager\Factory\FactoryInterface;

/**
* Class SlugifyService
* @package cocur/slugify
* @subpackage bridge
* @license http://www.opensource.org/licenses/MIT The MIT License
*/
class SlugifyService implements FactoryInterface

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would suggest a filter here instead of a service because this package provides a conversion / transformation of (string) data.
To create a custom filter please have a look at the official documentation of laminas-filter: "Writing Filters"

{

/**
*
* @param ContainerInterface $container
* @param string $requestedName
* @param array $options
* @return object
*/
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;

return new Slugify($slugifyOptions, $provider);
}

}
41 changes: 41 additions & 0 deletions src/Bridge/Laminas/SlugifyViewHelper.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

namespace Cocur\Slugify\Bridge\Laminas;

use Cocur\Slugify\SlugifyInterface;
use Laminas\View\Helper\AbstractHelper;

/**
* Class SlugifyViewHelper
* @package cocur/slugify
* @subpackage bridge
* @license http://www.opensource.org/licenses/MIT The MIT License
*/
class SlugifyViewHelper extends AbstractHelper
{
/**
* @var SlugifyInterface
*/
protected $slugify;

/**
* @param SlugifyInterface $slugify
*
* @codeCoverageIgnore
*/
public function __construct(SlugifyInterface $slugify)
{
$this->slugify = $slugify;
}

/**
* @param string $string
* @param string|null $separator
*
* @return string
*/
public function __invoke($string, $separator = null)
{
return $this->slugify->slugify($string, $separator);
}
}
30 changes: 30 additions & 0 deletions src/Bridge/Laminas/SlugifyViewHelperFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

namespace Cocur\Slugify\Bridge\Laminas;

use Interop\Container\ContainerInterface;
use Laminas\ServiceManager\Factory\FactoryInterface;

/**
* Class SlugifyViewHelperFactory
* @package cocur/slugify
* @subpackage bridge
* @license http://www.opensource.org/licenses/MIT The MIT License
*/
class SlugifyViewHelperFactory implements FactoryInterface
{

/**
*
* @param ContainerInterface $container
* @param string $requestedName
* @param array $options
* @return SlugifyViewHelper
*/
public function __invoke(ContainerInterface $container, $requestedName, array $options = null): SlugifyViewHelper
{
$slugify = $container->get('Cocur\Slugify\Slugify');
return new SlugifyViewHelper($slugify);
}

}