TranslatorBladeProvider::stripParentheses()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 3
dl 0
loc 6
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 1
1
<?php
2
3
/**
4
 * Provider
5
 */
6
namespace Hokan22\LaravelTranslator\Provider;
7
8
9
use Illuminate\Support\Facades\Blade;
10
use Illuminate\Support\ServiceProvider;
11
use Illuminate\Support\Str;
12
13
/**
14
 * Class TranslatorBladeProvider
15
 *
16
 * @package  Hokan22\LaravelTranslator\Provider
17
 * @author   Alexander Viertel <[email protected]>
18
 * @license  http://opensource.org/licenses/MIT MIT
19
 */
20
class TranslatorBladeProvider extends ServiceProvider
21
{
22
    /**
23
     * Bootstrap any application services.
24
     */
25
    public function boot() {
26
        Blade::directive('translate', function($expression) {
27
28
                $expression = $this->stripParentheses($expression);
29
30
                // Call the TranslatorFacade to translate the string
31
                return "<?php echo Hokan22\\LaravelTranslator\\TranslatorFacade::translate({$expression}); ?>";
32
            }
33
        );
34
35
        Blade::directive('t', function($expression) {
36
37
                $expression = $this->stripParentheses($expression);
38
39
                // Call the TranslatorFacade to translate the string
40
                return "<?php echo Hokan22\\LaravelTranslator\\TranslatorFacade::translate({$expression}); ?>";
41
            }
42
        );
43
    }
44
45
    /**
46
     * Strip the parentheses from the given expression.
47
     *
48
     * @param string $expression
49
     * @return string
50
     */
51
    public function stripParentheses($expression) {
52
        if (Str::startsWith($expression, '(')) {
53
            $expression = substr($expression, 1, -1);
54
        }
55
56
        return $expression;
57
    }
58
}
59