Skip to content

Commit ede2aa5

Browse files
committed
Merge pull request #19 from UseMuffin/cake-slugger
Cake slugger
2 parents 5b88b88 + cab1029 commit ede2aa5

4 files changed

Lines changed: 48 additions & 21 deletions

File tree

.travis.yml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
language: php
22

33
php:
4-
- 5.4
54
- 5.5
65
- 5.6
76
- 7.0

README.md

Lines changed: 36 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,20 @@ Slugging for CakePHP 3.x
99

1010
## Requirements
1111

12-
- CakePHP 3.0+
12+
- CakePHP 3.2.7+ (for lower CakePHP versions use plugin version 1.0.*)
1313

1414
## Installation
1515

1616
Using [Composer][composer]:
1717

1818
```bash
19-
composer require muffin/slug:~1.0
19+
composer require muffin/slug
20+
```
21+
22+
or if your CakePHP is less than 3.2.7 use
23+
24+
```bash
25+
composer require muffin/slug:1.0.*
2026
```
2127

2228
To make your application load the plugin either run:
@@ -38,10 +44,10 @@ To enable slugging add the behavior to your table classes in the
3844
```php
3945
public function initialize(array $config)
4046
{
41-
//etc
42-
$this->addBehavior('Muffin/Slug.Slug', [
43-
// Optionally define your custom options here (see Configuration)
44-
]);
47+
//etc
48+
$this->addBehavior('Muffin/Slug.Slug', [
49+
// Optionally define your custom options here (see Configuration)
50+
]);
4551
}
4652
```
4753

@@ -77,9 +83,32 @@ Slug comes with the following configuration options:
7783
- `implementedEvents`: events this behavior listens to.
7884
- `implementedFinders`: custom finders implemented by this behavior.
7985
- `implementedMethods`: mixin methods directly accessible from the table.
80-
- `onUpdate`: Boolean indicating whether slug should be updated when updating
86+
- `onUpdate`: Boolean indicating whether slug should be updated when updating
8187
record, defaults to `false`.
8288

89+
## Sluggers
90+
91+
The plugin contains two sluggers:
92+
93+
### CakeSlugger
94+
95+
The `CakeSlugger` uses `\Cake\Utility\Text::slug()` to generate slugs. In the
96+
behavior config you can set the `slugger` key as shown below to pass options to
97+
the `$options` arguments of `Text::slug()`.
98+
99+
```php
100+
'slugger' => [
101+
'className' => 'Muffin\Slug\Slugger\CakeSlugger',
102+
'transliteratorId' => '<A valid ICU Transliterator ID here>'
103+
]
104+
```
105+
106+
### ConcurSlugger
107+
108+
The `ConcurSlugger` uses [concur/slugify](https://github.com/cocur/slugify) to generate slugs.
109+
You can use config array similar to the one shown above to pass options to
110+
`Cocur\Slugify\Slugify`'s constructor.
111+
83112
## Patches & Features
84113

85114
* Fork
@@ -100,10 +129,6 @@ ln -s ../../contrib/pre-commit .git/hooks/.
100129

101130
http://github.com/usemuffin/slug/issues
102131

103-
## Credits
104-
105-
This was originally inspired by @dereuromark's [`SluggedBehavior`](https://github.com/dereuromark/cakephp-tools/blob/cake3/src/Model/Behavior/SluggedBehavior.php).
106-
107132
## License
108133

109134
Copyright (c) 2015, [Use Muffin][muffin] and licensed under [The MIT License][mit].

composer.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,10 @@
3131
"source": "https://github.com/usemuffin/slug"
3232
},
3333
"require": {
34-
"cakephp/orm": "~3.1"
34+
"cakephp/orm": "^3.2.7"
3535
},
3636
"require-dev": {
37-
"cakephp/cakephp": "~3.1",
37+
"cakephp/cakephp": "^3.2.7",
3838
"phpunit/phpunit": "4.1.*",
3939
"cocur/slugify": "^1.2"
4040
},

src/Slugger/CakeSlugger.php

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,21 @@
11
<?php
22
namespace Muffin\Slug\Slugger;
33

4-
use Cake\Utility\Inflector;
4+
use Cake\Utility\Text;
55
use Muffin\Slug\SluggerInterface;
66

77
/**
8-
* CakePHP slugger (using `Inflector::slug()`).
8+
* CakePHP slugger.
99
*/
1010
class CakeSlugger implements SluggerInterface
1111
{
1212

1313
/**
14-
* Config options.
14+
* Config options. You can set any valid option which Text::slug() takes
15+
* besides the one listed below:
1516
*
1617
* - `lowercase` - Boolean indication whether slug should be lowercased.
17-
* Default to true.
18+
* Defaults to true.
1819
*
1920
* @var array
2021
*/
@@ -31,10 +32,12 @@ class CakeSlugger implements SluggerInterface
3132
*/
3233
public function slug($string, $replacement = '-')
3334
{
34-
$string = Inflector::slug($string, $replacement);
35+
$config = $this->config;
36+
$config['replacement'] = $replacement;
37+
$string = Text::slug($string, $config);
3538

36-
if ($this->config['lowercase']) {
37-
return strtolower($string);
39+
if ($config['lowercase']) {
40+
return mb_strtolower($string);
3841
}
3942

4043
return $string;

0 commit comments

Comments
 (0)