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