Completed
Push — master ( 5dc268...6492f9 )
by Philippe
60:52
created

Text   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 65
Duplicated Lines 0 %

Test Coverage

Coverage 90.91%

Importance

Changes 0
Metric Value
eloc 19
c 0
b 0
f 0
dl 0
loc 65
rs 10
ccs 20
cts 22
cp 0.9091
wmc 10

8 Methods

Rating   Name   Duplication   Size   Complexity  
A getContent() 0 3 1
A __construct() 0 5 1
A getContext() 0 3 1
A getEncoding() 0 3 1
A mergeContext() 0 10 3
A utf8() 0 3 1
A __toString() 0 3 1
A replaceContent() 0 3 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace PhpSpellCheck;
6
7
use PhpSpellCheck\Exception\InvalidArgumentException;
8
use PhpSpellCheck\Utils\TextEncoding;
9
10
class Text implements TextInterface
11
{
12
    /**
13
     * @var string
14
     */
15
    private $content;
16
17
    /**
18
     * @var string
19
     */
20
    private $encoding;
21
22
    /**
23
     * @var array
24
     */
25
    private $context;
26
27 36
    public function __construct(string $content, string $encoding, array $context = [])
28
    {
29 36
        $this->content = $content;
30 36
        $this->encoding = $encoding;
31 36
        $this->context = $context;
32 36
    }
33
34 31
    public function getContent(): string
35
    {
36 31
        return $this->content;
37
    }
38
39 4
    public function getEncoding(): string
40
    {
41 4
        return $this->encoding;
42
    }
43
44 31
    public function getContext(): array
45
    {
46 31
        return $this->context;
47
    }
48
49 3
    public function replaceContent(string $newContent): TextInterface
50
    {
51 3
        return new self($newContent, $this->encoding, $this->context);
52 1
    }
53
54
    public function mergeContext(array $context, bool $override = true): TextInterface
55 2
    {
56 2
        if (empty($context)) {
57 2
            throw new InvalidArgumentException('Context trying to be merged is empty');
58 2
        }
59
60
        return new self(
61
            $this->getContent(),
62 28
            $this->getEncoding(),
63
            $override ? array_merge($this->getContext(), $context) : array_merge($context, $this->getContext())
64 28
        );
65
    }
66
67
    public static function utf8(string $text, array $context = [])
68
    {
69
        return new self($text, TextEncoding::UTF8, $context);
70
    }
71
72
    public function __toString()
73
    {
74
        return $this->getContent();
75
    }
76
}
77