Completed
Push — master ( 07fe9b...05393a )
by Terzi
04:43
created

ElementContainer::setTitle()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 1
dl 0
loc 8
ccs 4
cts 4
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Terranet\Administrator\Traits\Collection;
4
5
use Coduo\PHPHumanizer\StringHumanizer;
6
use Terranet\Administrator\Contracts\AutoTranslatable;
7
use Terranet\Administrator\Scaffolding;
8
use Terranet\Administrator\Traits\AutoTranslatesInstances;
9
10
abstract class ElementContainer implements AutoTranslatable
11
{
12
    use AutoTranslatesInstances;
13
14
    /** @var string */
15
    protected $id;
16
17
    /** @var string */
18
    protected $title;
19
20
    /** @var null|object */
21
    protected $module;
22
23
    /**
24
     * Element constructor.
25
     *
26
     * @param $id
27
     */
28 15
    public function __construct($id)
29
    {
30 15
        $this->setId($id);
31
32 15
        if ($this->translator()->has($key = $this->translationKey())) {
33
            $this->setTitle(trans($key));
34
        } else {
35 15
            $this->setTitle(
36 15
                'id' === $id
37
                    ? 'ID'
38 15
                    : StringHumanizer::humanize(str_replace(['_id', '-', '_'], ['', ' ', ' '], $id))
39
            );
40
        }
41 15
    }
42
43
    /**
44
     * Set element ID.
45
     *
46
     * @param $id
47
     *
48
     * @return $this
49
     */
50 15
    public function setId($id)
51
    {
52 15
        $this->id = $this->buildId($id);
53
54 15
        return $this;
55
    }
56
57
    /**
58
     * Set element title.
59
     *
60
     * @param $title
61
     *
62
     * @return $this
63
     */
64 15
    public function setTitle($title)
65
    {
66
        // handle relations-style titles
67 15
        $title = preg_replace('~^(\w+)\.(\w+)$~si', '$1 $2', $title);
68
69 15
        $this->title = $title;
70
71 15
        return $this;
72
    }
73
74
    /**
75
     * Get element id.
76
     *
77
     * @return string
78
     */
79 2
    public function id()
80
    {
81 2
        return $this->id;
82
    }
83
84
    /**
85
     * Get element title.
86
     *
87
     * @return string
88
     */
89 3
    public function title()
90
    {
91 3
        return $this->title;
92
    }
93
94
    /**
95
     * @return string
96
     */
97 15
    public function translationKey()
98
    {
99 15
        $key = sprintf('administrator::columns.%s.%s', $this->module()->url(), $this->id);
100
101 15
        if (!$this->translator()->has($key)) {
102 15
            $key = sprintf('administrator::columns.%s.%s', 'global', $this->id);
103
        }
104
105 15
        return $key;
106
    }
107
108
    public function setModule($module)
109
    {
110
        $this->module = $module;
111
112
        return $this;
113
    }
114
115
    /**
116
     * @param $id
117
     *
118
     * @return string
119
     */
120 15
    protected function buildId($id)
121
    {
122 15
        $parts = explode('.', $id);
123
        $parts = array_map(function ($part) {
124 15
            return str_slug($part, '_');
125 15
        }, $parts);
126
127 15
        return implode('.', $parts);
128
    }
129
130
    /**
131
     * @return Scaffolding
132
     */
133 15
    protected function module()
134
    {
135 15
        return $this->module ?: app('scaffold.module');
136
    }
137
}
138