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 | use Symfony\Component\Filesystem\Path; |
||
18 | use function preg_quote; |
||
19 | use function preg_replace; |
||
20 | |||
21 | /** |
||
22 | * @internal |
||
23 | * |
||
24 | * @private |
||
25 | */ |
||
26 | final readonly class MapFile |
||
0 ignored issues
–
show
Bug
introduced
by
![]() |
|||
27 | { |
||
28 | /** |
||
29 | * @param string[][] $map |
||
30 | */ |
||
31 | public function __construct( |
||
32 | // Cannot have readonly properties: requires to be serializable |
||
33 | private string $basePath, |
||
34 | private array $map, |
||
35 | ) { |
||
36 | } |
||
37 | |||
38 | public function __invoke(string $path): ?string |
||
39 | { |
||
40 | $relativePath = Path::makeRelative($path, $this->basePath); |
||
41 | |||
42 | foreach ($this->map as $item) { |
||
43 | foreach ($item as $match => $replace) { |
||
44 | if ('' === $match) { |
||
45 | return $replace.'/'.$relativePath; |
||
46 | } |
||
47 | |||
48 | if (str_starts_with($relativePath, $match)) { |
||
49 | return preg_replace( |
||
50 | '/^'.preg_quote($match, '/').'/', |
||
51 | $replace, |
||
52 | $relativePath, |
||
53 | ); |
||
54 | } |
||
55 | } |
||
56 | } |
||
57 | |||
58 | return $relativePath; |
||
59 | } |
||
60 | |||
61 | /** |
||
62 | * @return string[][] $map |
||
63 | */ |
||
64 | public function getMap(): array |
||
65 | { |
||
66 | return $this->map; |
||
67 | } |
||
68 | } |
||
69 |