Completed
Push — v5-dev ( a42eb3 )
by Oscar
01:56
created

Translation   A

Complexity

Total Complexity 23

Size/Duplication

Total Lines 154
Duplicated Lines 0 %

Coupling/Cohesion

Components 4
Dependencies 3

Importance

Changes 0
Metric Value
dl 0
loc 154
rs 10
c 0
b 0
f 0
wmc 23
lcom 4
cbo 3

22 Methods

Rating   Name   Duplication   Size   Complexity  
A create() 0 10 1
A generateId() 0 4 1
A __construct() 0 9 1
A __clone() 0 7 1
A getId() 0 4 1
A getContext() 0 4 1
A withContext() 0 8 1
A getOriginal() 0 4 1
A withOriginal() 0 8 1
A setPlural() 0 6 1
A getPlural() 0 4 1
A disable() 0 6 1
A isDisabled() 0 4 1
A translate() 0 6 1
A getTranslation() 0 4 1
A isTranslated() 0 4 2
A translatePlural() 0 6 1
A getPluralTranslations() 0 4 1
A getReferences() 0 4 1
A getFlags() 0 4 1
A getComments() 0 4 1
A getExtractedComments() 0 4 1
1
<?php
2
declare(strict_types = 1);
3
4
namespace Gettext;
5
6
/**
7
 * Class to manage an individual translation
8
 */
9
class Translation
10
{
11
    protected $id;
12
    protected $context;
13
    protected $original;
14
    protected $translation;
15
    protected $plural;
16
    protected $pluralTranslations = [];
17
    protected $disabled = false;
18
    protected $references;
19
    protected $flags;
20
    protected $comments;
21
    protected $extractedComments;
22
23
    public static function create(?string $context, string $original): Translation
24
    {
25
        $id = static::generateId($context, $original);
26
27
        $translation = new static($id);
28
        $translation->context = $context;
29
        $translation->original = $original;
30
31
        return $translation;
32
    }
33
34
    protected static function generateId(?string $context, string $original): string
35
    {
36
        return "{$context}\004{$original}";
37
    }
38
39
    protected function __construct(string $id)
40
    {
41
        $this->id = $id;
42
43
        $this->references = new References();
44
        $this->flags = new Flags();
45
        $this->comments = new Comments();
46
        $this->extractedComments = new Comments();
47
    }
48
49
    public function __clone()
50
    {
51
        $this->references = clone $this->references;
52
        $this->flags = clone $this->flags;
53
        $this->comments = clone $this->comments;
54
        $this->extractedComments = clone $this->extractedComments;
55
    }
56
57
    public function getId(): string
58
    {
59
        return $this->id;
60
    }
61
62
    public function getContext(): ?string
63
    {
64
        return $this->context;
65
    }
66
67
    public function withContext(?string $context): Translation
68
    {
69
        $clone = clone $this;
70
        $clone->context = $context;
71
        $clone->id = static::generateId($clone->getContext(), $clone->getOriginal());
72
73
        return $clone;
74
    }
75
76
    public function getOriginal(): ?string
77
    {
78
        return $this->original;
79
    }
80
81
    public function withOriginal(string $original): Translation
82
    {
83
        $clone = clone $this;
84
        $clone->original = $original;
85
        $clone->id = static::generateId($clone->getContext(), $clone->getOriginal());
86
87
        return $clone;
88
    }
89
90
    public function setPlural(string $plural): self
91
    {
92
        $this->plural = $plural;
93
94
        return $this;
95
    }
96
97
    public function getPlural(): ?string
98
    {
99
        return $this->plural;
100
    }
101
102
    public function disable(bool $disabled = true): self
103
    {
104
        $this->disabled = $disabled;
105
106
        return $this;
107
    }
108
109
    public function isDisabled(): bool
110
    {
111
        return $this->disabled;
112
    }
113
114
    public function translate(string $translation): self
115
    {
116
        $this->translation = $translation;
117
118
        return $this;
119
    }
120
121
    public function getTranslation(): ?string
122
    {
123
        return $this->translation;
124
    }
125
126
    public function isTranslated(): bool
127
    {
128
        return isset($this->translation) && $this->translation !== '';
129
    }
130
131
    public function translatePlural(string ...$translations): self
132
    {
133
        $this->pluralTranslations = $translations;
134
135
        return $this;
136
    }
137
138
    public function getPluralTranslations(): array
139
    {
140
        return $this->pluralTranslations;
141
    }
142
143
    public function getReferences(): References
144
    {
145
        return $this->references;
146
    }
147
148
    public function getFlags(): Flags
149
    {
150
        return $this->flags;
151
    }
152
153
    public function getComments(): Comments
154
    {
155
        return $this->comments;
156
    }
157
158
    public function getExtractedComments(): Comments
159
    {
160
        return $this->extractedComments;
161
    }
162
}
163