From 58fe768a210f7c9f8893437874a286fa497810df Mon Sep 17 00:00:00 2001 From: Anton Smirnov Date: Sat, 6 Nov 2021 02:04:17 +0200 Subject: [PATCH] PathUtils --- src/PathUtils.php | 66 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 66 insertions(+) create mode 100644 src/PathUtils.php diff --git a/src/PathUtils.php b/src/PathUtils.php new file mode 100644 index 0000000..ea2ea85 --- /dev/null +++ b/src/PathUtils.php @@ -0,0 +1,66 @@ +resolveRelative($relativePath)->toString(); + } elseif ($relativePath instanceof AbsolutePathInterface) { + return $relativePath->toString(); + } + + throw new \InvalidArgumentException('relativePath must be a string or an instance of PathInterface'); + } + + /** + * @param string|AbsolutePathInterface $basePath + * @param string|AbsolutePathInterface $targetPath + * @return string + */ + public static function makeRelativePath($basePath, $targetPath): string + { + if (\is_string($basePath)) { + $basePath = PathFactory::parse($basePath); + } + + if (\is_string($targetPath)) { + $targetPath = PathFactory::parse($targetPath); + } + + if (!($basePath instanceof AbsolutePathInterface) || !$basePath->isAbsolute()) { + throw new \InvalidArgumentException( + 'basePath must be a string containing absolute path or an instance of AbsolutePathInterface' + ); + } + + if (!($targetPath instanceof AbsolutePathInterface) || !$targetPath->isAbsolute()) { + throw new \InvalidArgumentException( + 'targetPath must be a string containing absolute path or an instance of AbsolutePathInterface' + ); + } + + return $basePath->makeRelative($targetPath)->toString(); + } +}