-
-
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 #1710 from hydephp/support-automatic-navigation-pr…
…iorities-by-prefixing-a-number-in-filenames [2.x] Improve the numerical filename priority prefix feature
- Loading branch information
Showing
9 changed files
with
539 additions
and
211 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
84 changes: 0 additions & 84 deletions
84
packages/framework/src/Framework/Features/Navigation/FilenamePrefixNavigationHelper.php
This file was deleted.
Oops, something went wrong.
92 changes: 92 additions & 0 deletions
92
packages/framework/src/Framework/Features/Navigation/NumericalPageOrderingHelper.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,92 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Hyde\Framework\Features\Navigation; | ||
|
||
use Hyde\Facades\Config; | ||
use Illuminate\Support\Str; | ||
|
||
use function ltrim; | ||
use function substr; | ||
use function explode; | ||
use function implode; | ||
use function sprintf; | ||
use function array_map; | ||
use function preg_match; | ||
|
||
/** | ||
* @internal This class contains shared helper code for the framework to provide numerical page ordering. | ||
* It is not intended to be used outside the framework code internals. | ||
*/ | ||
class NumericalPageOrderingHelper | ||
{ | ||
/** @var array<string> The delimiters that are used to separate the numerical prefix from the rest of the identifier. */ | ||
protected const DELIMITERS = ['-', '_']; | ||
|
||
/** Check 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 hasNumericalPrefix(string $identifier): bool | ||
{ | ||
if (static::isIdentifierNested($identifier)) { | ||
$identifier = static::getCoreIdentifierPart($identifier); | ||
} | ||
|
||
return preg_match(sprintf('/^\d+[%s]/', implode(static::DELIMITERS)), $identifier) === 1; | ||
} | ||
|
||
/** | ||
* Splits a numbered identifier into its numerical prefix and the rest of the identifier. | ||
* | ||
* @return array{integer, string} | ||
*/ | ||
public static function splitNumericPrefix(string $identifier): array | ||
{ | ||
if (static::isIdentifierNested($identifier)) { | ||
$parentPath = static::getNestedIdentifierPrefix($identifier); | ||
$identifier = static::getCoreIdentifierPart($identifier); | ||
} | ||
|
||
$separator = static::getFirstCharacterFromIdentifier($identifier); | ||
$parts = explode($separator, $identifier, 2); | ||
|
||
$parts[0] = (int) $parts[0]; | ||
|
||
if (isset($parentPath)) { | ||
$parentPaths = explode('/', $parentPath); | ||
$parentPaths = array_map(function (string $part): string { | ||
return static::hasNumericalPrefix($part) ? static::splitNumericPrefix($part)[1] : $part; | ||
}, $parentPaths); | ||
$parentPath = implode('/', $parentPaths); | ||
|
||
$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 | ||
{ | ||
return Str::beforeLast($identifier, '/'); | ||
} | ||
|
||
protected static function getCoreIdentifierPart(string $identifier): string | ||
{ | ||
return Str::afterLast($identifier, '/'); | ||
} | ||
|
||
protected static function getFirstCharacterFromIdentifier(string $identifier): string | ||
{ | ||
return substr(ltrim($identifier, '0123456789'), 0, 1); | ||
} | ||
} |
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.