Completed
Push — master ( 40ea99...4590e9 )
by Elf
02:06
created

Base62Driver::__construct()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2.0185

Importance

Changes 0
Metric Value
cc 2
eloc 5
nc 2
nop 1
dl 0
loc 7
ccs 5
cts 6
cp 0.8333
crap 2.0185
rs 9.4285
c 0
b 0
f 0
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) :
35
            new PhpEncoder($options);
36 3
    }
37
38
    /**
39
     * Encode the data.
40
     *
41
     * @param  mixed  $data
42
     * @return string
43
     */
44 2
    public function encode($data)
45
    {
46 2
        return $this->base62->encode($data, $this->integer);
47
    }
48
49
    /**
50
     * Decode the data.
51
     *
52
     * @param  mixed  $data
53
     * @return mixed
54
     */
55 2
    public function decode($data)
56
    {
57 2
        return $this->base62->decode($data, $this->integer);
58
    }
59
}
60