-
-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1709 from hydephp/support-automatic-navigation-pr…
…iorities-by-prefixing-a-number-in-filenames [2.x] Support automatic navigation priorities by prefixing a number in filenames
- Loading branch information
Showing
9 changed files
with
651 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
84 changes: 84 additions & 0 deletions
84
packages/framework/src/Framework/Features/Navigation/FilenamePrefixNavigationHelper.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Hyde\Framework\Features\Navigation; | ||
|
||
use Hyde\Facades\Config; | ||
use Illuminate\Support\Str; | ||
|
||
use function assert; | ||
use function explode; | ||
use function preg_match; | ||
|
||
/** | ||
* @internal Helper class for the new Filename Prefix Navigation feature. | ||
* | ||
* @experimental The code herein may be moved to more appropriate locations in the future. | ||
*/ | ||
class FilenamePrefixNavigationHelper | ||
{ | ||
/** | ||
* Determines if the feature is enabled. | ||
*/ | ||
public static function enabled(): bool | ||
{ | ||
return Config::getBool('hyde.numerical_page_ordering', true); | ||
} | ||
|
||
/** | ||
* Determines if a given identifier has a numerical prefix. | ||
*/ | ||
public static function isIdentifierNumbered(string $identifier): bool | ||
{ | ||
if (self::isIdentifierNested($identifier)) { | ||
$identifier = self::getCoreIdentifierPart($identifier); | ||
} | ||
|
||
return preg_match('/^\d+-/', $identifier) === 1; | ||
} | ||
|
||
/** | ||
* Splits a numbered identifier into its numerical prefix and the rest of the identifier. | ||
* | ||
* @return array{integer, string} | ||
*/ | ||
public static function splitNumberAndIdentifier(string $identifier): array | ||
{ | ||
if (self::isIdentifierNested($identifier)) { | ||
$parentPath = self::getNestedIdentifierPrefix($identifier); | ||
$identifier = self::getCoreIdentifierPart($identifier); | ||
} | ||
|
||
assert(self::isIdentifierNumbered($identifier)); | ||
|
||
$parts = explode('-', $identifier, 2); | ||
|
||
$parts[0] = (int) $parts[0]; | ||
|
||
if (isset($parentPath)) { | ||
$parts[1] = $parentPath.'/'.$parts[1]; | ||
} | ||
|
||
return $parts; | ||
} | ||
|
||
protected static function isIdentifierNested(string $identifier): bool | ||
{ | ||
return str_contains($identifier, '/'); | ||
} | ||
|
||
protected static function getNestedIdentifierPrefix(string $identifier): string | ||
{ | ||
assert(self::isIdentifierNested($identifier)); | ||
|
||
return Str::beforeLast($identifier, '/'); | ||
} | ||
|
||
protected static function getCoreIdentifierPart(string $identifier): string | ||
{ | ||
assert(self::isIdentifierNested($identifier)); | ||
|
||
return Str::afterLast($identifier, '/'); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.