Passed
Push — master ( 6eb130...07fe9b )
by Terzi
06:06
created

ElementContainer   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 144
Duplicated Lines 0 %

Test Coverage

Coverage 86.11%

Importance

Changes 0
Metric Value
eloc 34
dl 0
loc 144
ccs 31
cts 36
cp 0.8611
rs 10
c 0
b 0
f 0
wmc 13

9 Methods

Rating   Name   Duplication   Size   Complexity  
A setTitle() 0 8 1
A setModule() 0 5 1
A module() 0 3 2
A __construct() 0 11 3
A id() 0 3 1
A setId() 0 5 1
A title() 0 3 1
A buildId() 0 9 1
A translationKey() 0 9 2
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
    /**
15
     * Element id.
16
     *
17
     * @var string
18
     */
19
    protected $id;
20
21
    /**
22
     * Element title.
23
     *
24
     * @var string
25
     */
26
    protected $title;
27
28
    /**
29
     * Keep original id or extract only last part.
30
     *
31
     * @example: When using this Container in Forms, the original id should be kept to allow parsing relations.
32
     * When for columns there is a different logic to extract Relational data.
33
     *
34
     * @var bool
35
     */
36
    protected $keepOriginalID = true;
37
38
    protected $module;
39
40
    /**
41
     * Element constructor.
42
     *
43
     * @param $id
44
     */
45 15
    public function __construct($id)
46
    {
47 15
        $this->setId($id);
48
49 15
        if ($this->translator()->has($key = $this->translationKey())) {
50
            $this->setTitle(trans($key));
51
        } else {
52 15
            $this->setTitle(
53 15
                'id' === $id
54
                    ? 'ID'
55 15
                    : StringHumanizer::humanize(str_replace(['_id', '-', '_'], ['', ' ', ' '], $id))
56
            );
57
        }
58 15
    }
59
60
    /**
61
     * Set element ID.
62
     *
63
     * @param $id
64
     *
65
     * @return $this
66
     */
67 15
    public function setId($id)
68
    {
69 15
        $this->id = $this->buildId($id);
70
71 15
        return $this;
72
    }
73
74
    /**
75
     * Set element title.
76
     *
77
     * @param $title
78
     *
79
     * @return $this
80
     */
81 15
    public function setTitle($title)
82
    {
83
        // handle relations-style titles
84 15
        $title = preg_replace('~^(\w+)\.(\w+)$~si', '$1 $2', $title);
85
86 15
        $this->title = $title;
87
88 15
        return $this;
89
    }
90
91
    /**
92
     * Get element id.
93
     *
94
     * @return string
95
     */
96 2
    public function id()
97
    {
98 2
        return $this->id;
99
    }
100
101
    /**
102
     * Get element title.
103
     *
104
     * @return string
105
     */
106 3
    public function title()
107
    {
108 3
        return $this->title;
109
    }
110
111
    /**
112
     * @return string
113
     */
114 15
    public function translationKey()
115
    {
116 15
        $key = sprintf('administrator::columns.%s.%s', $this->module()->url(), $this->id);
117
118 15
        if (!$this->translator()->has($key)) {
119 15
            $key = sprintf('administrator::columns.%s.%s', 'global', $this->id);
120
        }
121
122 15
        return $key;
123
    }
124
125
    public function setModule($module)
126
    {
127
        $this->module = $module;
128
129
        return $this;
130
    }
131
132
    /**
133
     * @param $id
134
     *
135
     * @return string
136
     */
137 15
    protected function buildId($id)
138
    {
139 15
        $parts = explode('.', $id);
140
        $parts = array_map(function ($part) {
141 15
            return str_slug($part, '_');
142 15
        }, $parts);
143 15
        $id = implode('.', $parts);
144
145 15
        return $id;
146
    }
147
148
    /**
149
     * @return Scaffolding
150
     */
151 15
    protected function module()
152
    {
153 15
        return $this->module ?: app('scaffold.module');
154
    }
155
}
156