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
|
47 |
|
protected function setUpFormatted() |
50
|
|
|
{ |
51
|
47 |
|
$this->formatted = new \stdClass(); |
52
|
47 |
|
$this->formatted->{self::$ID32} = null; |
53
|
47 |
|
$this->formatted->{self::$ID64} = null; |
54
|
47 |
|
$this->formatted->{self::$ID3} = null; |
55
|
47 |
|
} |
56
|
|
|
|
57
|
30 |
|
private function convertToAll($id) |
58
|
|
|
{ |
59
|
30 |
|
list($type, $matches) = $this->determineIDType($id); |
60
|
|
|
|
61
|
30 |
|
$this->getRawValue($id, $type, $matches); |
62
|
|
|
|
63
|
|
|
// Convert to each type |
64
|
30 |
|
$this->convertToID32(); |
65
|
30 |
|
$this->convertToID64(); |
66
|
30 |
|
$this->convertToID3(); |
67
|
|
|
|
68
|
30 |
|
return $this->formatted; |
69
|
|
|
} |
70
|
|
|
|
71
|
30 |
|
private function convertToID32() |
72
|
|
|
{ |
73
|
30 |
|
$z = bcdiv($this->rawValue, '2', 0); |
74
|
30 |
|
$y = bcmul($z, '2', 0); |
75
|
30 |
|
$y = bcsub($this->rawValue, $y, 0); |
76
|
30 |
|
$formatted = "STEAM_1:$y:$z"; |
77
|
30 |
|
$this->formatted->{self::$ID32} = $formatted; |
78
|
30 |
|
} |
79
|
|
|
|
80
|
30 |
|
private function convertToID64() |
81
|
|
|
{ |
82
|
30 |
|
$formatted = bcadd($this->rawValue, self::$id64Base, 0); |
83
|
30 |
|
$this->formatted->{self::$ID64} = $formatted; |
84
|
30 |
|
} |
85
|
|
|
|
86
|
30 |
|
private function convertToID3() |
87
|
|
|
{ |
88
|
30 |
|
$formatted = "[U:1:$this->rawValue]"; |
89
|
30 |
|
$this->formatted->{self::$ID3} = $formatted; |
90
|
30 |
|
} |
91
|
|
|
|
92
|
30 |
|
private function determineIDType($id) |
93
|
|
|
{ |
94
|
30 |
|
$id = trim($id); |
95
|
|
|
|
96
|
30 |
|
if (preg_match('/^STEAM_[0-1]:([0-1]):([0-9]+)$/', $id, $matches)) { |
97
|
1 |
|
return ['ID32', $matches]; |
98
|
|
|
} |
99
|
30 |
|
if (preg_match('/^[0-9]+$/', $id)) { |
100
|
30 |
|
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
|
30 |
|
private function getRawValue($id, $type, $matches) |
117
|
|
|
{ |
118
|
30 |
|
switch ($type) { |
119
|
30 |
|
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
|
30 |
|
case 'ID64': |
127
|
30 |
|
$this->rawValue = bcsub($id, self::$id64Base, 0); |
128
|
|
|
|
129
|
30 |
|
$this->formatted->{self::$ID64} = $id; |
130
|
|
|
|
131
|
30 |
|
break; |
132
|
|
|
case 'ID3': |
133
|
|
|
$this->rawValue = $matches[1]; |
134
|
|
|
|
135
|
|
|
$this->formatted->{self::$ID3} = $id; |
136
|
|
|
|
137
|
|
|
break; |
138
|
|
|
} |
139
|
30 |
|
} |
140
|
|
|
|
141
|
|
|
} |
142
|
|
|
|