Completed
Push — master ( a6e8b1...08dd78 )
by Elf
01:49
created

Base62Driver::__construct()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 2.0078

Importance

Changes 0
Metric Value
cc 2
eloc 7
nc 2
nop 1
dl 0
loc 11
ccs 7
cts 8
cp 0.875
crap 2.0078
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
        if (extension_loaded('gmp')) {
34 3
            $this->base62 = new GmpEncoder($options);
35 3
        } else {
36
            $this->base62 = new PhpEncoder($options);
37
        }
38
39 3
        $this->integer = Arr::get($config, 'integer', $this->integer);
40 3
    }
41
42
    /**
43
     * Encode the data.
44
     *
45
     * @param  mixed  $data
46
     * @return string
47
     */
48 2
    public function encode($data)
49
    {
50 2
        return $this->base62->encode($data, $this->integer);
51
    }
52
53
    /**
54
     * Decode the data.
55
     *
56
     * @param  string  $data
57
     * @return mixed
58
     */
59 2
    public function decode($data)
60
    {
61 2
        return $this->base62->decode($data, $this->integer);
62
    }
63
}
64