1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Arcanedev\Hasher\Drivers; |
6
|
|
|
|
7
|
|
|
use Arcanedev\Hasher\Contracts\HashDriver; |
8
|
|
|
use Hashids\Hashids; |
9
|
|
|
use Illuminate\Support\Arr; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Class HashidsDriver |
13
|
|
|
* |
14
|
|
|
* @package Arcanedev\Hasher\Drivers |
15
|
|
|
* @author ARCANEDEV <[email protected]> |
16
|
|
|
*/ |
17
|
|
|
class HashidsDriver implements HashDriver |
18
|
|
|
{ |
19
|
|
|
/* ----------------------------------------------------------------- |
20
|
|
|
| Properties |
21
|
|
|
| ----------------------------------------------------------------- |
22
|
|
|
*/ |
23
|
|
|
|
24
|
|
|
/** @var \Hashids\Hashids */ |
25
|
|
|
private $hasher; |
26
|
|
|
|
27
|
|
|
/* ----------------------------------------------------------------- |
28
|
|
|
| Constructor |
29
|
|
|
| ----------------------------------------------------------------- |
30
|
|
|
*/ |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* HashidsDriver constructor. |
34
|
|
|
* |
35
|
|
|
* @param array $options |
36
|
|
|
*/ |
37
|
72 |
|
public function __construct(array $options) |
38
|
|
|
{ |
39
|
72 |
|
$this->hasher = new Hashids( |
40
|
72 |
|
Arr::get($options, 'salt', ''), |
41
|
72 |
|
Arr::get($options, 'length', 0), |
42
|
72 |
|
Arr::get($options, 'alphabet', 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890') |
43
|
|
|
); |
44
|
72 |
|
} |
45
|
|
|
|
46
|
|
|
/* ----------------------------------------------------------------- |
47
|
|
|
| Main Methods |
48
|
|
|
| ----------------------------------------------------------------- |
49
|
|
|
*/ |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* Encode the value. |
53
|
|
|
* |
54
|
|
|
* @param mixed $value |
55
|
|
|
* |
56
|
|
|
* @return string |
57
|
|
|
*/ |
58
|
24 |
|
public function encode($value) |
59
|
|
|
{ |
60
|
24 |
|
return $this->hasher->encode($value); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Decode the hashed value. |
65
|
|
|
* |
66
|
|
|
* @param string $hashed |
67
|
|
|
* |
68
|
|
|
* @return mixed |
69
|
|
|
*/ |
70
|
24 |
|
public function decode($hashed) |
71
|
|
|
{ |
72
|
24 |
|
return head( |
73
|
24 |
|
$this->hasher->decode($hashed) |
74
|
|
|
); |
75
|
|
|
} |
76
|
|
|
} |
77
|
|
|
|