Shortcode   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 26
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 4
eloc 14
c 1
b 0
f 0
dl 0
loc 26
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A toID() 0 9 2
A fromID() 0 11 2
1
<?php
2
/**
3
 * Created for IG Monitoring.
4
 * User: jakim <[email protected]>
5
 * Date: 12.01.2018
6
 */
7
8
namespace jakim\ig;
9
10
11
class Shortcode
12
{
13
    protected static $chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_';
14
15
    public static function fromID($id)
16
    {
17
        $id = explode('_', $id)[0];
18
        $code = '';
19
        while ($id > 0) {
20
            $remainder = $id % 64;
21
            $id = ($id - $remainder) / 64;
22
            $code = static::$chars{$remainder} . $code;
23
        };
24
25
        return $code;
26
    }
27
28
    public static function toID($shortcode)
29
    {
30
        $id = 0;
31
        for ($i = 0; $i < strlen($shortcode); $i++) {
32
            $c = $shortcode[$i];
33
            $id = $id * 64 + strpos(static::$chars, $c);
34
        }
35
36
        return $id;
37
    }
38
}