self::MAX_LENGTH) { throw new InvalidArgumentException('Der Pfad darf höchstens '.self::MAX_LENGTH.' Zeichen lang sein.'); } return $path; } /** * @return list die Pfadteile; die Wurzel hat keine */ public static function segments(string $path): array { $trimmed = trim($path, '/'); return $trimmed === '' ? [] : explode('/', $trimmed); } public static function depth(string $path): int { return count(self::segments($path)); } /** Adresse der übergeordneten Stufe ("/" für den ersten Teil); null für die Wurzel. Ob dort eine Seite liegt, ist offen. */ public static function parent(string $path): ?string { $segments = self::segments($path); if ($segments === []) { return null; } array_pop($segments); return $segments === [] ? '/' : '/'.implode('/', $segments); } /** Ganze Pfadteile, nicht Zeichenfolgen: "/kurse-extra" liegt nicht unter "/kurse". */ public static function isDescendantOf(string $path, string $ancestor): bool { $own = self::segments($path); $other = self::segments($ancestor); return count($own) > count($other) && array_slice($own, 0, count($other)) === $other; } /** * Sortierfolge nach Pfadteilen (natürlich, also "gruppe-2" vor "gruppe-10"): Unterseiten stehen unmittelbar * hinter ihrer übergeordneten Adresse. Ein Vergleich der ganzen Zeichenkette ordnete "/kurse-extra" dazwischen. */ public static function compare(string $a, string $b): int { $left = self::segments($a); $right = self::segments($b); foreach (array_map(null, $left, $right) as [$x, $y]) { if ($x === null || $y === null) { return $x === null ? ($y === null ? 0 : -1) : 1; } if (($order = strnatcmp($x, $y)) !== 0) { return $order; } } return 0; } }