From 4e3fde352895ab72885cbbfac9e06886885bf659 Mon Sep 17 00:00:00 2001 From: Mitchell van Wijngaarden Date: Sat, 27 Apr 2013 15:15:05 +0200 Subject: [PATCH] Changed namespace for better readability and easier use --- .../Exceptions/UndefinedSaltException.php | 5 + src/Mitch/Hashids/Facades/Hashids.php | 14 +++ src/Mitch/Hashids/HashidsServiceProvider.php | 101 ++++++++++++++++++ 3 files changed, 120 insertions(+) create mode 100644 src/Mitch/Hashids/Exceptions/UndefinedSaltException.php create mode 100644 src/Mitch/Hashids/Facades/Hashids.php create mode 100644 src/Mitch/Hashids/HashidsServiceProvider.php diff --git a/src/Mitch/Hashids/Exceptions/UndefinedSaltException.php b/src/Mitch/Hashids/Exceptions/UndefinedSaltException.php new file mode 100644 index 0000000..8ca672f --- /dev/null +++ b/src/Mitch/Hashids/Exceptions/UndefinedSaltException.php @@ -0,0 +1,5 @@ +package('mitch/hashids'); + } + + /** + * Register the service provider. + * + * @return void + */ + + public function register() + { + $this->registerHashidsSalt(); + + $this->registerHashidsLength(); + + $this->registerHashids(); + } + + protected function registerHashidsSalt() + { + $this->app->bind('hashids.salt', function() + { + return $this->getSalt(); + }); + } + + protected function registerHashidsLength() + { + $this->app->bind('hashids.length', function() + { + return $this->getLength(); + }); + } + + protected function registerHashids() + { + $this->app['hashids'] = $this->app->share(function($app) + { + return new Hashids($app->make('hashids.salt'), $app->make('hashids.length')); + }); + } + + /** + * Get the length used for encrypting and decrypting hashes. + * + * @return string + */ + protected function getSalt() + { + $salt = $this->app['config']['hashids::salt']; + + if (! $salt) { + throw new UndefinedSaltException('No salt has been set in the configuration.'); + } + + return $salt; + } + + /** + * Get the length used for the length of the hash. + * + * @return string + */ + protected function getLength() + { + return $this->app['config']['hashids::length']; + } + + /** + * Get the services provided by the provider. + * + * @return array + */ + public function provides() + { + return array('hashids'); + } + +} \ No newline at end of file