Passed
Push — master ( e458aa...d84cb7 )
by Carlos C
02:30 queued 11s
created

StringUncaseReplacer::findReplacement()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 3.0416

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 5
nc 3
nop 1
dl 0
loc 9
ccs 5
cts 6
cp 0.8333
crap 3.0416
rs 10
c 1
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace CfdiUtils\Internals;
6
7
/** @internal */
8
final class StringUncaseReplacer
9
{
10
    /** @var array<string, array<string, true>> */
11
    private $replacements;
12
13
    /**
14
     * @param array<string, array<string, true>> $replacements
15
     */
16 1
    private function __construct(array $replacements = [])
17
    {
18 1
        $this->replacements = $replacements;
19 1
    }
20
21
    /**
22
     * @param array<string, array<string>> $replacements
23
     * @return static
24
     */
25 1
    public static function create(array $replacements): self
26
    {
27 1
        $replacer = new self();
28 1
        foreach ($replacements as $replacement => $needles) {
29 1
            $replacer->addReplacement($replacement, ...$needles);
30
        }
31 1
        return $replacer;
32
    }
33
34 1
    private function addReplacement(string $replacement, string ...$needle)
35
    {
36 1
        $needle[] = $replacement; // also include the replacement itself
37 1
        foreach ($needle as $entry) {
38 1
            $entry = mb_strtolower($entry); // normalize url to compare
39 1
            $this->replacements[$replacement][$entry] = true;
40
        }
41 1
    }
42
43 1
    public function findReplacement(string $url): string
44
    {
45 1
        $url = mb_strtolower($url); // normalize url to compare
46 1
        foreach ($this->replacements as $replacement => $entries) {
47 1
            if (isset($entries[$url])) {
48 1
                return $replacement;
49
            }
50
        }
51
        return '';
52
    }
53
}
54