<?php
namespace MockingMagician\Organic\Helper;
class Path
{
public static function getAbsolute(string $path): string
{
$path = mb_ereg_replace('\\\\|/', DIRECTORY_SEPARATOR, $path, 'msr');
$startWithSeparator = $path[0] === DIRECTORY_SEPARATOR;
preg_match('/^[a-z]:/', $path, $matches);
$startWithLetterDir = isset($matches[0]) ? $matches[0] : false;
$subPaths = array_filter(explode(DIRECTORY_SEPARATOR, $path), 'mb_strlen');
$absolutes = [];
foreach ($subPaths as $subPath) {
if ('.' === $subPath) {
continue;
}
if ('..' === $subPath
&& !$startWithSeparator
&& !$startWithLetterDir
&& empty(array_filter($absolutes, function ($value) { return !('..' === $value); }))
) {
$absolutes[] = $subPath;
continue;
}
if ('..' === $subPath) {
array_pop($absolutes);
continue;
}
$absolutes[] = $subPath;
}
return
(($startWithSeparator ? DIRECTORY_SEPARATOR : $startWithLetterDir) ?
$startWithLetterDir.DIRECTORY_SEPARATOR : ''
).implode(DIRECTORY_SEPARATOR, $absolutes);
}
}