diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml new file mode 100644 index 0000000..85f699c --- /dev/null +++ b/.github/workflows/tests.yml @@ -0,0 +1,39 @@ +name: PHP Tests +on: [ push, pull_request ] +jobs: + build: + + runs-on: ubuntu-latest + + strategy: + matrix: + php: [ '7.4', '8.0', '8.1' ] + + steps: + - name: Checkout + uses: actions/checkout@v2 + + - name: Setup PHP, with composer and extensions + uses: shivammathur/setup-php@v2 + with: + php-version: ${{ matrix.php }} + coverage: xdebug + + - name: Validate composer.json and composer.lock + run: composer validate + + - name: Cache Composer packages + id: composer-cache + uses: actions/cache@v2 + with: + path: vendor + key: ${{ runner.os }}-${{ matrix.php }}-${{ hashFiles('**/composer.lock') }} + restore-keys: | + ${{ runner.os }}-${{ matrix.php }}- + + - name: Install dependencies + if: steps.composer-cache.outputs.cache-hit != 'true' + run: composer install --prefer-dist --no-progress --no-suggest + + - name: Run tests + run: vendor/bin/phpunit --coverage-text diff --git a/.travis.yml b/.travis.yml deleted file mode 100644 index e8d33b1..0000000 --- a/.travis.yml +++ /dev/null @@ -1,21 +0,0 @@ -language: php - -php: - - 5.6 - - 7.0 - - 7.1 - - 7.2 - - nightly - -matrix: - allow_failures: - - php: nightly - -cache: - apt: true - bundler: true - -install: - travis_retry composer install --no-interaction --prefer-source - -script: vendor/bin/phpunit diff --git a/README.md b/README.md index eb004bc..fd53636 100644 --- a/README.md +++ b/README.md @@ -1,78 +1,79 @@ -# Shorten and obfuscate IDs - -[![Build Status](https://travis-ci.org/krowinski/tinyID.svg?branch=master)](https://travis-ci.org/krowinski/tinyID) -[![Latest Stable Version](https://poser.pugx.org/krowinski/tinyid/v/stable)](https://packagist.org/packages/krowinski/tinyid) [![Total Downloads](https://poser.pugx.org/krowinski/tinyid/downloads)](https://packagist.org/packages/krowinski/tinyid) [![Latest Unstable Version](https://poser.pugx.org/krowinski/tinyid/v/unstable)](https://packagist.org/packages/krowinski/tinyid) -[![License](https://poser.pugx.org/krowinski/tinyid/license)](https://packagist.org/packages/krowinski/tinyid) -[![Scrutinizer Code Quality](https://scrutinizer-ci.com/g/krowinski/tinyid/badges/quality-score.png?b=master)](https://scrutinizer-ci.com/g/krowinski/tinyid/?branch=master) -[![Code Coverage](https://scrutinizer-ci.com/g/krowinski/tinyid/badges/coverage.png?b=master)](https://scrutinizer-ci.com/g/krowinski/tinyid/?branch=master) - -## SYNOPSIS - -```php - use TinyID\TinyID; - - include __DIR__ . '/../vendor/autoload.php'; - - // dictionary must consist of at least two UNIQUE unicode characters. - $tinyId = new TinyID('2BjLhRduC6Tb8Q5cEk9oxnFaWUDpOlGAgwYzNre7tI4yqPvXm0KSV1fJs3ZiHM'); - - var_dump($tinyId->encode('48888851145')); // will print 1FN7Ab - var_dump($tinyId->decode('1FN7Ab')); // will print 48888851145 -``` - -## DESCRIPTION - -Using real IDs in various places - such as GET links or API payload - is generally a bad idea: - -* It may reveal some sensitive informations about your business, such as growth rate or amount of customers. -* If someone finds unprotected resource link, where you forgot to check if passed resource ID really belongs to currently logged-in user, he will be able to steal all of your data really fast just by incrementing ID in links. -* Big numbers may cause overflows in places where length is limited, such as SMS messages. - -With the help of this module you can shorten and obfuscate your IDs at the same time. - -## METHODS - -### new TidyID('qwerty') - -Key must consist of at least two ***unique*** unicode characters. - -The longer the dictionary - the shorter encoded ID. - -Encoded ID will be made exclusively out of characters from the key. -This very useful property allows to adapt your encoding to the environment. -For example in SMS messages you may restrict key to US ASCII to avoid available length reduction caused by conversion to GSM 03.38 charset. -Or if you want to use such ID as file/directory name in case insensitive filesystem you may want to use only lowercase letters in the key. - -### encode(123) - -Encode positive integer into a string. - -Note that leading `0`s are not preserved, `encode(123)` is the same as `encode(00123)`. - -Used algorithm is a base to the length of the key conversion that maps to distinct permutation of characters. -Do not consider it a strong encryption, but if you have secret and long and well shuffled key it is almost impossible to reverse-engineer real ID. - -### decode('rer') - -Decode string back into a positive integer. - -## TRICKS - -If you provide sequential characters in key you can convert your numbers to some weird numeric systems, for example base18: - -```php - var_dump((new TinyID('0123456789ABCDEFGH'))->encode(48888851145)); // '47F709HFF' -``` - -Or you can go wild just for the fun of it. - -```php - var_dump((new TinyID('😀😁😂😃😄😅😆😇😈😉😊😋😌😍😎😏😐😑😒😓😔😕😖😗😘😙😚😛😜😝😞😟😠😡😢😣😤😥😦😧😨😩😪😫😬😭😮😯😰😱😲😳😴😵😶😷😸😹😺😻😼😽😾😿'))->encode(48888851145)); // '😭😢😀😊😫😉' -``` - -## OTHER IMPLEMENTATIONS - -* [Perl 5](http://search.cpan.org/~bbkr/Integer-Tiny-0.3/lib/Integer/Tiny.pm) -* [Perl 6](https://github.com/bbkr/TinyID) - -All examples are in example dir. +# Shorten and obfuscate IDs + +[![PHP Tests](https://github.com/krowinski/tinyID/actions/workflows/tests.yml/badge.svg)](https://github.com/krowinski/tinyID/actions/workflows/tests.yml) +[![Latest Stable Version](https://poser.pugx.org/krowinski/tinyid/v/stable)](https://packagist.org/packages/krowinski/tinyid) +[![Total Downloads](https://poser.pugx.org/krowinski/tinyid/downloads)](https://packagist.org/packages/krowinski/tinyid) +[![License](https://poser.pugx.org/krowinski/tinyid/license)](https://packagist.org/packages/krowinski/tinyid) +[![Scrutinizer Code Quality](https://scrutinizer-ci.com/g/krowinski/tinyid/badges/quality-score.png?b=master)](https://scrutinizer-ci.com/g/krowinski/tinyid/?branch=master) +[![Code Coverage](https://scrutinizer-ci.com/g/krowinski/tinyid/badges/coverage.png?b=master)](https://scrutinizer-ci.com/g/krowinski/tinyid/?branch=master) + +## SYNOPSIS + +```php + use TinyID\TinyID; + + include __DIR__ . '/../vendor/autoload.php'; + + // dictionary must consist of at least two UNIQUE unicode characters. + $tinyId = new TinyID('2BjLhRduC6Tb8Q5cEk9oxnFaWUDpOlGAgwYzNre7tI4yqPvXm0KSV1fJs3ZiHM'); + + var_dump($tinyId->encode('48888851145')); // will print 1FN7Ab + var_dump($tinyId->decode('1FN7Ab')); // will print '48888851145' +``` + +## DESCRIPTION + +Using real IDs in various places - such as GET links or API payload - is generally a bad idea: + +* It may reveal some sensitive information about your business, such as growth rate or amount of customers. +* If someone finds unprotected resource link, where you forgot to check if passed resource ID really belongs to currently logged-in user, he will be able to steal all of your data really fast just by + incrementing ID in links. +* Big numbers may cause overflows in places where length is limited, such as SMS messages. + +With the help of this module you can shorten and obfuscate your IDs at the same time. + +## METHODS + +### new TidyID('qwerty') + +Key must consist of at least two ***unique*** unicode characters. + +The longer the dictionary - the shorter encoded ID. + +Encoded ID will be made exclusively out of characters from the key. This very useful property allows to adapt your encoding to the environment. For example in SMS messages you may restrict key to US +ASCII to avoid available length reduction caused by conversion to GSM 03.38 charset. Or if you want to use such ID as file/directory name in case-insensitive filesystem you may want to use only +lowercase letters in the key. + +### encode('123') + +Encode positive integer into a string. + +Note that leading `0`s are not preserved, `encode('123')` is the same as `encode('00123')`. + +Used algorithm is a base to the length of the key conversion that maps to distinct permutation of characters. Do not consider it a strong encryption, but if you have secret and long and well shuffled +key it is almost impossible to reverse-engineer real ID. + +### decode('rer') + +Decode string back into a positive integer. + +## TRICKS + +If you provide sequential characters in key you can convert your numbers to some weird numeric systems, for example base18: + +```php + var_dump((new TinyID('0123456789ABCDEFGH'))->encode('48888851145')); // '47F709HFF' +``` + +Or you can go wild just for the fun of it. + +```php + var_dump((new TinyID('😀😁😂😃😄😅😆😇😈😉😊😋😌😍😎😏😐😑😒😓😔😕😖😗😘😙😚😛😜😝😞😟😠😡😢😣😤😥😦😧😨😩😪😫😬😭😮😯😰😱😲😳😴😵😶😷😸😹😺😻😼😽😾😿'))->encode(48888851145)); // '😭😢😀😊😫😉' +``` + +## OTHER IMPLEMENTATIONS + +* [Perl 5](http://search.cpan.org/~bbkr/Integer-Tiny-0.3/lib/Integer/Tiny.pm) +* [Perl 6](https://github.com/bbkr/TinyID) + +Examples are in example dir. diff --git a/composer.json b/composer.json index f66a97a..cd27cdf 100644 --- a/composer.json +++ b/composer.json @@ -9,12 +9,12 @@ ], "type": "library", "require": { - "php": ">=5.6", - "krowinski/bcmath-extended": "^4.1", + "php": ">=7.4", + "krowinski/bcmath-extended": "^6.0", "ext-mbstring": "*" }, "require-dev": { - "phpunit/phpunit": "^5.7 || ^6.5" + "phpunit/phpunit": "^9.0" }, "license": "MIT", "authors": [ diff --git a/example/example.php b/example/example.php index 5608bb5..b613b44 100644 --- a/example/example.php +++ b/example/example.php @@ -1,15 +1,17 @@ -encode('48888851145')); // 1FN7Ab -var_dump($tinyId->decode('1FN7Ab')); // 48888851145 - -var_dump((new TinyID('0123456789ABCDEFGH'))->encode(48888851145)); // '47F709HFF' - -var_dump((new TinyID('😀😁😂😃😄😅😆😇😈😉😊😋😌😍😎😏😐😑😒😓😔😕😖😗😘😙😚😛😜😝😞😟😠😡😢😣😤😥😦😧😨😩😪😫😬😭😮😯😰😱😲😳😴😵😶😷😸😹😺😻😼😽😾😿'))->encode(48888851145)); // '😭😢😀😊😫😉' +encode('48888851145')); // 1FN7Ab +var_dump($tinyId->decode('1FN7Ab')); // 48888851145 + +var_dump((new TinyID('0123456789ABCDEFGH'))->encode('48888851145')); // '47F709HFF' + +var_dump((new TinyID('😀😁😂😃😄😅😆😇😈😉😊😋😌😍😎😏😐😑😒😓😔😕😖😗😘😙😚😛😜😝😞😟😠😡😢😣😤😥😦😧😨😩😪😫😬😭😮😯😰😱😲😳😴😵😶😷😸😹😺😻😼😽😾😿'))->encode('48888851145')); // '😭😢😀😊😫😉' diff --git a/src/TinyID/TinyID.php b/src/TinyID/TinyID.php index 4ec73e2..0819ef7 100644 --- a/src/TinyID/TinyID.php +++ b/src/TinyID/TinyID.php @@ -1,89 +1,63 @@ -dictionary = $this->stringSplit($dictionary); - $this->dictionaryLength = count(array_unique($this->dictionary)); - - if ($dictionaryLength !== $this->dictionaryLength) { - throw new \InvalidArgumentException('dictionary contains duplicated characters'); - } - } - - /** - * @param string $value - * @return array[]|false|string[] - */ - private function stringSplit($value) - { - return preg_split('//u', $value, -1, PREG_SPLIT_NO_EMPTY); - } - - /** - * @param string $value - * @return string - * @throws \InvalidArgumentException - */ - public function encode($value) - { - if (BC::COMPARE_RIGHT_GRATER === BC::comp($value, '0')) { - throw new \InvalidArgumentException('cannot encode negative number'); - } - - $encoded = ''; - do { - $encoded = $this->dictionary[BC::mod($value, $this->dictionaryLength, 0)] . $encoded; - $value = BC::div($value, $this->dictionaryLength, 0); - } while ($value); - - - return $encoded; - } - - /** - * @param string $value - * @return string - */ - public function decode($value) - { - $charsToPosition = array_flip($this->dictionary); - $out = '0'; - foreach (array_reverse($this->stringSplit($value)) as $pos => $tmp) { - if (!isset($charsToPosition[$tmp])) { - throw new \InvalidArgumentException('cannot decode string with characters not in dictionary'); - } - $out = BC::add($out, BC::mul($charsToPosition[$tmp], BC::pow($this->dictionaryLength, $pos, 0), 0), 0); - } - - return $out; - } +dictionary = $this->stringSplit($dictionary); + $this->dictionaryLength = count(array_unique($this->dictionary)); + + if ($dictionaryLength !== $this->dictionaryLength) { + throw new InvalidArgumentException('dictionary contains duplicated characters'); + } + } + + private function stringSplit(string $value): array + { + return (array)preg_split('//u', $value, -1, PREG_SPLIT_NO_EMPTY); + } + + public function encode(string $value): string + { + if (BC::COMPARE_RIGHT_GRATER === BC::comp($value, '0')) { + throw new InvalidArgumentException('cannot encode negative number'); + } + + $encoded = ''; + do { + $encoded = $this->dictionary[BC::mod($value, (string)$this->dictionaryLength, 0)] . $encoded; + $value = BC::div($value, (string)$this->dictionaryLength, 0); + } while ($value); + + return $encoded; + } + + public function decode(string $value): string + { + $charsToPosition = array_flip($this->dictionary); + $out = '0'; + foreach (array_reverse($this->stringSplit($value)) as $pos => $tmp) { + if (!isset($charsToPosition[$tmp])) { + throw new InvalidArgumentException('cannot decode string with characters not in dictionary'); + } + $out = BC::add($out, BC::mul((string)$charsToPosition[$tmp], BC::pow((string)$this->dictionaryLength, (string)$pos, 0), 0), 0); + } + + return $out; + } } \ No newline at end of file diff --git a/tests/Unit/TinyIDTest.php b/tests/Unit/TinyIDTest.php index 5821e9d..ef04d2b 100644 --- a/tests/Unit/TinyIDTest.php +++ b/tests/Unit/TinyIDTest.php @@ -1,120 +1,100 @@ -encode('0')); - self::assertEquals('0', $tinyId->decode('a')); - - self::assertEquals('b', $tinyId->encode('1')); - self::assertEquals('1', $tinyId->decode('b')); - - self::assertEquals('bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb', $tinyId->encode('18446744073709551615')); - self::assertEquals('18446744073709551615', $tinyId->decode('bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb')); - } - - /** - * @test - */ - public function accentSensitive() - { - $tinyId = new TinyID('ąä'); - - self::assertEquals('äą', $tinyId->encode('2')); - self::assertEquals('2', $tinyId->decode('äą')); - } - - /** - * @test - */ - public function caseSensitive() - { - $tinyId = new TinyID('Aa'); - - self::assertEquals('aA', $tinyId->encode('2')); - self::assertEquals('2', $tinyId->decode('aA')); - } - - - /** - * @test - */ - public function alphanumericKey() - { - $tinyId = new TinyID('FujSBZHkPMincNQr6pq0mgxw2tXAsyb8DWV534EC1RUIlYoGOJhed9afKT7vzL'); - - self::assertEquals('gzUp3uHipVr', $tinyId->encode('18446744073709551615')); - self::assertEquals('18446744073709551615', $tinyId->decode('gzUp3uHipVr')); - } - - /** - * @test - */ - public function veryLongUnicodeKey() - { - $tinyId - = new TinyID('⊷⇑≩≔⊴⊖⢻⢬⇖⊙⣮≺⇋↨⣺∄⇫⊌⊍⢶∦⠢⋠⊜⋅⋾⊔⠅⣎⋥⠌⣋⢟⋕⇮∔↻⣃⢅≭⡆∕↩⇨⢺⇩∤⣝⇛↡⡖⡃⢤≖⋍⊗∐⊒↮∜⣭⇌⇭⣒≼≴∶≵⢭⋰⡦⇏∳⇄∍⋧⋐⣉⢊∝∠⠸⠯⋋≷⣑∮≜⡚⠕⊎∎⡐⣶⋇⊂⡘⢘⡵∟⋹∿⣜⣽⢱∞⊸⣸⢪↢⣖∹⇱⇳⡫≍↲⣴≳⊋⠩⋣⣰≈≾⢽≪∫⊘∈⋶⠒≘∖∪⊺⊏⠼⢼⠐⢮⊪⊕⊿⠬⇈⠚↷≻↾≆⠄⋂↚⇙⇁⠇⊓⢎⣲⡒⠓⣻≞⣈∬⊨⋔⇠↣⢹⣍∁⠋↠⡇⊁⡅↗⣣∾≂⠴⋭⠖⡥∆⇴⊄≐⊈⣐⋑⡂⊭≝∃⠗∛⇿⊡⡮⢿∣⡢↬≏⋒⢀∥⇦⠃⣔⇒⊥⇽⊚⢌⠿≥⋡↳↛⡀⋢⣅⣵≃↑≲⇆∧⡝⊧≓⢢∡≑⋸⢲↰⢳⣧⡭≹≬⊼⡙⊠⠤⡈⇟≎↸⣫∏⡏⢛⢑⣷⇯⢃∻∭⡔⊅⢨⇝≒⊶⠉⡾⇉⡛↼∵⋿⇻⋵⇂∩∼⡋⡽⡶↘⡨⊉⢞⊟≡⢈≰⇾≤⇵≙≊⣤⠈⋩↖⋴⇡↹⠮⠦⇢∰⠵⣁≣⡁⠜⋦⋪√⢥↿⣌⇃↴⊯≫⢾⇔⡷⇊⣠⋽⇞⣞∅⠰⋘≁⇸⊾⊫⢏↽⢴⋨⋱⠣⡯⣿∊⣩⡠⋖∯⊹⠟⠺⠞⡓⡕⇕⢸⋬⊊⣇⇧↜⇹⣙⢰⠥⊮∺↧⢋⋙⣟⋼⊣⠹⣹⡻⢫↞⣄⡗⡣⣨⇤⊛⡤↦⢵↝⡱⠽⠶⇰⢉⇷⢡↪⊱⋳↔⡪⊀⋆⣘⇶⢠⣡⊽⠊⇓∽⡞⊑⊐⇇⠱≕⣀↫⢩⢦⣢⡺↭⣪∂∢⠷⣊∗⋉⠳⇺⣂⢁⋯⡡⢣⠙⇬⠡⋗⠭≧⢜⣚⡳⋫⇚⋃⢗⋮⇲∨⠆⢒⠁⋚⋞⣬≢⡧≀⋓⢇≯⡿⋜⢂⠑⋁⋈≉⡩⠍⊞⊇≽⊳⢍⡟≨⇪⇍↥⇅⠝↵∑⡸⢕∲≅⊬⠠⠪≦⡊←↕⣳⋌⊦≠⋲⠲⊝⋎⊩⇜⠨⡹⣓⊵⠧⇣⊃∱⡲⋏↓⢷⠫∉⠂≌⢯⣏⠘≶⢄≟↶⋷≸⇎⡴⣾⣛⇘⇀⢔⡉⡬⡼⊤⢆⡄⢐⠾∋→⢧⇥∀⡜⡍≿⢙⇐⡌⇼⣼≮⋺⣱↙∘⊰≗⣆↯⣕⠔≄⊻⋛⡰⠏∓⠻↟≱⋀∷⢝∴⣥≋↤⋝↱∇⡑⣦⢖⢚⣯⋄⊆⡎⠎⢓≚⋊≇≛↺∌⋤−∙⠛⊲⊢∸⣗⋟⇗⋻%'); - - self::assertEquals('18446744073709551615', $tinyId->decode($tinyId->encode('18446744073709551615'))); - } - - public function failuresProvider() - { - return [ - ['a', 'dictionary too short'], - ['aa', 'dictionary contains duplicated characters'], - ]; - } - - /** - * @test - * @dataProvider failuresProvider - */ - public function failuresOnInvalidString($invalidString, $expectedMessage) - { - try { - new TinyID($invalidString); - } catch (\InvalidArgumentException $e) { - self::assertEquals($expectedMessage, $e->getMessage()); - } - } - - /** - * @test - */ - public function failuresOnEncodingWithNegativeNumber() - { - try { - (new TinyID('ab'))->encode(-1); - } catch (\InvalidArgumentException $e) { - self::assertEquals('cannot encode negative number', $e->getMessage()); - } - } - - /** - * @test - */ - public function failuresOnDecodingWithCharacter() - { - try { - (new TinyID('ab'))->decode('x'); - } catch (\InvalidArgumentException $e) { - self::assertEquals('cannot decode string with characters not in dictionary', $e->getMessage()); - } - } -} +encode('0')); + self::assertEquals('0', $tinyId->decode('a')); + + self::assertEquals('b', $tinyId->encode('1')); + self::assertEquals('1', $tinyId->decode('b')); + + self::assertEquals('bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb', $tinyId->encode('18446744073709551615')); + self::assertEquals('18446744073709551615', $tinyId->decode('bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb')); + } + + public function testAccentSensitive(): void + { + $tinyId = new TinyID('ąä'); + + self::assertEquals('äą', $tinyId->encode('2')); + self::assertEquals('2', $tinyId->decode('äą')); + } + + public function testCaseSensitive(): void + { + $tinyId = new TinyID('Aa'); + + self::assertEquals('aA', $tinyId->encode('2')); + self::assertEquals('2', $tinyId->decode('aA')); + } + + public function testAlphanumericKey(): void + { + $tinyId = new TinyID('FujSBZHkPMincNQr6pq0mgxw2tXAsyb8DWV534EC1RUIlYoGOJhed9afKT7vzL'); + + self::assertEquals('gzUp3uHipVr', $tinyId->encode('18446744073709551615')); + self::assertEquals('18446744073709551615', $tinyId->decode('gzUp3uHipVr')); + } + + public function testVeryLongUnicodeKey(): void + { + $tinyId + = new TinyID( + '⊷⇑≩≔⊴⊖⢻⢬⇖⊙⣮≺⇋↨⣺∄⇫⊌⊍⢶∦⠢⋠⊜⋅⋾⊔⠅⣎⋥⠌⣋⢟⋕⇮∔↻⣃⢅≭⡆∕↩⇨⢺⇩∤⣝⇛↡⡖⡃⢤≖⋍⊗∐⊒↮∜⣭⇌⇭⣒≼≴∶≵⢭⋰⡦⇏∳⇄∍⋧⋐⣉⢊∝∠⠸⠯⋋≷⣑∮≜⡚⠕⊎∎⡐⣶⋇⊂⡘⢘⡵∟⋹∿⣜⣽⢱∞⊸⣸⢪↢⣖∹⇱⇳⡫≍↲⣴≳⊋⠩⋣⣰≈≾⢽≪∫⊘∈⋶⠒≘∖∪⊺⊏⠼⢼⠐⢮⊪⊕⊿⠬⇈⠚↷≻↾≆⠄⋂↚⇙⇁⠇⊓⢎⣲⡒⠓⣻≞⣈∬⊨⋔⇠↣⢹⣍∁⠋↠⡇⊁⡅↗⣣∾≂⠴⋭⠖⡥∆⇴⊄≐⊈⣐⋑⡂⊭≝∃⠗∛⇿⊡⡮⢿∣⡢↬≏⋒⢀∥⇦⠃⣔⇒⊥⇽⊚⢌⠿≥⋡↳↛⡀⋢⣅⣵≃↑≲⇆∧⡝⊧≓⢢∡≑⋸⢲↰⢳⣧⡭≹≬⊼⡙⊠⠤⡈⇟≎↸⣫∏⡏⢛⢑⣷⇯⢃∻∭⡔⊅⢨⇝≒⊶⠉⡾⇉⡛↼∵⋿⇻⋵⇂∩∼⡋⡽⡶↘⡨⊉⢞⊟≡⢈≰⇾≤⇵≙≊⣤⠈⋩↖⋴⇡↹⠮⠦⇢∰⠵⣁≣⡁⠜⋦⋪√⢥↿⣌⇃↴⊯≫⢾⇔⡷⇊⣠⋽⇞⣞∅⠰⋘≁⇸⊾⊫⢏↽⢴⋨⋱⠣⡯⣿∊⣩⡠⋖∯⊹⠟⠺⠞⡓⡕⇕⢸⋬⊊⣇⇧↜⇹⣙⢰⠥⊮∺↧⢋⋙⣟⋼⊣⠹⣹⡻⢫↞⣄⡗⡣⣨⇤⊛⡤↦⢵↝⡱⠽⠶⇰⢉⇷⢡↪⊱⋳↔⡪⊀⋆⣘⇶⢠⣡⊽⠊⇓∽⡞⊑⊐⇇⠱≕⣀↫⢩⢦⣢⡺↭⣪∂∢⠷⣊∗⋉⠳⇺⣂⢁⋯⡡⢣⠙⇬⠡⋗⠭≧⢜⣚⡳⋫⇚⋃⢗⋮⇲∨⠆⢒⠁⋚⋞⣬≢⡧≀⋓⢇≯⡿⋜⢂⠑⋁⋈≉⡩⠍⊞⊇≽⊳⢍⡟≨⇪⇍↥⇅⠝↵∑⡸⢕∲≅⊬⠠⠪≦⡊←↕⣳⋌⊦≠⋲⠲⊝⋎⊩⇜⠨⡹⣓⊵⠧⇣⊃∱⡲⋏↓⢷⠫∉⠂≌⢯⣏⠘≶⢄≟↶⋷≸⇎⡴⣾⣛⇘⇀⢔⡉⡬⡼⊤⢆⡄⢐⠾∋→⢧⇥∀⡜⡍≿⢙⇐⡌⇼⣼≮⋺⣱↙∘⊰≗⣆↯⣕⠔≄⊻⋛⡰⠏∓⠻↟≱⋀∷⢝∴⣥≋↤⋝↱∇⡑⣦⢖⢚⣯⋄⊆⡎⠎⢓≚⋊≇≛↺∌⋤−∙⠛⊲⊢∸⣗⋟⇗⋻%' + ); + + self::assertEquals('18446744073709551615', $tinyId->decode($tinyId->encode('18446744073709551615'))); + } + + public function failuresProvider(): array + { + return [ + ['a', 'dictionary too short'], + ['aa', 'dictionary contains duplicated characters'], + ]; + } + + /** + * @dataProvider failuresProvider + * @param string $invalidString + * @param string $expectedMessage + */ + public function testFailuresOnInvalidString(string $invalidString, string $expectedMessage): void + { + try { + new TinyID($invalidString); + } catch (InvalidArgumentException $e) { + self::assertEquals($expectedMessage, $e->getMessage()); + } + } + + public function testFailuresOnEncodingWithNegativeNumber(): void + { + try { + (new TinyID('ab'))->encode('-1'); + } catch (InvalidArgumentException $e) { + self::assertEquals('cannot encode negative number', $e->getMessage()); + } + } + + public function testFailuresOnDecodingWithCharacter(): void + { + try { + (new TinyID('ab'))->decode('x'); + } catch (InvalidArgumentException $e) { + self::assertEquals('cannot decode string with characters not in dictionary', $e->getMessage()); + } + } +}