Skip to content

Commit edffaf9

Browse files
committed
Revert "Remove deprecated BigInteger::parse()"
This reverts commit 6c37468. Re-introducing - this should not have been removed in the 0.8 release cycle.
1 parent 90c46f0 commit edffaf9

File tree

2 files changed

+231
-0
lines changed

2 files changed

+231
-0
lines changed

src/BigInteger.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,30 @@ public static function of($value) : BigNumber
5757
return parent::of($value)->toBigInteger();
5858
}
5959

60+
/**
61+
* Parses a string containing an integer in an arbitrary base.
62+
*
63+
* @deprecated will be removed in version 0.9 - use fromBase() instead
64+
*
65+
* The string can optionally be prefixed with the `+` or `-` sign.
66+
* For bases greater than 10, both uppercase and lowercase letters are allowed.
67+
*
68+
* @param string $number The number to parse.
69+
* @param int $base The base of the number, between 2 and 36.
70+
*
71+
* @return BigInteger
72+
*
73+
* @throws \InvalidArgumentException If the number is invalid or the base is out of range.
74+
*/
75+
public static function parse(string $number, int $base = 10) : BigInteger
76+
{
77+
try {
78+
return self::fromBase($number, $base);
79+
} catch (NumberFormatException $e) {
80+
throw new \InvalidArgumentException($e->getMessage(), 0, $e);
81+
}
82+
}
83+
6084
/**
6185
* Creates a number from a string in a given base.
6286
*

tests/BigIntegerTest.php

Lines changed: 207 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,213 @@ public function providerOfNonConvertibleValueThrowsException() : array
145145
];
146146
}
147147

148+
/**
149+
* @dataProvider providerParse
150+
*
151+
* @param string $number The number to create.
152+
* @param int $base The base of the number.
153+
* @param string $expected The expected result in base 10.
154+
*/
155+
public function testParse($number, $base, $expected)
156+
{
157+
$this->assertBigIntegerEquals($expected, BigInteger::parse($number, $base));
158+
}
159+
160+
/**
161+
* @return array
162+
*/
163+
public function providerParse()
164+
{
165+
return [
166+
['0', 10, '0'],
167+
['-0', 10, '0'],
168+
['+0', 10, '0'],
169+
['00', 16, '0'],
170+
['-00', 16, '0'],
171+
['+00', 16, '0'],
172+
173+
['1', 10, '1'],
174+
['-1', 10, '-1'],
175+
['+1', 10, '1'],
176+
['01', 8, '1'],
177+
['-01', 8, '-1'],
178+
['+01', 8, '1'],
179+
180+
['123', 10, '123'],
181+
['+456', 10, '456'],
182+
['-789', 10, '-789'],
183+
['0123', 10, '123'],
184+
['+0456', 10, '456'],
185+
['-0789', 10, '-789'],
186+
187+
['123', 11, '146'],
188+
['+456', 11, '545'],
189+
['-789', 11, '-944'],
190+
['0123', 11, '146'],
191+
['+0456', 11, '545'],
192+
['-0789', 11, '-944'],
193+
194+
['110011001100110011001111', 36, '640998479760579495168036691627608949'],
195+
['110011001100110011001111', 35, '335582856048758779730579523833856636'],
196+
['110011001100110011001111', 34, '172426711023004493064981145981549295'],
197+
['110011001100110011001111', 33, '86853227285668653965326574185738990'],
198+
['110011001100110011001111', 32, '42836489934972583913564073319498785'],
199+
['110011001100110011001111', 31, '20658924711984480538771889603666144'],
200+
['110011001100110011001111', 30, '9728140488839986222205212599027931'],
201+
['110011001100110011001111', 29, '4465579470019956787945275674107410'],
202+
['110011001100110011001111', 28, '1994689924537781753408144504465645'],
203+
['110011001100110011001111', 27, '865289950909412968716094193925700'],
204+
['110011001100110011001111', 26, '363729369583879309352831568000039'],
205+
['110011001100110011001111', 25, '147793267388865354156500488297526'],
206+
['110011001100110011001111', 24, '57888012016107577099138793486425'],
207+
['110011001100110011001111', 23, '21788392294523974761749372677800'],
208+
['110011001100110011001111', 22, '7852874701996329566765721637715'],
209+
['110011001100110011001111', 21, '2699289081943123258094476428634'],
210+
['110011001100110011001111', 20, '880809345058406615041344008421'],
211+
['110011001100110011001111', 19, '271401690926468032718781859340'],
212+
['110011001100110011001111', 18, '78478889737009209699633503455'],
213+
['110011001100110011001111', 17, '21142384915931646646976872830'],
214+
['110011001100110011001111', 16, '5261325448418072742917574929'],
215+
['110011001100110011001111', 15, '1197116069565850925807253616'],
216+
['110011001100110011001111', 14, '245991074299834917455374155'],
217+
['110011001100110011001111', 13, '44967318722190498361960610'],
218+
['110011001100110011001111', 12, '7177144825886069940574045'],
219+
['110011001100110011001111', 11, '976899716207148313491924'],
220+
['110011001100110011001111', 10, '110011001100110011001111'],
221+
['110011001100110011001111', 9, '9849210196991880028870'],
222+
['110011001100110011001111', 8, '664244955832213832265'],
223+
['110011001100110011001111', 7, '31291601125492514360'],
224+
['110011001100110011001111', 6, '922063395565287619'],
225+
['110011001100110011001111', 5, '14328039609468906'],
226+
['110011001100110011001111', 4, '88305875046485'],
227+
['110011001100110011001111', 3, '127093291420'],
228+
['110011001100110011001111', 2, '13421775'],
229+
230+
['ZyXwVuTsRqPoNmLkJiHgFeDcBa9876543210', 36, '106300512100105327644605138221229898724869759421181854980'],
231+
['YxWvUtSrQpOnMlKjIhGfEdCbA9876543210', 35, '1101553773143634726491620528194292510495517905608180485'],
232+
['XwVuTsRqPoNmLkJiHgFeDcBa9876543210', 34, '11745843093701610854378775891116314824081102660800418'],
233+
['WvUtSrQpOnMlKjIhGfEdCbA9876543210', 33, '128983956064237823710866404905431464703849549412368'],
234+
['VuTsRqPoNmLkJiHgFeDcBa9876543210', 32, '1459980823972598128486511383358617792788444579872'],
235+
['UtSrQpOnMlKjIhGfEdCbA9876543210', 31, '17050208381689099029767742314582582184093573615'],
236+
['TsRqPoNmLkJiHgFeDcBa9876543210', 30, '205646315052919334126040428061831153388822830'],
237+
['SrQpOnMlKjIhGfEdCbA9876543210', 29, '2564411043271974895869785066497940850811934'],
238+
['RqPoNmLkJiHgFeDcBa9876543210', 28, '33100056003358651440264672384704297711484'],
239+
['QpOnMlKjIhGfEdCbA9876543210', 27, '442770531899482980347734468443677777577'],
240+
['PoNmLkJiHgFeDcBa9876543210', 26, '6146269788878825859099399609538763450'],
241+
['OnMlKjIhGfEdCbA9876543210', 25, '88663644327703473714387251271141900'],
242+
['NmLkJiHgFeDcBa9876543210', 24, '1331214537196502869015340298036888'],
243+
['MlKjIhGfEdCbA9876543210', 23, '20837326537038308910317109288851'],
244+
['LkJiHgFeDcBa9876543210', 22, '340653664490377789692799452102'],
245+
['KjIhGfEdCbA9876543210', 21, '5827980550840017565077671610'],
246+
['JiHgFeDcBa9876543210', 20, '104567135734072022160664820'],
247+
['IhGfEdCbA9876543210', 19, '1972313422155189164466189'],
248+
['HgFeDcBa9876543210', 18, '39210261334551566857170'],
249+
['GfEdCbA9876543210', 17, '824008854613343261192'],
250+
['FeDcBa9876543210', 16, '18364758544493064720'],
251+
['EdCbA9876543210', 15, '435659737878916215'],
252+
['DcBa9876543210', 14, '11046255305880158'],
253+
['CbA9876543210', 13, '300771807240918'],
254+
['Ba9876543210', 12, '8842413667692'],
255+
['A9876543210', 11, '282458553905'],
256+
['9876543210', 10, '9876543210'],
257+
['876543210', 9, '381367044'],
258+
['76543210', 8, '16434824'],
259+
['6543210', 7, '800667'],
260+
['543210', 6, '44790'],
261+
['43210', 5, '2930'],
262+
['3210', 4, '228'],
263+
['210', 3, '21'],
264+
['10', 2, '2'],
265+
];
266+
}
267+
268+
/**
269+
* @dataProvider providerParseInvalidValueThrowsException
270+
* @expectedException \InvalidArgumentException
271+
*
272+
* @param string $value
273+
* @param int $base
274+
*/
275+
public function testParseInvalidValueThrowsException($value, $base)
276+
{
277+
BigInteger::parse($value, $base);
278+
}
279+
280+
/**
281+
* @return array
282+
*/
283+
public function providerParseInvalidValueThrowsException()
284+
{
285+
return [
286+
['', 10],
287+
[' ', 10],
288+
['+', 10],
289+
['-', 10],
290+
['1 ', 10],
291+
[' 1', 10],
292+
293+
['Z', 35],
294+
['y', 34],
295+
['X', 33],
296+
['w', 32],
297+
['V', 31],
298+
['u', 30],
299+
['T', 29],
300+
['s', 28],
301+
['R', 27],
302+
['q', 26],
303+
['P', 25],
304+
['o', 24],
305+
['N', 23],
306+
['m', 22],
307+
['L', 21],
308+
['k', 20],
309+
['J', 19],
310+
['i', 18],
311+
['H', 17],
312+
['g', 16],
313+
['F', 15],
314+
['e', 14],
315+
['D', 13],
316+
['c', 12],
317+
['B', 11],
318+
['a', 10],
319+
['9', 9],
320+
['8', 8],
321+
['7', 7],
322+
['6', 6],
323+
['5', 5],
324+
['4', 4],
325+
['3', 3],
326+
['2', 2]
327+
];
328+
}
329+
330+
/**
331+
* @dataProvider providerParseWithInvalidBaseThrowsException
332+
* @expectedException \InvalidArgumentException
333+
*
334+
* @param int $base
335+
*/
336+
public function testParseWithInvalidBaseThrowsException($base)
337+
{
338+
BigInteger::parse('0', $base);
339+
}
340+
341+
/**
342+
* @return array
343+
*/
344+
public function providerParseWithInvalidBaseThrowsException()
345+
{
346+
return [
347+
[-2],
348+
[-1],
349+
[0],
350+
[1],
351+
[37]
352+
];
353+
}
354+
148355
/**
149356
* @dataProvider providerFromBase
150357
*

0 commit comments

Comments
 (0)