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

Translations   A

Complexity

Total Complexity 20

Size/Duplication

Total Lines 106
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 3

Importance

Changes 0
Metric Value
dl 0
loc 106
rs 10
c 0
b 0
f 0
wmc 20
lcom 2
cbo 3

13 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 8 2
A __clone() 0 8 2
A getIterator() 0 4 1
A count() 0 4 1
A getHeaders() 0 4 1
A add() 0 8 1
A remove() 0 10 2
A setDomain() 0 6 1
A getDomain() 0 4 1
A setLanguage() 0 14 2
A getLanguage() 0 4 1
A find() 0 10 4
A toArray() 0 4 1
1
<?php
2
3
namespace Gettext;
4
5
use Gettext\Languages\Language;
6
use BadMethodCallException;
7
use InvalidArgumentException;
8
use IteratorAggregate;
9
use ArrayIterator;
10
use Countable;
11
12
/**
13
 * Class to manage a collection of translations under the same domain.
14
 */
15
class Translations implements Countable, IteratorAggregate
16
{
17
    protected $translations = [];
18
    protected $headers;
19
20
    public function __construct(string $domain = null)
21
    {
22
        $this->headers = new Headers();
23
24
        if (isset($domain)) {
25
            $this->setDomain($domain);
26
        }
27
    }
28
29
    public function __clone()
30
    {
31
        foreach ($this->translations as $id => $translation) {
32
            $this->translations[$id] = clone $translation;
33
        }
34
35
        $this->headers = clone $this->headers;
36
    }
37
38
    public function getIterator()
39
    {
40
        return new ArrayIterator($this->toArray());
41
    }
42
43
    public function count(): int
44
    {
45
        return count($this->translations);
46
    }
47
48
    public function getHeaders(): Headers
49
    {
50
        return $this->headers;
51
    }
52
53
    public function add(Translation $translation): self
54
    {
55
        $id = $translation->getId();
56
57
        $this->translations[$id] = $translation;
58
59
        return $this;
60
    }
61
62
    public function remove(Translation $translation): self
63
    {
64
        $key = array_search($translation, $this->translations);
65
66
        if ($key !== false) {
67
            unset($this->translations[$key]);
68
        }
69
70
        return $this;
71
    }
72
73
    public function setDomain(string $domain): self
74
    {
75
        $this->getHeaders()->setDomain($domain);
76
77
        return $this;
78
    }
79
80
    public function getDomain(): ?string
81
    {
82
        return $this->getHeaders()->getDomain();
83
    }
84
85
    public function setLanguage(string $language): self
86
    {
87
        $info = Language::getById($language);
88
89
        if (empty($info)) {
90
            throw new InvalidArgumentException(sprintf('The language "%s" is not valid', $language));
91
        }
92
93
        $this->getHeaders()
94
            ->setLanguage($language)
95
            ->setPluralForms(count($info->categories), $info->formula);
96
97
        return $this;
98
    }
99
100
    public function getLanguage(): ?string
101
    {
102
        return $this->getHeaders()->getLanguage();
103
    }
104
105
    public function find(?string $context, string $original): ?Translation
106
    {
107
        foreach ($this->translations as $translation) {
108
            if ($translation->getContext() === $context && $translation->getOriginal() === $original) {
109
                return $translation;
110
            }
111
        }
112
113
        return null;
114
    }
115
116
    public function toArray(): array
117
    {
118
        return array_values($this->translations);
119
    }
120
}
121