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

Base62Driver::__construct()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 4
nc 2
nop 1
dl 0
loc 6
ccs 5
cts 5
cp 1
crap 2
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) : 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