BindTranslationsToJs   A
last analyzed

Complexity

Total Complexity 12

Size/Duplication

Total Lines 101
Duplicated Lines 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
wmc 12
eloc 43
c 3
b 0
f 0
dl 0
loc 101
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 9 2
A put() 0 15 5
A get() 0 24 5
1
<?php
2
3
namespace Sirgrimorum\JSLocalization;
4
5
use Illuminate\Events\Dispatcher;
6
use Illuminate\Filesystem\Filesystem;
7
use Illuminate\Support\Facades\App;
8
use Illuminate\Support\Str;
9
10
class BindTranslationsToJs {
11
12
    /**
13
     * @var Dispatcher
14
     */
15
    private $event;
16
17
    /**
18
     * @var string
19
     */
20
    private $viewToBind;
21
22
    /**
23
     * @var string
24
     */
25
    private $group;
26
27
    /**
28
     * @var string
29
     */
30
    private $app;
31
32
    /**
33
     * @var string
34
     */
35
    private $basevar;
36
37
    /**
38
     * @param Dispatcher $event
39
     * @param $viewToBindVariables
40
     */
41
    function __construct($app, $viewToBind, $group, $basevar) {
42
        $this->event = $app['events'];
43
        $this->viewToBind = $viewToBind;
44
        $this->app = $app;
45
        $this->basevar = $basevar;
46
        if ($group == "") {
47
            $this->group = "";
48
        } else {
49
            $this->group = $group;
50
        }
51
        //echo "<p>Construct</p>";
52
        //echo "<pre>" . print_r(["viewToBind"=>$this->viewToBind,"group"=>$this->group,"basevar"=>$this->basevar] , true) . "</pre>";
53
    }
54
55
    /**
56
     * Return the  JavaScript 
57
     * 
58
     *
59
     * @param $langfile String The language file to load
60
     * @param $group String The key in the file to load, use . for nesting
61
     * @param $basevar String The variable to put de data in javascript
62
     */
63
    public static function get($langfile, $group = "", $basevar = "") {
64
        if ($basevar == "") {
65
            $basevar = config('sirgrimorum.jslocalization.default_base_var', 'translations');
66
        }
67
        if ($group == "") {
68
            $group = config('sirgrimorum.jslocalization.trans_group', 'messages');
69
        }
70
        $lang = App::getLocale();
71
        $file = new Filesystem();
72
        $langPath = config("sirgrimorum.jslocalization.default_lang_path", 'resources/lang');
73
        $transP = $file->getRequire(base_path() . Str::start(Str::finish($langPath, '/'), '/') . $lang . '/' . $langfile . '.php');
74
        if ($group != "") {
75
            $trans = $transP[$group];
76
        } else {
77
            $trans = $transP;
78
        }
79
        //$translator = new Translator();
80
        //$trans = $translator->get($langfile . $this->group);
81
        if (is_array($trans)) {
82
            $jsarray = json_encode($trans);
83
        } else {
84
            $jsarray = $trans;
85
        }
86
        return "<script>window.{$basevar} = window.{$basevar} || {};{$basevar}.{$langfile} = {$jsarray};</script>";
87
    }
88
89
    /**
90
     * Puth a model in a variable in JavaScript 
91
     * 
92
     *
93
     * @param Model $model The object to load
0 ignored issues
show
Bug introduced by
The type Sirgrimorum\JSLocalization\Model was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
94
     * @param $variable string The name of the variable, if empty, get the object basename
95
     */
96
    public static function put($model, $variable = "") {
97
        if (is_object($model)) {
98
            if ($variable == "") {
99
                $variable = class_basename(get_class($model));
100
            }
101
            $jsarray = $model->toJson();
102
            return "<script>{$variable} = {$jsarray};</script>";
103
        }elseif (is_array($model)) {
104
            if ($variable == "") {
105
                $variable = "varArray";
106
            }
107
            $jsarray = json_encode($model);
108
            return "<script>{$variable} = {$jsarray};</script>";
109
        }else{
110
            return "<script>{$variable} = '';</script>";
111
        }
112
    }
113
114
}
115