SegmentedPathInterface.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. <?php
  2. /**
  3. * League.Uri (https://uri.thephpleague.com)
  4. *
  5. * (c) Ignace Nyamagana Butera <nyamsprod@gmail.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. declare(strict_types=1);
  11. namespace League\Uri\Contracts;
  12. use Countable;
  13. use Iterator;
  14. use IteratorAggregate;
  15. use League\Uri\Exceptions\SyntaxError;
  16. use Stringable;
  17. /**
  18. * @extends IteratorAggregate<string>
  19. */
  20. interface SegmentedPathInterface extends Countable, IteratorAggregate, PathInterface
  21. {
  22. /**
  23. * Returns the total number of segments in the path.
  24. */
  25. public function count(): int;
  26. /**
  27. * Iterate over the path segment.
  28. *
  29. * @return Iterator<string>
  30. */
  31. public function getIterator(): Iterator;
  32. /**
  33. * Returns parent directory's path.
  34. */
  35. public function getDirname(): string;
  36. /**
  37. * Returns the path basename.
  38. */
  39. public function getBasename(): string;
  40. /**
  41. * Returns the basename extension.
  42. */
  43. public function getExtension(): string;
  44. /**
  45. * Retrieves a single path segment.
  46. *
  47. * If the segment offset has not been set, returns null.
  48. */
  49. public function get(int $offset): ?string;
  50. /**
  51. * Returns the associated key for a specific segment.
  52. *
  53. * If a value is specified only the keys associated with
  54. * the given value will be returned
  55. *
  56. * @return array<int>
  57. */
  58. public function keys(Stringable|string|null $segment = null): array;
  59. /**
  60. * Appends a segment to the path.
  61. */
  62. public function append(Stringable|string $segment): self;
  63. /**
  64. * Extracts a slice of $length elements starting at position $offset from the host.
  65. *
  66. * This method MUST retain the state of the current instance, and return
  67. * an instance that contains the selected slice.
  68. *
  69. * If $length is null it returns all elements from $offset to the end of the Path.
  70. */
  71. public function slice(int $offset, ?int $length = null): self;
  72. /**
  73. * Prepends a segment to the path.
  74. */
  75. public function prepend(Stringable|string $segment): self;
  76. /**
  77. * Returns an instance with the modified segment.
  78. *
  79. * This method MUST retain the state of the current instance, and return
  80. * an instance that contains the new segment
  81. *
  82. * If $key is non-negative, the added segment will be the segment at $key position from the start.
  83. * If $key is negative, the added segment will be the segment at $key position from the end.
  84. *
  85. * @throws SyntaxError If the key is invalid
  86. */
  87. public function withSegment(int $key, Stringable|string $segment): self;
  88. /**
  89. * Returns an instance without the specified segment.
  90. *
  91. * This method MUST retain the state of the current instance, and return
  92. * an instance that contains the modified component
  93. *
  94. * If $key is non-negative, the removed segment will be the segment at $key position from the start.
  95. * If $key is negative, the removed segment will be the segment at $key position from the end.
  96. *
  97. * @throws SyntaxError If the key is invalid
  98. */
  99. public function withoutSegment(int ...$keys): self;
  100. /**
  101. * Returns an instance without duplicate delimiters.
  102. *
  103. * This method MUST retain the state of the current instance, and return
  104. * an instance that contains the path component normalized by removing
  105. * multiple consecutive empty segment
  106. */
  107. public function withoutEmptySegments(): self;
  108. /**
  109. * Returns an instance with the specified parent directory's path.
  110. *
  111. * This method MUST retain the state of the current instance, and return
  112. * an instance that contains the extension basename modified.
  113. */
  114. public function withDirname(Stringable|string $path): self;
  115. /**
  116. * Returns an instance with the specified basename.
  117. *
  118. * This method MUST retain the state of the current instance, and return
  119. * an instance that contains the extension basename modified.
  120. */
  121. public function withBasename(Stringable|string $basename): self;
  122. /**
  123. * Returns an instance with the specified basename extension.
  124. *
  125. * This method MUST retain the state of the current instance, and return
  126. * an instance that contains the extension basename modified.
  127. */
  128. public function withExtension(Stringable|string $extension): self;
  129. }