Skip to content

Commit 31282af

Browse files
Add files via upload
1 parent 594d80f commit 31282af

File tree

6 files changed

+343
-2
lines changed

6 files changed

+343
-2
lines changed

LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2017 Mustafa Khaled
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

README.md

Lines changed: 109 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,109 @@
1-
# laravel-token
2-
Generate unique Laravel Token
1+
# Laravel Token
2+
3+
![Laravel](http://getfreetutorial.com/wp-content/uploads/2016/07/Laravel-From-Scratch.jpg)
4+
5+
Laravel unique Token Generator
6+
```php
7+
// Generate unique Token From Database.
8+
$new_token = $token->Unique($table_name, $column_name, $size);
9+
10+
```
11+
12+
## Installation
13+
14+
Require this package, with [Composer](https://getcomposer.org/), in the root directory of your project.
15+
16+
```bash
17+
$ composer require dirape/token
18+
```
19+
20+
Add the service provider to `config/app.php` in the `providers` array, or if you're using Laravel 5.5, this can be done via the automatic package discovery.
21+
22+
```php
23+
Dirape\Token\TokenServiceProvider::class
24+
```
25+
26+
If you want you can use the [facade](http://laravel.com/docs/facades). Add the reference in `config/app.php` to your aliases array.
27+
28+
```php
29+
'Token'=>\Dirape\Token\Facades\Facade::class
30+
```
31+
32+
## Documentation
33+
34+
#### Generate unique token
35+
36+
With this package you can generate unqiue token not repated in database just by using `unique($table_name,$column_name,$size)` Function `$table_name` is the table name in database , `$column_name` is the column name in the table, `$size` is token size.
37+
38+
39+
#### Generate unique string token
40+
41+
generate unique strings token with the same signature of unique token with function `UniqueString($table_name,$column_name,$size)`.
42+
43+
44+
#### Generate unique integer token
45+
46+
generate unique integers token with the same signature of unique token with function `UniqueNumber($table_name,$column_name,$size)`.
47+
48+
49+
#### Generate random token
50+
generate random token with function `Random($size)` and `$size` is the size of token length.
51+
52+
53+
#### Generate random integer token
54+
generate random integer token with function `RandomNumber($size)` and `$size` is the size of token length.
55+
56+
57+
#### Generate random string token
58+
generate random string token with function `RandomString($size)` and `$size` is the size of token length.
59+
60+
#### Special Characters
61+
62+
use `true` to allow special characters in your token `!@#$%^&*()` in all functions just like `Random($size,true)`.
63+
64+
### Examples
65+
66+
Here you can see an example of just how simple this package is to use.
67+
#### Unique Token
68+
```php
69+
70+
// Generate unique token not rebeated in database table with column name
71+
Token::Unique($table_name, $column_name, 10 );
72+
//Result: fCWih6TDAf
73+
74+
75+
// Generate unique integer token not rebeated in database table with column name
76+
Token::UniqueNumber($table_name, $column_name, 10 );
77+
//Result: 9647307239
78+
79+
80+
// Generate unique string token not rebeated in database table with column name
81+
Token::UniqueString($table_name, $column_name, 10 );
82+
//Result: SOUjkyAyxC
83+
84+
85+
//You can use special characters just add "true" to the function
86+
Token::Unique($table_name, $column_name, 10,true );
87+
//Result: H@klU$u^3z
88+
```
89+
90+
#### Random Token (not unique)
91+
```php
92+
$size=10;
93+
// Generate random token
94+
Token::Random($size);
95+
96+
// Generate random integer token
97+
Token::RandomNumber($size);
98+
99+
// Generate random string token
100+
Token::RandomString($size);
101+
102+
//You can use special characters just add "true" to the function
103+
Token::Random($size,true);
104+
105+
```
106+
107+
## License
108+
109+
[MIT](LICENSE) © [Mustafa Khaled](https://github.com/Dirape)

composer.json

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
{
2+
"name": "dirape/token",
3+
"description": "Unique Token Generator For Laravel",
4+
5+
"authors": [
6+
{
7+
"name": "MustafaKhaled",
8+
"email": "[email protected]"
9+
}
10+
],
11+
"keywords": ["laravel", "token", "generate", "token generator", "unique token", "laravel token", "php token"],
12+
"license": "MIT",
13+
"require": {
14+
"php": ">=5.4.0",
15+
"illuminate/support": "~5.0 || ~4.0"
16+
17+
},
18+
"autoload": {
19+
"psr-4": {
20+
"Dirape\\Token\\": "src/"
21+
}
22+
},
23+
24+
"extra": {
25+
26+
"laravel": {
27+
"providers": [
28+
29+
"Dirape\\Token\\TokenServiceProvider"
30+
],
31+
"aliases": {
32+
"Token":"Dirape\\Token\\Facades\\Facade"
33+
}
34+
}
35+
},
36+
"minimum-stability" : "stable"
37+
38+
39+
}

src/Facades/Facade.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
namespace Dirape\Token\Facades;
3+
4+
5+
class Facade extends \Illuminate\Support\Facades\Facade
6+
{
7+
/**
8+
* Get the registered name of the component.
9+
*
10+
* @return string
11+
*/
12+
protected static function getFacadeAccessor()
13+
{
14+
return 'Token';
15+
}
16+
17+
}

src/Token.php

Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
<?php
2+
3+
namespace Dirape\Token;
4+
5+
6+
use Illuminate\Support\Facades\DB;
7+
8+
class Token
9+
{
10+
/**
11+
* Create a new Unique Token.
12+
*
13+
* @return string
14+
*/
15+
public function Unique($table, $col, $size, $special = false)
16+
{
17+
$this->SpecialCharacter = $special;
18+
Do {
19+
20+
$code = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
21+
$code .= "abcdefghijklmnopqrstuvwxyz";
22+
$code .= "0123456789";
23+
$token = $this->Generate($code, $size);
24+
$t = DB::table($table)->where($col, $token)->first();
25+
26+
} while ($t);
27+
return $token;
28+
}
29+
30+
/**
31+
* Create a new Unique Integer Token.
32+
*
33+
* @return integer
34+
*/
35+
public function UniqueNumber($table, $col, $size, $special = false)
36+
{
37+
$this->SpecialCharacter = $special;
38+
Do {
39+
$code = "0123456789";
40+
$token = $this->Generate($code, $size);
41+
$t = DB::table($table)->where($col, $token)->first();
42+
43+
} while ($t);
44+
return $token;
45+
}
46+
47+
/**
48+
* Create a new Unique String Token.
49+
*
50+
* @return string
51+
*/
52+
public function UniqueString($table, $col, $size, $special = false)
53+
{
54+
$this->SpecialCharacter = $special;
55+
Do {
56+
$code = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
57+
$code .= "abcdefghijklmnopqrstuvwxyz";
58+
$token = $this->Generate($code, $size);
59+
$t = DB::table($table)->where($col, $token)->first();
60+
61+
} while ($t);
62+
return $token;
63+
}
64+
65+
/**
66+
* Create a new Random Token.
67+
*
68+
* @return string
69+
*/
70+
public function Random($size, $special = false)
71+
{
72+
$this->SpecialCharacter = $special;
73+
$code = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
74+
$code .= "abcdefghijklmnopqrstuvwxyz";
75+
$code .= "0123456789";
76+
$token = $this->Generate($code, $size);
77+
return $token;
78+
}
79+
80+
/**
81+
* Create a new Random Number Token.
82+
*
83+
* @return string
84+
*/
85+
public function RandomNumber($size, $special = false)
86+
{
87+
$this->SpecialCharacter = $special;
88+
89+
$code = "0123456789";
90+
$token = $this->Generate($code, $size);
91+
return $token;
92+
}
93+
94+
/**
95+
* Create a new Random Number Token.
96+
*
97+
* @return string
98+
*/
99+
public function RandomString($size, $special = false)
100+
{
101+
$this->SpecialCharacter = $special;
102+
103+
$code = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
104+
$code .= "abcdefghijklmnopqrstuvwxyz";
105+
$token = $this->Generate($code, $size);
106+
return $token;
107+
}
108+
109+
/**
110+
* Generate The Token.
111+
*
112+
* @return string
113+
*/
114+
private function Generate($code, $size)
115+
{
116+
if ($this->SpecialCharacter) {
117+
$code .= '!@#$%^&*()';
118+
}
119+
$token = '';
120+
$max = strlen($code);
121+
for ($i = 0; $i < $size; $i++) {
122+
$token .= $code[random_int(0, $max - 1)];
123+
}
124+
return $token;
125+
}
126+
127+
}

src/TokenServiceProvider.php

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?php
2+
3+
namespace Dirape\Token;
4+
5+
use Illuminate\Support\ServiceProvider;
6+
7+
class TokenServiceProvider extends ServiceProvider
8+
{
9+
/**
10+
* Bootstrap the application services.
11+
*
12+
* @return void
13+
*/
14+
public function boot()
15+
{
16+
//
17+
}
18+
19+
/**
20+
* Register the application services.
21+
*
22+
* @return void
23+
*/
24+
public function register()
25+
{
26+
$this->app->bind('Token',function(){
27+
return new Token;
28+
});
29+
}
30+
}

0 commit comments

Comments
 (0)