Completed
Push — master ( 1afd97...8dffa3 )
by ARCANEDEV
10s
created

HashidsDriver::decode()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 1
rs 9.4285
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