GenerateSlug::generateSlug()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 10
rs 9.4286
cc 1
eloc 7
nc 1
nop 1
1
<?php
2
3
namespace Fillet\TextUtils;
4
5
/**
6
 * Helper trait for generating properly slugged titles
7
 *
8
 * @package Fillet\TextUtils
9
 */
10
trait GenerateSlug
11
{
12
    /**
13
     * Generate an appropriate slug from a title
14
     * This function will turn a title into a lowercase and hyphenated title that is compatible with how Sculpin expects
15
     * slugs.
16
     *
17
     * @param string $title Raw title
18
     *
19
     * @return string
20
     */
21
    public function generateSlug($title)
22
    {
23
        $title = strtolower($title);
24
        $title = preg_replace('/[^%a-z0-9 _-]/', '', $title);
25
        $title = preg_replace('/\s+/', '-', $title);
26
        $title = preg_replace('|-+|', '-', $title);
27
        $title = trim($title, '-');
28
29
        return $title;
30
    }
31
}