Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
96.88% covered (success)
96.88%
31 / 32
50.00% covered (danger)
50.00%
1 / 2
CRAP
0.00% covered (danger)
0.00%
0 / 1
StripFsRootPathNormalizer
96.88% covered (success)
96.88%
31 / 32
50.00% covered (danger)
50.00%
1 / 2
5
0.00% covered (danger)
0.00%
0 / 1
 __construct
80.00% covered (warning)
80.00%
4 / 5
0.00% covered (danger)
0.00%
0 / 1
3.07
 normalizePath
100.00% covered (success)
100.00%
27 / 27
100.00% covered (success)
100.00%
1 / 1
2
1<?php
2
3namespace BrianHenryIE\Strauss\Helpers;
4
5use League\Flysystem\PathNormalizer;
6use League\Flysystem\WhitespacePathNormalizer;
7
8class StripFsRootPathNormalizer implements PathNormalizer
9{
10    /**
11     * @var string[]|null
12     */
13    private ?array $fsRoots;
14
15    private ?PathNormalizer $delegateNormalizer;
16
17    /**
18     * @param string|string[]|null $fsRoots
19     */
20    public function __construct(
21        $fsRoots = null,
22        ?PathNormalizer $delegateNormalizer = null
23    ) {
24        $this->fsRoots = is_string($fsRoots)
25            ? [ $fsRoots ]
26            : $fsRoots;
27        $this->delegateNormalizer = $delegateNormalizer
28            ?: new WhitespacePathNormalizer();
29    }
30
31    public function normalizePath(string $path): string
32    {
33
34        $fsRoots = array_unique(
35            $this->fsRoots ??
36                  [
37                      FileSystem::getFsRoot(),
38                      FileSystem::normalizeDirSeparator(FileSystem::getFsRoot()),
39                      'c:\\',
40                      'c:/',
41                   ]
42        );
43
44        $pattern = '^(' . implode(
45            '|',
46            array_map(
47                fn($str) => str_replace(
48                    '\\',
49                    '\\\\',
50                    str_replace(
51                        '\/',
52                        '\\\/',
53                        $str
54                    )
55                ),
56                $fsRoots
57            )
58        ) . ')';
59        $path   = preg_replace("#" . $pattern . "#i", '', $path);
60
61        if ($this->delegateNormalizer !== null) {
62            $path = $this->delegateNormalizer->normalizePath($path);
63        }
64
65        return $path;
66    }
67}