Skip to content

Commit

Permalink
fix id generation
Browse files Browse the repository at this point in the history
  • Loading branch information
ibelar committed Jun 16, 2024
1 parent 835eb3e commit 6b478bb
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 13 deletions.
9 changes: 2 additions & 7 deletions src/Core/Utils.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,21 +16,16 @@ class Utils
* Thanks to Roy Tanck
* https://roytanck.com/2021/10/17/generating-short-hashes-in-php/.
*/
public static function generateId(string $longName, string $keep = '', int $length = 10): string
public static function generateId(string $longName, string $leftPad = 'f-', int $length = 10): string
{
if ($keep) {
$longName = str_replace($keep, '', $longName);
$keep = '-' . $keep;
}
// Create a raw binary sha256 hash and base64 encode it.
$hashBase64 = base64_encode(hash('sha256', $longName, true));
// Replace non-urlsafe chars to make the string urlsafe.
$hashUrlsafe = strtr($hashBase64, '+/', '__');
// Trim base64 padding characters from the end.
$hashUrlsafe = rtrim($hashUrlsafe, '=');

// Shorten the string before returning.
return substr($hashUrlsafe, 0, $length) . $keep;
return $leftPad . substr($hashUrlsafe, 0, $length);
}

public static function hasValidOptions(array $options, array $validKeys): bool
Expand Down
4 changes: 2 additions & 2 deletions src/Service/Ui.php
Original file line number Diff line number Diff line change
Expand Up @@ -326,9 +326,9 @@ public function factoryViewName(string $className): string
return Utils::getFromClassName($className);
}

public function factoryId(string $viewName, string $keep = '', int $lenght = 10): string
public function factoryId(string $viewName, string $leftPad = 'f-', int $lenght = 10): string
{
return Utils::generateId($viewName, $keep, $lenght);
return Utils::generateId($viewName, $leftPad, $lenght);
}

public function sanitize(string $html): string
Expand Down
8 changes: 4 additions & 4 deletions tests/Service/UiServiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@ public function testGenerateId(): void
$longName = 'fonh_view_view_view_view_view';
$shortName = 'fohn';

$this->assertSame('uig8quzkuK', Ui::service()->factoryId($longName));
$this->assertSame('mAp86sLodh', Ui::service()->factoryId($shortName));
$this->assertSame('uig8quzkuK-fohn', Ui::service()->factoryId($longName, 'fohn'));
$this->assertSame('dEZ4PCB4Gc-fo', Ui::service()->factoryId($shortName, 'fo'));
$this->assertSame('uig8quzkuK', Ui::service()->factoryId($longName, ''));
$this->assertSame('mAp86sLodh', Ui::service()->factoryId($shortName, ''));
$this->assertSame('fohn-uig8quzkuK', Ui::service()->factoryId($longName, 'fohn-'));
$this->assertSame('fo-mAp86sLodh', Ui::service()->factoryId($shortName, 'fo-'));

$this->assertSame(10, strlen(Ui::service()->factoryId($longName, '', 10)));
$this->assertSame(20, strlen(Ui::service()->factoryId($longName, '', 20)));
Expand Down

0 comments on commit 6b478bb

Please sign in to comment.