1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Particle. |
4
|
|
|
* |
5
|
|
|
* @link http://github.com/particle-php for the canonical source repository |
6
|
|
|
* @copyright Copyright (c) 2005-2016 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
|
|
|
* Allows a default value to be set if no data key was provided |
22
|
|
|
* |
23
|
|
|
* @var bool |
24
|
|
|
*/ |
25
|
|
|
protected $allowNotSet = true; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @var string |
29
|
|
|
*/ |
30
|
|
|
private $fieldToSlugFrom; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @var string |
34
|
|
|
*/ |
35
|
|
|
private $transliterator = 'Any-Latin; Latin-ASCII; NFD; [:Nonspacing Mark:] Remove; NFC; [:Punctuation:] Remove; Lower();'; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @param string $fieldToSlugFrom |
39
|
|
|
*/ |
40
|
8 |
|
public function __construct($fieldToSlugFrom) |
41
|
|
|
{ |
42
|
8 |
|
$this->fieldToSlugFrom = $fieldToSlugFrom; |
43
|
8 |
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Slug the value of either the actual field of the given one. |
47
|
|
|
* |
48
|
|
|
* @param mixed $value |
49
|
|
|
* @return string |
50
|
|
|
*/ |
51
|
8 |
|
public function filter($value) |
52
|
|
|
{ |
53
|
8 |
|
if (empty($value) && isset($this->filterData[$this->fieldToSlugFrom])) { |
54
|
1 |
|
$value = $this->filterData[$this->fieldToSlugFrom]; |
55
|
1 |
|
} |
56
|
|
|
|
57
|
8 |
|
if (is_null($value)) { |
58
|
1 |
|
return $this->setEmpty(); |
59
|
|
|
} |
60
|
|
|
|
61
|
7 |
|
$value = transliterator_transliterate($this->transliterator, $value); |
62
|
7 |
|
$value = iconv("UTF-8", "ASCII//TRANSLIT//IGNORE", $value); |
63
|
7 |
|
$value = preg_replace('/[-$?\s]+/', '-', $value); |
64
|
7 |
|
$value = trim($value, '-'); |
65
|
7 |
|
return strtolower($value); |
66
|
|
|
} |
67
|
|
|
} |
68
|
|
|
|