@@ -592,11 +592,13 @@ class Sqids implements SqidsInterface
592
592
593
593
protected MathInterface $ math ;
594
594
595
+ protected ?string $ blocklist = null ;
596
+
595
597
/** @throws \InvalidArgumentException */
596
598
public function __construct (
597
599
protected string $ alphabet = self ::DEFAULT_ALPHABET ,
598
600
protected int $ minLength = self ::DEFAULT_MIN_LENGTH ,
599
- protected array $ blocklist = self ::DEFAULT_BLOCKLIST ,
601
+ array $ blocklist = self ::DEFAULT_BLOCKLIST ,
600
602
) {
601
603
$ this ->math = $ this ->getMathExtension ();
602
604
@@ -628,9 +630,12 @@ public function __construct(
628
630
$ alphabetPattern = '/^[ ' . preg_quote ($ alphabet , '/ ' ) . ']+$/i ' ;
629
631
foreach ($ blocklist as $ word ) {
630
632
if (strlen ((string ) $ word ) >= 3 && preg_match ($ alphabetPattern , $ word )) {
631
- $ filteredBlocklist [] = strtolower ((string ) $ word );
633
+ $ filteredBlocklist [] = preg_quote ((string ) $ word, ' / ' );
632
634
}
633
635
}
636
+ if ($ filteredBlocklist ) {
637
+ $ this ->blocklist = '/( ' . implode ('| ' , $ filteredBlocklist ) . ')/i ' ;
638
+ }
634
639
635
640
$ this ->alphabet = $ this ->shuffle ($ alphabet );
636
641
$ this ->blocklist = $ filteredBlocklist ;
@@ -799,25 +804,7 @@ protected function toNumber(string $id, string $alphabet): int
799
804
800
805
protected function isBlockedId (string $ id ): bool
801
806
{
802
- $ id = strtolower ($ id );
803
-
804
- foreach ($ this ->blocklist as $ word ) {
805
- if (strlen ((string ) $ word ) <= strlen ($ id )) {
806
- if (strlen ($ id ) <= 3 || strlen ((string ) $ word ) <= 3 ) {
807
- if ($ id == $ word ) {
808
- return true ;
809
- }
810
- } elseif (preg_match ('/~[0-9]+~/ ' , (string ) $ word )) {
811
- if (str_starts_with ($ id , (string ) $ word ) || strrpos ($ id , (string ) $ word ) === strlen ($ id ) - strlen ((string ) $ word )) {
812
- return true ;
813
- }
814
- } elseif (str_contains ($ id , (string ) $ word )) {
815
- return true ;
816
- }
817
- }
818
- }
819
-
820
- return false ;
807
+ return $ this ->blocklist !== null && preg_match ($ this ->blocklist , $ id );
821
808
}
822
809
823
810
protected static function maxValue (): int
0 commit comments