Completed
Push — master ( e600ef...7ac924 )
by Basenko
07:00
created

TemplatableField::getNestingLevel()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
crap 1
1
<?php
2
3
namespace MadWeb\Seoable\Fields;
4
5
use Illuminate\Container\Container;
6
7
abstract class TemplatableField extends Field
8
{
9
    /** @var string */
10
    protected $templateKey;
11
12
    /** @var \Illuminate\Contracts\Translation\Translator */
13
    protected $translator;
14
15 9
    public function __construct($value, $model, string $templateKey = '')
16
    {
17 9
        $this->templateKey = $templateKey;
18 9
        $this->translator = Container::getInstance()->make('translator');
19
20 9
        parent::__construct($value, $model);
21 9
    }
22
23 9
    protected function parseValue($value): string
24
    {
25 9
        $nesting_level = $this->templateKey ?: $this->getNestingLevel();
26
27 9
        $template_path = $this->getTemplatePath(
28 9
            get_class($this->model).
29 9
            ($nesting_level ? '.'.$nesting_level : '')
30
        );
31
32 9
        return $this->translator->has($template_path) ? $this->translator->trans(
33 9
            $template_path,
34 9
            $this->parseAttributesWithKeys($value)
35 9
        ) : $this->model->getAttribute($value);
36
    }
37
38 9
    protected function getTemplatePath(string $templateKey): string
39
    {
40 9
        return config('seoable.templates_path').
41 9
            '.'.$templateKey.'.'.
42 9
            mb_strtolower(class_basename($this));
43
    }
44
45 3
    protected function getNestingLevel(): string
46
    {
47 3
        return '';
48
    }
49
}
50