diff --git a/.travis.yml b/.travis.yml index 6a8ff58..d3d390c 100644 --- a/.travis.yml +++ b/.travis.yml @@ -3,7 +3,7 @@ language: php sudo: false php: - - 7.0 + - 7.1.3 - 7.1 - 7.2 - nightly diff --git a/README.md b/README.md index 3277265..e0effe2 100644 --- a/README.md +++ b/README.md @@ -26,7 +26,7 @@ Feel free to check out the [releases](https://github.com/ARCANEDEV/Hasher/releas * Flexible hash manager. * Well documented & IDE Friendly. * Well tested with maximum code quality. - * Laravel `5.0 | 5.1 | 5.2 | 5.3 | 5.4 | 5.5` are supported. + * Laravel `5.0 | 5.1 | 5.2 | 5.3 | 5.4 | 5.5 | 5.6` are supported. * Made with :heart: & :coffee:. ## Table of contents diff --git a/_docs/0-Home.md b/_docs/0-Home.md index cf05cab..a9cf398 100644 --- a/_docs/0-Home.md +++ b/_docs/0-Home.md @@ -8,7 +8,7 @@ The Hasher package provides a flexible hash manager for Laravel. * Flexible hash manager. * Well documented & IDE Friendly. * Well tested with maximum code quality. - * Laravel `5.0 | 5.1 | 5.2 | 5.3 | 5.4 | 5.5` are supported. + * Laravel `5.0 | 5.1 | 5.2 | 5.3 | 5.4 | 5.5 | 5.6` are supported. * Made with :heart: & :coffee:. ## Table of contents diff --git a/_docs/1-Installation-and-Setup.md b/_docs/1-Installation-and-Setup.md index 9b51fe3..e7795a1 100644 --- a/_docs/1-Installation-and-Setup.md +++ b/_docs/1-Installation-and-Setup.md @@ -6,14 +6,6 @@ 2. [Configuration](2-Configuration.md) 3. [Usage](3-Usage.md) -## Server Requirements - -The Hasher package has a few system requirements: - -``` -- PHP >= 7.0 -``` - ## Version Compatibility | Hasher | Laravel | @@ -21,6 +13,7 @@ The Hasher package has a few system requirements: | ![Hasher v2.0.x][hasher_2_0_x] | ![Laravel v5.0][laravel_5_0] ![Laravel v5.1][laravel_5_1] ![Laravel v5.2][laravel_5_2] ![Laravel v5.3][laravel_5_3] | | ![Hasher v2.1.x][hasher_2_1_x] | ![Laravel v5.4][laravel_5_4] | | ![Hasher v2.2.x][hasher_2_2_x] | ![Laravel v5.5][laravel_5_5] | +| ![Hasher v2.3.x][hasher_2_3_x] | ![Laravel v5.6][laravel_5_6] | [laravel_5_0]: https://img.shields.io/badge/v5.0-supported-brightgreen.svg?style=flat-square "Laravel v5.0" [laravel_5_1]: https://img.shields.io/badge/v5.1-supported-brightgreen.svg?style=flat-square "Laravel v5.1" @@ -28,10 +21,12 @@ The Hasher package has a few system requirements: [laravel_5_3]: https://img.shields.io/badge/v5.3-supported-brightgreen.svg?style=flat-square "Laravel v5.3" [laravel_5_4]: https://img.shields.io/badge/v5.4-supported-brightgreen.svg?style=flat-square "Laravel v5.4" [laravel_5_5]: https://img.shields.io/badge/v5.5-supported-brightgreen.svg?style=flat-square "Laravel v5.5" +[laravel_5_6]: https://img.shields.io/badge/v5.6-supported-brightgreen.svg?style=flat-square "Laravel v5.6" [hasher_2_0_x]: https://img.shields.io/badge/version-2.0.*-blue.svg?style=flat-square "Hasher v2.0.*" [hasher_2_1_x]: https://img.shields.io/badge/version-2.1.*-blue.svg?style=flat-square "Hasher v2.1.*" [hasher_2_2_x]: https://img.shields.io/badge/version-2.2.*-blue.svg?style=flat-square "Hasher v2.2.*" +[hasher_2_3_x]: https://img.shields.io/badge/version-2.3.*-blue.svg?style=flat-square "Hasher v2.3.*" ## Composer diff --git a/_docs/2-Configuration.md b/_docs/2-Configuration.md index 88ea88a..d48d8ca 100644 --- a/_docs/2-Configuration.md +++ b/_docs/2-Configuration.md @@ -12,13 +12,15 @@ After you've published the config file `config/hasher.php`, you can customize th ```php return [ - /* ------------------------------------------------------------------------------------------------ + + /* ----------------------------------------------------------------- | Defaults - | ------------------------------------------------------------------------------------------------ + | ----------------------------------------------------------------- */ + 'default' => [ - 'driver' => 'hashids', - 'connection' => 'main', + 'driver' => 'hashids', + 'option' => 'main', ], //.. @@ -32,47 +34,35 @@ You can set your default hashing `driver` and `connection` for your application. return [ //... - /* ------------------------------------------------------------------------------------------------ + /* ----------------------------------------------------------------- | Drivers - | ------------------------------------------------------------------------------------------------ + | ----------------------------------------------------------------- */ + 'drivers' => [ - 'hashids' => Arcanedev\Hasher\Drivers\HashidsDriver::class, - ], - - //... -]; -``` -The `drivers` attribute allows you to specify the supported drivers for your application and you can also create & add your own custom driver. + 'hashids' => [ + 'driver' => Arcanedev\Hasher\Drivers\HashidsDriver::class, -## Connections + 'options' => [ + 'main' => [ + 'salt' => env('HASHIDS_MAIN_SALT', ''), + 'length' => env('HASHIDS_MAIN_LENGTH', 0), + 'alphabet' => env('HASHIDS_MAIN_ALPHABET', 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890'), + ], -```php -return [ - // ... - - /* ------------------------------------------------------------------------------------------------ - | Connections - | ------------------------------------------------------------------------------------------------ - */ - 'connections' => [ - 'hashids' => [ - 'main' => [ - 'salt' => env('HASHIDS_MAIN_SALT', ''), - 'length' => env('HASHIDS_MAIN_LENGTH', 0), - 'alphabet' => env('HASHIDS_MAIN_ALPHABET', 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890'), + // ], - // 'alt' => [ - // 'salt' => '', - // 'length' => 0, - // 'alphabet' => 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890', - // ], ], + ], + + //... ]; ``` -The `connections` attribute are options for your hashing driver, it may vary according to your driver. +The `drivers` attribute allows you to specify the supported drivers for your application and you can also create & add your own custom `class` & `options`. + +The `options` attribute are options for your hashing driver, it may vary according to your driver. You can also specify multiple connections for each driver, like `main` and `alt`, it allows you to hash each entity in your application to different hashing results. diff --git a/_docs/3-Usage.md b/_docs/3-Usage.md index 7cd0289..c79cdcc 100644 --- a/_docs/3-Usage.md +++ b/_docs/3-Usage.md @@ -67,7 +67,7 @@ The `driver()` method accepts an argument as the hash driver name. So you can do $hasher = hasher()->driver('custom-driver'); ``` -And to get your default driver with different `connection`, you can use the `with()` method for that: +And to get your default driver with different `option`, you can use the `with()` method for that: ```php $hasher = hasher()->with('alt'); @@ -79,7 +79,7 @@ You can also use another helper if you don't like calling multiple methods: $hasher = hash_with('alt'); ``` -Of course, you can specify the `connection` and the `driver` at the same time with the same helpers: +Of course, you can specify the `option` and the `driver` at the same time with the same helpers: ```php $hasher = hasher()->with('alt', 'custom-driver'); @@ -97,11 +97,11 @@ Other useful methods: // To get your default driver name $driverName = hasher()->getDefaultDriver(); -// To get your default connection name -$connection = hasher()->getDefaultConnection() +// To get your default option name +$option = hasher()->getDefaultOption() -// To set the default connection name -$manager = hasher()->connection('alt'); +// To set the default option name +$manager = hasher()->option('alt'); ``` ## Facade @@ -197,7 +197,7 @@ class CustomDriver implements HashDriver } ``` -As you can see, the `__constructor` will receive an array as argument containing the selected `connection` settings. +As you can see, the `__constructor` will receive an array as argument containing the selected `option` settings. > Check the [HashidsDriver](https://github.com/ARCANEDEV/Hasher/blob/master/src/Drivers/HashidsDriver.php) class as example. @@ -209,41 +209,32 @@ After that, you need to add your driver to the supported `drivers` in your `conf return [ //... - /* ------------------------------------------------------------------------------------------------ + /* ----------------------------------------------------------------- | Drivers - | ------------------------------------------------------------------------------------------------ + | ----------------------------------------------------------------- */ + 'drivers' => [ - 'hashids' => Arcanedev\Hasher\Drivers\HashidsDriver::class, - 'custom' => App\Hashers\CustomDriver::class + 'hashids' => [ + 'driver' => Arcanedev\Hasher\Drivers\HashidsDriver::class, + 'options' => [ + 'main' => [ + 'salt' => env('HASHIDS_MAIN_SALT', ''), + 'length' => env('HASHIDS_MAIN_LENGTH', 0), + 'alphabet' => env('HASHIDS_MAIN_ALPHABET', 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890'), + ], + + // + ], + ], + + 'custom' => [ + 'driver' => App\Hashers\CustomDriver::class, + ] ], //... ]; ``` -This is optional but you can specify a `connection` settings (or multiple) for your `custom` driver: - -```php - [ - 'custom' => [ - 'main' => [ - 'option-1' => 'value-1', - 'option-2' => 'value-2', - //... - ], - ], - 'hashids' => [ - // - ], - ], -]; -``` +This is optional but you can specify a `option` settings (or multiple) for your `custom` driver. diff --git a/composer.json b/composer.json index 0991bdf..a5dcf2b 100644 --- a/composer.json +++ b/composer.json @@ -16,14 +16,14 @@ "type": "library", "license": "MIT", "require": { - "php": ">=7.0", - "arcanedev/support": "~4.2.0", - "hashids/hashids": "~2.0" + "php": ">=7.1.3", + "arcanedev/support": "~4.3.0", + "hashids/hashids": "~3.0" }, "require-dev": { - "orchestra/testbench": "~3.5.0", - "phpunit/phpunit": "~6.0", - "phpunit/phpcov": "~4.0" + "orchestra/testbench": "~3.6.0", + "phpunit/phpunit": "~7.0", + "phpunit/phpcov": "~5.0" }, "autoload": { "psr-4": { diff --git a/config/hasher.php b/config/hasher.php index c6fb45b..41682ad 100644 --- a/config/hasher.php +++ b/config/hasher.php @@ -8,8 +8,8 @@ */ 'default' => [ - 'driver' => 'hashids', - 'connection' => 'main', + 'driver' => 'hashids', + 'option' => 'main', ], /* ----------------------------------------------------------------- @@ -18,28 +18,21 @@ */ 'drivers' => [ - 'hashids' => Arcanedev\Hasher\Drivers\HashidsDriver::class, - ], - - /* ----------------------------------------------------------------- - | Connections - | ----------------------------------------------------------------- - */ - 'connections' => [ 'hashids' => [ - 'main' => [ - 'salt' => env('HASHIDS_MAIN_SALT', ''), - 'length' => env('HASHIDS_MAIN_LENGTH', 0), - 'alphabet' => env('HASHIDS_MAIN_ALPHABET', 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890'), - ], + 'driver' => Arcanedev\Hasher\Drivers\HashidsDriver::class, + + 'options' => [ + 'main' => [ + 'salt' => env('HASHIDS_MAIN_SALT', ''), + 'length' => env('HASHIDS_MAIN_LENGTH', 0), + 'alphabet' => env('HASHIDS_MAIN_ALPHABET', 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890'), + ], - // 'alt' => [ - // 'salt' => '', - // 'length' => 0, - // 'alphabet' => 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890', - // ], + // + ], ], + ], ]; diff --git a/helpers.php b/helpers.php index 6c0b276..71bd863 100644 --- a/helpers.php +++ b/helpers.php @@ -16,13 +16,13 @@ function hasher() /** * Get the Hash Driver instance. * - * @param string $connection + * @param string $option * @param string|null $driver * * @return \Arcanedev\Hasher\Contracts\HashDriver */ - function hash_with($connection, $driver = null) + function hash_with($option, $driver = null) { - return hasher()->with($connection, $driver); + return hasher()->with($option, $driver); } } diff --git a/src/Contracts/HashManager.php b/src/Contracts/HashManager.php index 981649d..b5501a4 100644 --- a/src/Contracts/HashManager.php +++ b/src/Contracts/HashManager.php @@ -21,20 +21,20 @@ interface HashManager extends HashDriver public function getDefaultDriver(); /** - * Get the default driver connection. + * Get the default driver option. * * @return string */ - public function getDefaultConnection(); + public function getDefaultOption(); /** - * Set the hasher connection. + * Set the hasher option. * - * @param string $connection + * @param string $option * * @return \Arcanedev\Hasher\HashManager */ - public function connection($connection = null); + public function option($option = null); /* ----------------------------------------------------------------- | Main Methods @@ -45,11 +45,11 @@ public function connection($connection = null); * Get a driver instance. * * @param string $driver - * @param string|null $connection + * @param string|null $option * * @return \Arcanedev\Hasher\Contracts\HashDriver */ - public function with($connection = null, $driver = null); + public function with($option = null, $driver = null); /** * Get a driver instance. diff --git a/src/HashManager.php b/src/HashManager.php index 3bfc93d..93e06f2 100644 --- a/src/HashManager.php +++ b/src/HashManager.php @@ -1,6 +1,7 @@ connection ?? $this->getHasherConfig('default.connection', 'main'); + return $this->option ?? $this->getHasherConfig('default.option', 'main'); } /** - * Set the hasher connection. + * Set the hasher option. * - * @param string $connection + * @param string $option * * @return \Arcanedev\Hasher\HashManager */ - public function connection($connection = null) + public function option($option = null) { - if ( ! is_null($connection)) { - $this->connection = $connection; + if ( ! is_null($option)) { + $this->option = $option; $this->buildDrivers(); } @@ -91,14 +92,13 @@ public function connection($connection = null) * Get a driver instance. * * @param string $driver - * @param string|null $connection + * @param string|null $option * * @return \Arcanedev\Hasher\Contracts\HashDriver */ - public function with($connection = null, $driver = null) + public function with($option = null, $driver = null) { - return $this->connection($connection) - ->driver($driver); + return $this->option($option)->driver($driver); } /** @@ -120,8 +120,8 @@ private function buildDrivers() { $drivers = $this->getHasherConfig('drivers', []); - foreach ($drivers as $name => $class) { - $this->buildDriver($name, $class); + foreach ($drivers as $name => $driver) { + $this->buildDriver($name, $driver); } } @@ -129,16 +129,14 @@ private function buildDrivers() * Build the driver. * * @param string $name - * @param string $class + * @param array $driver * - * @return Contracts\HashDriver + * @return \Arcanedev\Hasher\Contracts\HashDriver */ - private function buildDriver($name, $class) + private function buildDriver($name, array $driver) { - $connection = $this->getDefaultConnection(); - - return $this->drivers[$name] = new $class( - $this->getHasherConfig("connections.$name.$connection", []) + return $this->drivers[$name] = new $driver['driver']( + Arr::get($driver, 'options.'.$this->getDefaultOption(), []) ); } diff --git a/tests/Drivers/HashidsDriverTest.php b/tests/Drivers/HashidsDriverTest.php index 1e52fcb..bd68cbe 100644 --- a/tests/Drivers/HashidsDriverTest.php +++ b/tests/Drivers/HashidsDriverTest.php @@ -28,7 +28,11 @@ protected function setUp() { parent::setUp(); - $this->hasher = new HashidsDriver($this->getConfig()); + $this->hasher = new HashidsDriver([ + 'salt' => 'This is my main salt', + 'length' => 8, + 'alphabet' => 'abcdefghij1234567890', + ]); } protected function tearDown() @@ -55,6 +59,7 @@ public function it_can_be_instantiated() static::assertInstanceOf($expected, $this->hasher); } } + /** @test */ public function it_assert_it_can_encode_and_decode() { @@ -64,23 +69,4 @@ public function it_assert_it_can_encode_and_decode() static::assertNotEquals($hashed, $value); static::assertSame($value, $this->hasher->decode($hashed)); } - - /* ----------------------------------------------------------------- - | Other Methods - | ----------------------------------------------------------------- - */ - - /** - * Get Hashids config. - * - * @return array - */ - private function getConfig() - { - return [ - 'salt' => 'This is my main salt', - 'length' => 8, - 'alphabet' => 'abcdefghij1234567890', - ]; - } } diff --git a/tests/HasherManagerTest.php b/tests/HasherManagerTest.php index 26aeda6..486ad3a 100644 --- a/tests/HasherManagerTest.php +++ b/tests/HasherManagerTest.php @@ -60,17 +60,17 @@ public function it_can_get_default_driver() } /** @test */ - public function it_can_get_default_connection() + public function it_can_get_default_option() { - static::assertSame('main', $this->manager->getDefaultConnection()); + static::assertSame('main', $this->manager->getDefaultOption()); } /** @test */ - public function it_can_set_default_connection() + public function it_can_set_default_option() { - $this->manager->connection('alt'); + $this->manager->option('alt'); - static::assertSame('alt', $this->manager->getDefaultConnection()); + static::assertSame('alt', $this->manager->getDefaultOption()); } /** @test */ @@ -104,11 +104,11 @@ public function it_can_get_hash_driver_with_name() } /** @test */ - public function it_can_get_hash_driver_with_name_and_connection() + public function it_can_get_hash_driver_with_name_and_option() { $driver = $this->manager->with('alt', 'custom'); - static::assertSame('alt', $this->manager->getDefaultConnection()); + static::assertSame('alt', $this->manager->getDefaultOption()); $expectations = [ \Arcanedev\Hasher\Contracts\HashDriver::class, @@ -124,8 +124,8 @@ public function it_can_get_hash_driver_with_name_and_connection() public function it_can_encode_and_decode() { $value = 123456; - $mainDriver = $this->manager->connection('main')->driver(); - $altDriver = $this->manager->connection('alt')->driver(); + $mainDriver = $this->manager->option('main')->driver(); + $altDriver = $this->manager->option('alt')->driver(); $mainHashed = $mainDriver->encode($value); $altHashed = $altDriver->encode($value); diff --git a/tests/TestCase.php b/tests/TestCase.php index b1079f6..235cab5 100644 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -22,11 +22,6 @@ protected function setUp() $this->app->loadDeferredProviders(); } - protected function tearDown() - { - parent::tearDown(); - } - /** * Get package providers. * @@ -66,23 +61,25 @@ protected function getEnvironmentSetUp($app) $config = $app['config']; $config->set('hasher.drivers', [ - 'hashids' => \Arcanedev\Hasher\Drivers\HashidsDriver::class, - 'custom' => \Arcanedev\Hasher\Tests\Stubs\CustomHasherClient::class, - ]); - - $config->set('hasher.connections', [ - 'hashids' => [ - 'main' => [ - 'salt' => 'This is my main salt', - 'length' => 8, - 'alphabet' => 'abcdefghij1234567890', - ], - 'alt' => [ - 'salt' => 'This is my alternative salt', - 'length' => 6, - 'alphabet' => 'ABCDEFGHIJ1234567890', + 'hashids' => [ + 'driver' => \Arcanedev\Hasher\Drivers\HashidsDriver::class, + 'options' => [ + 'main' => [ + 'salt' => 'This is my main salt', + 'length' => 8, + 'alphabet' => 'abcdefghij1234567890', + ], + 'alt' => [ + 'salt' => 'This is my alternative salt', + 'length' => 6, + 'alphabet' => 'ABCDEFGHIJ1234567890', + ], ], ], + + 'custom' => [ + 'driver' => \Arcanedev\Hasher\Tests\Stubs\CustomHasherClient::class + ], ]); } }