Completed
Push — master ( d20e27...66f42f )
by Anılcan
03:17
created

FormerHelper::getUrlByRoute()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
c 0
b 0
f 0
rs 10
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
3
namespace AnilcanCakir\Former;
4
5
use AnilcanCakir\Former\Contracts\FormerHelper as Contract;
6
use Illuminate\Contracts\Config\Repository as Config;
7
use Illuminate\Contracts\Routing\UrlGenerator as Url;
8
use Illuminate\Contracts\Translation\Translator;
9
use Illuminate\Contracts\View\Factory as View;
10
11
class FormerHelper implements Contract
12
{
13
    /**
14
     * @var View
15
     */
16
    protected $view;
17
18
    /**
19
     * @var Translator
20
     */
21
    protected $translator;
22
23
    /**
24
     * @var Config
25
     */
26
    protected $config;
27
28
    /**
29
     * @var Url
30
     */
31
    protected $url;
32
33
    public function __construct(View $view, Translator $translator, Config $config, Url $url)
34
    {
35
        $this->view = $view;
36
        $this->translator = $translator;
37
        $this->config = $config;
38
        $this->url = $url;
39
    }
40
41
    /**
42
     * Get view path.
43
     *
44
     * @param string|null $view
45
     * @param null $theme
46
     * @return string
47
     */
48
    public function getViewPath($view = null, $theme = null): string
49
    {
50
        // Update the theme variable
51
        $theme = $this->getThemeOrDefault($theme);
52
53
        $path = "former.{$theme}.{$view}";
54
        if ($this->view->exists($path)) {
55
            return $path;
56
        }
57
58
        return "former::{$theme}.{$view}";
59
    }
60
61
    /**
62
     * Get config value by given key.
63
     *
64
     * @param string $key
65
     * @return mixed
66
     */
67
    public function config($key)
68
    {
69
        return $this->config->get(
70
            "former.{$key}"
71
        );
72
    }
73
74
    /**
75
     * Get field class from rules.
76
     *
77
     * @param array $rules
78
     * @return string
79
     */
80 View Code Duplication
    public function getFieldClassFromRules(array $rules): string
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
81
    {
82
        foreach ($this->config('fields') as $input => $types) {
83
            foreach ($rules as $rule) {
84
                if (in_array($rule, $types)) {
85
                    return $input;
86
                }
87
            }
88
        }
89
90
        return $this->config('default_field');
91
    }
92
93
    /**
94
     * Get field class from type.
95
     *
96
     * @param string $type
97
     * @return string
98
     */
99 View Code Duplication
    public function getFieldClassFromType($type): string
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
100
    {
101
        foreach ($this->config('fields') as $input => $types) {
102
            if (in_array($type, $types)) {
103
                return $input;
104
            }
105
        }
106
107
        return $this->config('default_field');
108
    }
109
110
    /**
111
     * Get given theme or default theme.
112
     *
113
     * @param null|string $theme
114
     * @return string
115
     */
116
    public function getThemeOrDefault($theme = null): string
117
    {
118
        return $theme ?: $this->config('theme');
119
    }
120
121
    /**
122
     * Get the label of field by name.
123
     *
124
     * @param string $name
125
     * @return string
126
     */
127 View Code Duplication
    public function getLabel($name): string
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
128
    {
129
        $key = "validation.labels.{$name}";
130
        $trans = $this->translator->trans($key);
131
132
        return $trans == $key ? $this->translator->trans("validation.attributes.{$name}") : $trans;
133
    }
134
135
    /**
136
     * Get the placeholder of field by name.
137
     *
138
     * @param string $name
139
     * @return string|null
140
     */
141 View Code Duplication
    public function getPlaceholder($name)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
142
    {
143
        $key = "validation.placeholders.{$name}";
144
        $trans = $this->translator->trans($key);
145
146
        if ($trans != $key) {
147
            return $trans;
148
        }
149
    }
150
151
    /**
152
     * Get the text of field by name.
153
     *
154
     * @param string $name
155
     * @return string|null
156
     */
157 View Code Duplication
    public function getText($name)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
158
    {
159
        $key = "validation.texts.{$name}";
160
        $trans = $this->translator->trans($key);
161
162
        if ($trans != $key) {
163
            return $trans;
164
        }
165
    }
166
167
    /**
168
     * Get url by route name.
169
     *
170
     * @param string $route
171
     * @return string
172
     */
173
    public function getUrlByRoute($route): string
174
    {
175
        return $this->url->route($route);
176
    }
177
}
178