Passed
Push — main ( bf0a36...0aa4d0 )
by Alex
11:31
created

Translate::lang()   A

Complexity

Conditions 4
Paths 8

Size

Total Lines 14
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 4
eloc 9
c 2
b 0
f 0
nc 8
nop 1
dl 0
loc 14
rs 9.9666
1
<?php
2
3
namespace Erykai\Translate;
4
5
/**
6
 * Class translate remote all languages
7
 */
8
class Translate extends Resource
9
{
10
    /**
11
     * @param object $data
12
     * @return $this
13
     */
14
    public function data(object $data): static
15
    {
16
        if(!isset($data->dynamic)){
17
            $data->dynamic = "";
18
        }
19
        $this->setDynamic($data->dynamic);
20
        $data->text = str_replace($this->getDynamic(),"<#>", $data->text);
21
22
        $this->setData($data);
23
        return $this;
24
    }
25
26
    /**
27
     * @param ?string $lang
28
     * @return $this
29
     * detect language if not declare in target or const TRANSLATE_DEFAULT
30
     */
31
    public function target(?string $lang = null): static
32
    {
33
        $this->lang($lang);
34
        $this->dir();
35
        $this->file();
36
        $this->setResponse();
37
        return $this;
38
    }
39
40
    /**
41
     * @param string|null $lang
42
     * @return string
43
     */
44
    public function lang(?string $lang = null): string
45
    {
46
        $this->setTarget('en');
47
        if (!empty($_SERVER['HTTP_ACCEPT_LANGUAGE'])) {
48
            [$l] = explode(",", $_SERVER['HTTP_ACCEPT_LANGUAGE']);
49
            $this->setTarget($l);
50
        }
51
        if(TRANSLATE_DEFAULT){
52
            $this->setTarget(TRANSLATE_DEFAULT);
53
        }
54
        if ($lang) {
55
            $this->setTarget($lang);
56
        }
57
        return $this->getTarget();
58
    }
59
60
    /**
61
     * @return object
62
     */
63
    public function response(): object
64
    {
65
        return $this->getResponse();
66
    }
67
68
}