Completed
Pull Request — master (#110)
by
unknown
01:32
created

SteamId::getRawValue()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 24

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 0
Metric Value
dl 0
loc 24
ccs 0
cts 16
cp 0
rs 9.536
c 0
b 0
f 0
cc 4
nc 4
nop 3
crap 20
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
    public function convertId($id, $format = null)
28
    {
29
        $this->convertToAll($id);
30
31
        switch ($format) {
32
            case 'ID32':
33
            case 'id32':
34
            case 32:
35
                return $this->formatted->{self::$ID32};
36
            case 'ID64':
37
            case 'id64':
38
            case 64:
39
                return $this->formatted->{self::$ID64};
40
            case 'ID3':
41
            case 'id3':
42
            case 3:
43
                return $this->formatted->{self::$ID3};
44
            default:
45
                return $this->formatted;
46
        }
47
    }
48
49
    protected function setUpFormatted()
50
    {
51
        $this->formatted                = new \stdClass();
52
        $this->formatted->{self::$ID32} = null;
53
        $this->formatted->{self::$ID64} = null;
54
        $this->formatted->{self::$ID3}  = null;
55
    }
56
57
    private function convertToAll($id)
58
    {
59
        list($type, $matches) = $this->determineIDType($id);
60
61
        $this->getRawValue($id, $type, $matches);
62
63
        // Convert to each type
64
        $this->convertToID32();
65
        $this->convertToID64();
66
        $this->convertToID3();
67
68
        return $this->formatted;
69
    }
70
71
    private function convertToID32()
72
    {
73
        $z                              = bcdiv($this->rawValue, '2', 0);
74
        $y                              = bcmul($z, '2', 0);
75
        $y                              = bcsub($this->rawValue, $y, 0);
76
        $formatted                      = "STEAM_1:$y:$z";
77
        $this->formatted->{self::$ID32} = $formatted;
78
    }
79
80
    private function convertToID64()
81
    {
82
        $formatted                      = bcadd($this->rawValue, self::$id64Base, 0);
83
        $this->formatted->{self::$ID64} = $formatted;
84
    }
85
86
    private function convertToID3()
87
    {
88
        $formatted                     = "[U:1:$this->rawValue]";
89
        $this->formatted->{self::$ID3} = $formatted;
90
    }
91
92
    private function determineIDType($id)
93
    {
94
        $id = trim($id);
95
96
        if (preg_match('/^STEAM_[0-1]:([0-1]):([0-9]+)$/', $id, $matches)) {
97
            return ['ID32', $matches];
98
        }
99
        if (preg_match('/^[0-9]+$/', $id)) {
100
            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
    private function getRawValue($id, $type, $matches)
117
    {
118
        switch ($type) {
119
            case 'ID32':
120
                $this->rawValue = bcmul($matches[2], '2', 0);
121
                $this->rawValue = bcadd($this->rawValue, $matches[1], 0);
122
123
                $this->formatted->{self::$ID32} = $id;
124
125
                break;
126
            case 'ID64':
127
                $this->rawValue = bcsub($id, self::$id64Base, 0);
128
129
                $this->formatted->{self::$ID64} = $id;
130
131
                break;
132
            case 'ID3':
133
                $this->rawValue = $matches[1];
134
135
                $this->formatted->{self::$ID3} = $id;
136
137
                break;
138
        }
139
    }
140
141
}
142