Directory.php 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  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. * @readonly
  17. *
  18. * @internal
  19. */
  20. final class Directory implements DirectoryInterface
  21. {
  22. private string $directoryName;
  23. public function __construct(string $directoryName)
  24. {
  25. $this->directoryName = $directoryName;
  26. }
  27. public function getRelativePathTo(string $file): string
  28. {
  29. $file = $this->normalizePath($file);
  30. if (
  31. '' === $this->directoryName
  32. || 0 !== stripos($file, $this->directoryName.\DIRECTORY_SEPARATOR)
  33. ) {
  34. return $file;
  35. }
  36. return substr($file, \strlen($this->directoryName) + 1);
  37. }
  38. private function normalizePath(string $path): string
  39. {
  40. return str_replace(['\\', '/'], \DIRECTORY_SEPARATOR, $path);
  41. }
  42. }