Directory.php 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. <?php
  2. declare(strict_types=1);
  3. /*
  4. * This file is part of PHP CS Fixer.
  5. *
  6. * (c) Fabien Potencier <fabien@symfony.com>
  7. * Dariusz Rumiński <dariusz.ruminski@gmail.com>
  8. *
  9. * This source file is subject to the MIT license that is bundled
  10. * with this source code in the file LICENSE.
  11. */
  12. namespace PhpCsFixer\Cache;
  13. /**
  14. * @author Dariusz Rumiński <dariusz.ruminski@gmail.com>
  15. *
  16. * @internal
  17. */
  18. final class Directory implements DirectoryInterface
  19. {
  20. private string $directoryName;
  21. public function __construct(string $directoryName)
  22. {
  23. $this->directoryName = $directoryName;
  24. }
  25. public function getRelativePathTo(string $file): string
  26. {
  27. $file = $this->normalizePath($file);
  28. if (
  29. '' === $this->directoryName
  30. || 0 !== stripos($file, $this->directoryName.\DIRECTORY_SEPARATOR)
  31. ) {
  32. return $file;
  33. }
  34. return substr($file, \strlen($this->directoryName) + 1);
  35. }
  36. private function normalizePath(string $path): string
  37. {
  38. return str_replace(['\\', '/'], \DIRECTORY_SEPARATOR, $path);
  39. }
  40. }