Completed
Push — master ( c63978...2e5569 )
by Mathieu
04:42
created

RoutableTrait::setSlugPattern()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 5
rs 9.4285
cc 1
eloc 3
nc 1
nop 1
1
<?php
0 ignored issues
show
Coding Style Compatibility introduced by
For compatibility and reusability of your code, PSR1 recommends that a file should introduce either new symbols (like classes, functions, etc.) or have side-effects (like outputting something, or including other files), but not both at the same time. The first symbol is defined on line 27 and the first side effect is on line 11.

The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.

The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.

To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.

Loading history...
2
3
namespace Charcoal\Object;
4
5
use \Charcoal\View\Viewable;
6
use \Charcoal\Translation\TranslationString;
7
8
/**
9
 * Full implementation, as Trait, of the `RoutableInterface`.
10
 */
11
trait RoutableTrait
0 ignored issues
show
Bug introduced by
Possible parse error: trait missing opening or closing brace
Loading history...
12
{
13
    /**
14
     * @var string
15
     */
16
    private $slugPattern = '';
17
18
    /**
19
     * @var string $slug
20
     */
21
    private $slug;
22
23
    /**
24
     * @param mixed $pattern The slug pattern.
25
     * @return RoutableInterface Chainable
26
     */
27
    public function setSlugPattern($pattern)
28
    {
29
        $this->slugPattern = new TranslationString($pattern);
30
        return $this;
31
    }
32
33
    /**
34
     * @return TranslationString
35
     */
36
    public function slugPattern()
37
    {
38
        if (!$this->slugPattern) {
39
            $metadata = $this->medatada();
40
            return isset($metadata['slug_pattern']) ? $metadata['slug_pattern'] : '';
41
        }
42
        return $this->slugPattern;
43
44
    /**
45
     * @param mixed $slug The slug.
46
     * @return RoutableInterface Chainable
47
     */
48
    public function setSlug($slug)
0 ignored issues
show
Bug introduced by
This code did not parse for me. Apparently, there is an error somewhere around this line:

Syntax error, unexpected T_PUBLIC
Loading history...
49
    {
50
        $this->slug = $slug;
51
        return $this;
52
    }
53
54
    /**
55
     * @return string
56
     */
57
    public function slug()
58
    {
59
        if ($this->slug === null) {
60
            $this->slug = $this->generateSlug();
61
        }
62
        return $this->slug;
63
    }
64
65
    /**
66
     * Generate a URL slug from the object's URL slug pattern.
67
     *
68
     * @return string
69
     */
70
    public function generateSlug()
71
    {
72
        $pattern = $this->slugPattern();
73
        if ($this instanceof Viewable) {
74
            $slug = $this->render($pattern);
75
        } else {
76
            $slug = $pattern;
77
        }
78
        return $slug;
79
    }
80
81
    /**
82
     * @return string
83
     */
84
    public function url()
85
    {
86
        return $this->slug();
87
    }
88
}
89