Completed
Push — master ( 61323d...82f5dc )
by
unknown
17s queued 10s
created

SteamId::convertToAll()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 13
ccs 7
cts 7
cp 1
rs 9.8333
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 1
1
<?php
2
3
namespace Syntax\SteamApi;
4
5
use Syntax\SteamApi\Exceptions\UnrecognizedId;
6
7
trait SteamId
8
{
9
    public $formatted;
10
11
    private $rawValue;
12
13
    private static $ID32 = 'id32';
14
15
    private static $ID64 = 'id64';
16
17
    private static $ID3 = 'id3';
18
19
    private static $id64Base = '76561197960265728';
20
21
    /**
22
     * @param string|int  $id
23
     * @param string|null $format
24
     *
25
     * @return mixed
26
     */
27 8
    public function convertId($id, $format = null)
28
    {
29 8
        $this->convertToAll($id);
30
31
        switch ($format) {
32 8
            case 'ID32':
33 8
            case 'id32':
34 8
            case 32:
35
                return $this->formatted->{self::$ID32};
36 8
            case 'ID64':
37 8
            case 'id64':
38 8
            case 64:
39
                return $this->formatted->{self::$ID64};
40 8
            case 'ID3':
41 8
            case 'id3':
42 8
            case 3:
43
                return $this->formatted->{self::$ID3};
44
            default:
45 8
                return $this->formatted;
46
        }
47
    }
48
49 41
    protected function setUpFormatted()
50
    {
51 41
        $this->formatted                = new \stdClass();
52 41
        $this->formatted->{self::$ID32} = null;
53 41
        $this->formatted->{self::$ID64} = null;
54 41
        $this->formatted->{self::$ID3}  = null;
55 41
    }
56
57 27
    private function convertToAll($id)
58
    {
59 27
        list($type, $matches) = $this->determineIDType($id);
60
61 27
        $this->getRawValue($id, $type, $matches);
62
63
        // Convert to each type
64 27
        $this->convertToID32();
65 27
        $this->convertToID64();
66 27
        $this->convertToID3();
67
68 27
        return $this->formatted;
69
    }
70
71 27
    private function convertToID32()
72
    {
73 27
        $z                              = bcdiv($this->rawValue, '2', 0);
74 27
        $y                              = bcmul($z, '2', 0);
75 27
        $y                              = bcsub($this->rawValue, $y, 0);
76 27
        $formatted                      = "STEAM_1:$y:$z";
77 27
        $this->formatted->{self::$ID32} = $formatted;
78 27
    }
79
80 27
    private function convertToID64()
81
    {
82 27
        $formatted                      = bcadd($this->rawValue, self::$id64Base, 0);
83 27
        $this->formatted->{self::$ID64} = $formatted;
84 27
    }
85
86 27
    private function convertToID3()
87
    {
88 27
        $formatted                     = "[U:1:$this->rawValue]";
89 27
        $this->formatted->{self::$ID3} = $formatted;
90 27
    }
91
92 27
    private function determineIDType($id)
93
    {
94 27
        $id = trim($id);
95
96 27
        if (preg_match('/^STEAM_[0-1]:([0-1]):([0-9]+)$/', $id, $matches)) {
97 1
            return ['ID32', $matches];
98
        }
99 27
        if (preg_match('/^[0-9]+$/', $id)) {
100 27
            return ['ID64', null];
101
        }
102
        if (preg_match('/^\[U:1:([0-9]+)\]$/', $id, $matches)) {
103
            return ['ID3', $matches];
104
        }
105
106
        throw new UnrecognizedId('Id [' . $id . '] is not recognized as a steam id.');
107
    }
108
109
    /**
110
     * Get a raw value from any type of steam id
111
     *
112
     * @param $id
113
     * @param $type
114
     * @param $matches
115
     */
116 27
    private function getRawValue($id, $type, $matches)
117
    {
118 27
        switch ($type) {
119 27
            case 'ID32':
120 1
                $this->rawValue = bcmul($matches[2], '2', 0);
121 1
                $this->rawValue = bcadd($this->rawValue, $matches[1], 0);
122
123 1
                $this->formatted->{self::$ID32} = $id;
124
125 1
                break;
126 27
            case 'ID64':
127 27
                $this->rawValue = bcsub($id, self::$id64Base, 0);
128
129 27
                $this->formatted->{self::$ID64} = $id;
130
131 27
                break;
132
            case 'ID3':
133
                $this->rawValue = $matches[1];
134
135
                $this->formatted->{self::$ID3} = $id;
136
137
                break;
138
        }
139 27
    }
140
141
}
142