Completed
Push — master ( 5fbe2f...d42655 )
by Elf
02:28
created

Base62Driver   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 50
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
dl 0
loc 50
ccs 9
cts 9
cp 1
rs 10
c 0
b 0
f 0
wmc 4
lcom 1
cbo 3

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 2
A encode() 0 4 1
A decode() 0 4 1
1
<?php
2
3
namespace ElfSundae\Laravel\Hashid;
4
5
use Illuminate\Support\Arr;
6
use Tuupola\Base62\GmpEncoder;
7
use Tuupola\Base62\PhpEncoder;
8
9
class Base62Driver implements DriverInterface
10
{
11
    /**
12
     * The Base62 instance.
13
     *
14
     * @var \Tuupola\Base62\GmpEncoder|\Tuupola\Base62\PhpEncoder
15
     */
16
    protected $base62;
17
18
    /**
19
     * Indicates integer encoding.
20
     *
21
     * @var bool
22
     */
23
    protected $integer = false;
24
25
    /**
26
     * Create a new hashid driver instance.
27
     *
28
     * @param  array  $config
29
     */
30 3
    public function __construct(array $config = [])
31
    {
32 3
        $options = Arr::only($config, ['characters']);
33 3
        $this->base62 = extension_loaded('gmp')
34 3
            ? new GmpEncoder($options) : new PhpEncoder($options);
35 3
    }
36
37
    /**
38
     * Encode the data.
39
     *
40
     * @param  mixed  $data
41
     * @return string
42
     */
43 2
    public function encode($data)
44
    {
45 2
        return $this->base62->encode($data, $this->integer);
46
    }
47
48
    /**
49
     * Decode the data.
50
     *
51
     * @param  mixed  $data
52
     * @return mixed
53
     */
54 2
    public function decode($data)
55
    {
56 2
        return $this->base62->decode($data, $this->integer);
57
    }
58
}
59