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