Completed
Pull Request — master (#34)
by Théo
02:20
created

MapFile::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 1
eloc 1
nc 1
nop 1
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the box project.
7
 *
8
 * (c) Kevin Herrera <[email protected]>
9
 *     Théo Fidry <[email protected]>
10
 *
11
 * This source file is subject to the MIT license that is bundled
12
 * with this source code in the file LICENSE.
13
 */
14
15
namespace KevinGH\Box;
16
17
/**
18
 * @internal
19
 */
20
final class MapFile
21
{
22
    private $map;
23
24
    /**
25
     * @param string[][] $map
26
     */
27
    public function __construct(array $map)
28
    {
29
        $this->map = $map;
30
    }
31
32
    public function __invoke(string $path): ?string
33
    {
34
        foreach ($this->map as $item) {
35
            foreach ($item as $match => $replace) {
36
                if ('' === $match) {
37
                    return $replace.$path;
38
                }
39
40
                if (0 === strpos($path, $match)) {
41
                    return preg_replace(
42
                        '/^'.preg_quote($match, '/').'/',
43
                        $replace,
44
                        $path
45
                    );
46
                }
47
            }
48
        }
49
50
        return null;
51
    }
52
53
    public function getMap(): array
54
    {
55
        return $this->map;
56
    }
57
}
58