Completed
Pull Request — master (#47)
by
unknown
09:17
created

Slug   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 39
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 1
Metric Value
wmc 4
c 1
b 0
f 1
lcom 1
cbo 0
dl 0
loc 39
ccs 12
cts 12
cp 1
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A filter() 0 12 3
1
<?php
2
/**
3
 * Particle.
4
 *
5
 * @link      http://github.com/particle-php for the canonical source repository
6
 * @copyright Copyright (c) 2005-2015 Particle (http://particle-php.com)
7
 * @license   https://github.com/particle-php/Filter/blob/master/LICENSE New BSD License
8
 */
9
namespace Particle\Filter\FilterRule;
10
11
use Particle\Filter\FilterRule;
12
13
/**
14
 * Class Slug
15
 *
16
 * @package Particle\Filter\FilterRule
17
 */
18
class Slug extends FilterRule
19
{
20
    /**
21
     * @var string
22
     */
23
    private $fieldToSlugFrom;
24
25
    /**
26
     * @var string
27
     */
28
    private $transliterator = "Any-Latin; Latin-ASCII; NFD; [:Nonspacing Mark:] Remove; NFC; [:Punctuation:] Remove; Lower();";
29
30
    /**
31
     * @param string $fieldToSlugFrom
32
     */
33 7
    public function __construct($fieldToSlugFrom)
34
    {
35 7
        $this->fieldToSlugFrom = $fieldToSlugFrom;
36 7
    }
37
38
    /**
39
     * Slug the value of either the actual field of the given one.
40
     *
41
     * @param mixed $value
42
     * @return string
43
     */
44 7
    public function filter($value)
45
    {
46 7
        if (empty($value) && isset($this->filterData[$this->fieldToSlugFrom])) {
47 1
            $value =  $this->filterData[$this->fieldToSlugFrom];
48 1
        }
49
50 7
        $value = transliterator_transliterate($this->transliterator, $value);
51 7
        $value = iconv("UTF-8", "ASCII//TRANSLIT//IGNORE", $value);
52 7
        $value = preg_replace('/[-$?\s]+/', '-', $value);
53 7
        $value = trim($value, '-');
54 7
        return strtolower($value);
55
    }
56
}
57