SlugSegmentFieldModifier::setDefault()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
1
<?php
2
3
namespace SilverStripe\Forms\SegmentFieldModifier;
4
5
use SilverStripe\Control\HTTPRequest;
6
use SilverStripe\Forms\Filter\SlugFilter;
7
8
class SlugSegmentFieldModifier extends AbstractSegmentFieldModifier
9
{
10
    /**
11
     * @var string
12
     */
13
    protected $default = '';
14
15
    /**
16
     * @param string $default
17
     *
18
     * @return $this
19
     */
20
    public function setDefault($default)
21
    {
22
        $this->default = $default;
23
24
        return $this;
25
    }
26
27
    /**
28
     * @inheritdoc
29
     *
30
     * @param string $value
31
     *
32
     * @return string
33
     */
34
    public function getPreview($value)
35
    {
36
        return $this->getSuggestion($value);
37
    }
38
39
    /**
40
     * @inheritdoc
41
     *
42
     * @param string $value
43
     *
44
     * @return string
45
     */
46
    public function getSuggestion($value)
47
    {
48
        if ($filtered = SlugFilter::create()->filter($this->getValue())) {
49
            return $value . $filtered;
50
        }
51
52
        return $value . $this->default;
53
    }
54
55
    /**
56
     * @return string
57
     */
58
    protected function getValue()
59
    {
60
        if ($this->request instanceof HTTPRequest && $value = $this->request->getVar('value')) {
61
            return $value;
62
        }
63
64
        return '';
65
    }
66
}
67