Passed
Pull Request — master (#42)
by
unknown
12:48
created

InMemoryCache::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 4
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace PhpMyAdmin\MoTranslator\Cache;
6
7
use PhpMyAdmin\MoTranslator\MoParser;
8
9
use function array_key_exists;
10
11
final class InMemoryCache implements CacheInterface, GetAllInterface
12
{
13
    /** @var array<string, string> */
14
    private $cache;
15
16
    public function __construct(MoParser $parser)
17
    {
18
        $this->cache = [];
19
        $parser->parseIntoCache($this);
20
    }
21
22
    public function get(string $msgid): string
23
    {
24
        return array_key_exists($msgid, $this->cache) ? $this->cache[$msgid] : $msgid;
25
    }
26
27
    public function set(string $msgid, string $msgstr): void
28
    {
29
        $this->cache[$msgid] = $msgstr;
30
    }
31
32
    public function has(string $msgid): bool
33
    {
34
        return array_key_exists($msgid, $this->cache);
35
    }
36
37
    public function setAll(array $translations): void
38
    {
39
        $this->cache = $translations;
40
    }
41
42
    /**
43
     * @inheritDoc
44
     */
45
    public function getAll(): array
46
    {
47
        return $this->cache;
48
    }
49
}
50