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

Translate   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 58
Duplicated Lines 0 %

Importance

Changes 4
Bugs 0 Features 0
Metric Value
eloc 22
c 4
b 0
f 0
dl 0
loc 58
rs 10
wmc 8

4 Methods

Rating   Name   Duplication   Size   Complexity  
A data() 0 10 2
A response() 0 3 1
A lang() 0 14 4
A target() 0 7 1
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
}