Completed
Push — master ( 8f5f7c...7faed5 )
by Basenko
04:23
created

TemplatableField   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 37
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
dl 0
loc 37
c 0
b 0
f 0
wmc 6
lcom 1
cbo 1
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A parseValue() 0 12 3
A getTemplatePath() 0 6 1
A getNestingLevel() 0 4 1
1
<?php
2
3
namespace MadWeb\Seoable\Fields;
4
5
abstract class TemplatableField extends Field
6
{
7
    /** @var string */
8
    protected $templateKey;
9
10
    public function __construct($value, $model, string $templateKey = '')
11
    {
12
        $this->templateKey = $templateKey;
13
14
        parent::__construct($value, $model);
15
    }
16
17
    protected function parseValue($value): string
18
    {
19
        $nesting_level = $this->templateKey ?: $this->getNestingLevel();
20
21
        return trans(
22
            $this->getTemplatePath(
23
                get_class($this->model).
24
                ($nesting_level ? '.'.$nesting_level : '')
25
            ),
26
            $this->parseAttributesWithKeys($value)
27
        );
28
    }
29
30
    protected function getTemplatePath(string $templateKey): string
31
    {
32
        return config('seoable.templates_path').
33
            '.'.$templateKey.'.'.
34
            mb_strtolower(class_basename($this));
35
    }
36
37
    protected function getNestingLevel(): string
38
    {
39
        return '';
40
    }
41
}
42