Passed
Push — master ( c780f6...60df12 )
by Andres
02:52
created

BindTranslationsToJs::put()   B

Complexity

Conditions 5
Paths 5

Size

Total Lines 14
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 14
rs 8.8571
c 0
b 0
f 0
cc 5
eloc 11
nc 5
nop 2
1
<?php
2
3
namespace Sirgrimorum\JSLocalization;
4
5
use Illuminate\Events\Dispatcher;
6
use Illuminate\Filesystem\Filesystem;
7
use Illuminate\Support\Facades\Event;
8
use Exception;
9
use App;
10
11
class BindTranslationsToJs {
12
13
    /**
14
     * @var Dispatcher
15
     */
16
    private $event;
17
18
    /**
19
     * @var string
20
     */
21
    private $viewToBind;
22
23
    /**
24
     * @var string
25
     */
26
    private $group;
27
28
    /**
29
     * @var string
30
     */
31
    private $app;
32
33
    /**
34
     * @var string
35
     */
36
    private $basevar;
37
38
    /**
39
     * @param Dispatcher $event
40
     * @param $viewToBindVariables
41
     */
42
    function __construct($app, $viewToBind, $group, $basevar) {
43
        $this->event = $app['events'];
44
        $this->viewToBind = $viewToBind;
45
        $this->app = $app;
46
        $this->basevar = $basevar;
47
        if ($group == "") {
48
            $this->group = "";
49
        } else {
50
            $this->group = $group;
51
        }
52
        //echo "<p>Construct</p>";
53
        //echo "<pre>" . print_r(["viewToBind"=>$this->viewToBind,"group"=>$this->group,"basevar"=>$this->basevar] , true) . "</pre>";
54
    }
55
56
    /**
57
     * Return the  JavaScript 
58
     * 
59
     *
60
     * @param $langfile String The language file to load
61
     * @param $group String The key in the file to load, use . for nesting
62
     * @param $basevar String The variable to put de data in javascript
63
     */
64
    public static function get($langfile, $group = "", $basevar = "") {
65
        if ($basevar == "") {
66
            $basevar = config('sirgrimorum.jslocalization.default_base_var', 'translations');
67
        }
68
        if ($group == "") {
69
            $group = config('sirgrimorum.jslocalization.trans_group', 'messages');
70
        }
71
        $lang = App::getLocale();
72
        $file = new Filesystem();
73
        $langPath = config("sirgrimorum.jslocalization.default_lang_path", 'resources/lang');
74
        $transP = $file->getRequire(base_path() . str_start(str_finish($langPath, '/'), '/') . $lang . '/' . $langfile . '.php');
75
        if ($group != "") {
76
            $trans = $transP[$group];
77
        } else {
78
            $trans = $transP;
79
        }
80
        //$translator = new Translator();
81
        //$trans = $translator->get($langfile . $this->group);
82
        if (is_array($trans)) {
83
            $jsarray = json_encode($trans);
84
        } else {
85
            $jsarray = $trans;
86
        }
87
        return "<script>window.{$basevar} = window.{$basevar} || {};{$basevar}.{$langfile} = {$jsarray};</script>";
88
    }
89
90
    /**
91
     * Puth a model in a variable in JavaScript 
92
     * 
93
     *
94
     * @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...
95
     * @param $variable string The name of the variable, if empty, get the object basename
96
     */
97
    public static function put($model, $variable = "") {
98
        if (is_object($model)) {
99
            if ($variable == "") {
100
                $variable = class_basename(get_class($model));
101
            }
102
            $jsarray = $model->toJson();
103
            return "<script>{$variable} = {$jsarray};</script>";
104
        }elseif (is_array($model)) {
105
            if ($variable == "") {
106
                $variable = "varArray";
107
            }
108
            $jsarray = json_encode($model);
109
            return "<script>{$variable} = {$jsarray};</script>";
110
        }else{
111
            
112
        }
113
    }
114
115
}
116