Translatable::name()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 1 Features 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
ccs 0
cts 1
cp 0
crap 2
rs 10
c 1
b 1
f 0
1
<?php
2
3
namespace Terranet\Administrator\Field;
4
5
use Illuminate\Support\Facades\View;
6
use Illuminate\Support\Str;
7
use Terranet\Administrator\Scaffolding;
8
use Terranet\Localizer\Locale;
9
10
/**
11
 * Class Translatable.
12
 *
13
 * @method switchTo(string $className)
14
 * @method tinymce()
15
 * @method ckeditor()
16
 * @method markdown()
17
 * @method medium()
18
 * @method hideLabel()
19
 * @method sortable(\Closure $callback = null)
20
 * @method disableSorting()
21
 */
22
class Translatable
23
{
24
    /** @var Field */
25
    protected $field;
26
27
    /**
28
     * Translatable constructor.
29
     *
30
     * @param Field $field
31
     */
32
    protected function __construct(Field $field)
33
    {
34
        $this->field = $field;
35
    }
36
37
    /**
38
     * Proxy field methods calls.
39
     *
40
     * @param $method
41
     * @param $args
42
     *
43
     * @return mixed
44
     */
45
    public function __call($method, $args)
46
    {
47
        if (\in_array($method, Textarea::KNOWN_EDITORS, true)) {
48
            return new static($this->field->$method());
49
        }
50
51
        if (method_exists($this->field, $method)) {
52
            return \call_user_func_array([$this->field, $method], $args);
53
        }
54
    }
55
56
    /**
57
     * @param Field $field
58
     *
59
     * @return Translatable
60
     */
61
    public static function make(Field $field)
62
    {
63
        return new static($field);
64
    }
65
66
    /**
67
     * @param string $page
68
     *
69
     * @return mixed
70
     */
71
    public function render(string $page = 'index')
72
    {
73
        if (\in_array($page, [Scaffolding::PAGE_INDEX, Scaffolding::PAGE_VIEW], true)) {
74
            if ($this->field->hasCustomFormat()) {
75
                return $this->field->callFormatter($this->field->getModel(), $page);
76
            }
77
78
            if ($presenter = $this->field->hasPresenter($this->field->getModel(), $this->field->id())) {
79
                return $this->field->callPresenter($presenter);
80
            }
81
        }
82
83
        $data = [
84
            'field' => $this->field,
85
            'model' => $this->field->getModel(),
86
        ];
87
88
        if (method_exists($this, $dataGetter = 'on'.Str::title($page))) {
89
            $data += \call_user_func([$this, $dataGetter]);
90
        }
91
92
        return View::make('administrator::fields.translatable.'.$page, $data);
93
    }
94
95
    /**
96
     * @param Locale $language
97
     *
98
     * @return string
99
     */
100
    public function name(Locale $language)
101
    {
102
        return "translatable[{$language->id()}][{$this->field->id()}]";
103
    }
104
105
    /**
106
     * @param Locale $language
107
     *
108
     * @return null|mixed
109
     */
110
    public function value(Locale $language)
111
    {
112
        $model = $this->field->getModel();
113
        $entity = $model->translate($language->id());
114
115
        return $entity ? $entity->getAttribute($this->field->id()) : null;
116
    }
117
118
    /**
119
     * @return array
120
     */
121
    protected function onEdit(): array
122
    {
123
        return [
124
            'languages' => \localizer\locales(),
125
            'locale' => \localizer\locale(),
126
            'container' => $this,
127
            'translations' => app('scaffold.translations'),
0 ignored issues
show
Bug introduced by
The function app was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

127
            'translations' => /** @scrutinizer ignore-call */ app('scaffold.translations'),
Loading history...
128
        ];
129
    }
130
}
131