Subversion-Projekte lars-tiefland.laravel_shop

Revision

Revision 148 | Ganze Datei anzeigen | Leerzeichen ignorieren | Details | Blame | Letzte Änderung | Log anzeigen | RSS feed

Revision 148 Revision 399
Zeile 46... Zeile 46...
46
     * - floating point numbers are converted to a string then parsed as such
46
     * - floating point numbers are converted to a string then parsed as such
47
     * - strings containing a `/` character are returned as BigRational
47
     * - strings containing a `/` character are returned as BigRational
48
     * - strings containing a `.` character or using an exponential notation are returned as BigDecimal
48
     * - strings containing a `.` character or using an exponential notation are returned as BigDecimal
49
     * - strings containing only digits with an optional leading `+` or `-` sign are returned as BigInteger
49
     * - strings containing only digits with an optional leading `+` or `-` sign are returned as BigInteger
50
     *
50
     *
51
     * @param BigNumber|int|float|string $value
-
 
52
     *
-
 
53
     * @return BigNumber
-
 
54
     *
-
 
55
     * @throws NumberFormatException   If the format of the number is not valid.
51
     * @throws NumberFormatException   If the format of the number is not valid.
56
     * @throws DivisionByZeroException If the value represents a rational number with a denominator of zero.
52
     * @throws DivisionByZeroException If the value represents a rational number with a denominator of zero.
57
     *
53
     *
58
     * @psalm-pure
54
     * @psalm-pure
59
     */
55
     */
60
    public static function of($value) : BigNumber
56
    public static function of(BigNumber|int|float|string $value) : BigNumber
61
    {
57
    {
62
        if ($value instanceof BigNumber) {
58
        if ($value instanceof BigNumber) {
63
            return $value;
59
            return $value;
64
        }
60
        }
Zeile 65... Zeile 61...
65
 
61
 
66
        if (\is_int($value)) {
62
        if (\is_int($value)) {
67
            return new BigInteger((string) $value);
63
            return new BigInteger((string) $value);
Zeile 68... Zeile -...
68
        }
-
 
69
 
64
        }
Zeile 70... Zeile 65...
70
        /** @psalm-suppress RedundantCastGivenDocblockType We cannot trust the untyped $value here! */
65
 
71
        $value = \is_float($value) ? self::floatToString($value) : (string) $value;
66
        $value = \is_float($value) ? self::floatToString($value) : $value;
72
 
67
 
73
        $throw = static function() use ($value) : void {
68
        $throw = static function() use ($value) : void {
Zeile 151... Zeile 146...
151
    /**
146
    /**
152
     * Safely converts float to string, avoiding locale-dependent issues.
147
     * Safely converts float to string, avoiding locale-dependent issues.
153
     *
148
     *
154
     * @see https://github.com/brick/math/pull/20
149
     * @see https://github.com/brick/math/pull/20
155
     *
150
     *
156
     * @param float $float
-
 
157
     *
-
 
158
     * @return string
-
 
159
     *
-
 
160
     * @psalm-pure
151
     * @psalm-pure
161
     * @psalm-suppress ImpureFunctionCall
152
     * @psalm-suppress ImpureFunctionCall
162
     */
153
     */
163
    private static function floatToString(float $float) : string
154
    private static function floatToString(float $float) : string
164
    {
155
    {
Zeile 171... Zeile 162...
171
 
162
 
172
        return $result;
163
        return $result;
Zeile 173... Zeile 164...
173
    }
164
    }
174
 
165
 
175
    /**
166
    /**
176
     * Proxy method to access protected constructors from sibling classes.
167
     * Proxy method to access BigInteger's protected constructor from sibling classes.
-
 
168
     *
-
 
169
     * @internal
-
 
170
     * @psalm-pure
-
 
171
     */
-
 
172
    protected function newBigInteger(string $value) : BigInteger
-
 
173
    {
-
 
174
        return new BigInteger($value);
-
 
175
    }
-
 
176
 
177
     *
177
    /**
-
 
178
     * Proxy method to access BigDecimal's protected constructor from sibling classes.
-
 
179
     *
-
 
180
     * @internal
178
     * @internal
181
     * @psalm-pure
179
     *
182
     */
180
     * @param mixed ...$args The arguments to the constructor.
183
    protected function newBigDecimal(string $value, int $scale = 0) : BigDecimal
-
 
184
    {
-
 
185
        return new BigDecimal($value, $scale);
-
 
186
    }
-
 
187
 
181
     *
188
    /**
-
 
189
     * Proxy method to access BigRational's protected constructor from sibling classes.
182
     * @return static
190
     *
183
     *
-
 
184
     * @psalm-pure
-
 
185
     * @psalm-suppress TooManyArguments
191
     * @internal
186
     * @psalm-suppress UnsafeInstantiation
192
     * @psalm-pure
187
     */
193
     */
188
    protected static function create(... $args) : BigNumber
194
    protected function newBigRational(BigInteger $numerator, BigInteger $denominator, bool $checkDenominator) : BigRational
189
    {
195
    {
Zeile 190... Zeile 196...
190
        return new static(... $args);
196
        return new BigRational($numerator, $denominator, $checkDenominator);
191
    }
197
    }
192
 
198
 
193
    /**
199
    /**
194
     * Returns the minimum of the given values.
200
     * Returns the minimum of the given values.
195
     *
201
     *
196
     * @param BigNumber|int|float|string ...$values The numbers to compare. All the numbers need to be convertible
-
 
197
     *                                              to an instance of the class this method is called on.
-
 
198
     *
202
     * @param BigNumber|int|float|string ...$values The numbers to compare. All the numbers need to be convertible
199
     * @return static The minimum value.
203
     *                                              to an instance of the class this method is called on.
200
     *
204
     *
201
     * @throws \InvalidArgumentException If no values are given.
205
     * @throws \InvalidArgumentException If no values are given.
202
     * @throws MathException             If an argument is not valid.
206
     * @throws MathException             If an argument is not valid.
203
     *
207
     *
204
     * @psalm-suppress LessSpecificReturnStatement
208
     * @psalm-suppress LessSpecificReturnStatement
205
     * @psalm-suppress MoreSpecificReturnType
209
     * @psalm-suppress MoreSpecificReturnType
206
     * @psalm-pure
210
     * @psalm-pure
207
     */
211
     */
Zeile 208... Zeile 212...
208
    public static function min(...$values) : BigNumber
212
    public static function min(BigNumber|int|float|string ...$values) : static
209
    {
213
    {
Zeile 228... Zeile 232...
228
     * Returns the maximum of the given values.
232
     * Returns the maximum of the given values.
229
     *
233
     *
230
     * @param BigNumber|int|float|string ...$values The numbers to compare. All the numbers need to be convertible
234
     * @param BigNumber|int|float|string ...$values The numbers to compare. All the numbers need to be convertible
231
     *                                              to an instance of the class this method is called on.
235
     *                                              to an instance of the class this method is called on.
232
     *
236
     *
233
     * @return static The maximum value.
-
 
234
     *
-
 
235
     * @throws \InvalidArgumentException If no values are given.
237
     * @throws \InvalidArgumentException If no values are given.
236
     * @throws MathException             If an argument is not valid.
238
     * @throws MathException             If an argument is not valid.
237
     *
239
     *
238
     * @psalm-suppress LessSpecificReturnStatement
240
     * @psalm-suppress LessSpecificReturnStatement
239
     * @psalm-suppress MoreSpecificReturnType
241
     * @psalm-suppress MoreSpecificReturnType
240
     * @psalm-pure
242
     * @psalm-pure
241
     */
243
     */
242
    public static function max(...$values) : BigNumber
244
    public static function max(BigNumber|int|float|string ...$values) : static
243
    {
245
    {
244
        $max = null;
246
        $max = null;
Zeile 245... Zeile 247...
245
 
247
 
246
        foreach ($values as $value) {
248
        foreach ($values as $value) {
Zeile 262... Zeile 264...
262
     * Returns the sum of the given values.
264
     * Returns the sum of the given values.
263
     *
265
     *
264
     * @param BigNumber|int|float|string ...$values The numbers to add. All the numbers need to be convertible
266
     * @param BigNumber|int|float|string ...$values The numbers to add. All the numbers need to be convertible
265
     *                                              to an instance of the class this method is called on.
267
     *                                              to an instance of the class this method is called on.
266
     *
268
     *
267
     * @return static The sum.
-
 
268
     *
-
 
269
     * @throws \InvalidArgumentException If no values are given.
269
     * @throws \InvalidArgumentException If no values are given.
270
     * @throws MathException             If an argument is not valid.
270
     * @throws MathException             If an argument is not valid.
271
     *
271
     *
272
     * @psalm-suppress LessSpecificReturnStatement
-
 
273
     * @psalm-suppress MoreSpecificReturnType
-
 
274
     * @psalm-pure
272
     * @psalm-pure
275
     */
273
     */
276
    public static function sum(...$values) : BigNumber
274
    public static function sum(BigNumber|int|float|string ...$values) : static
277
    {
275
    {
278
        /** @var BigNumber|null $sum */
276
        /** @var static|null $sum */
279
        $sum = null;
277
        $sum = null;
Zeile 280... Zeile 278...
280
 
278
 
281
        foreach ($values as $value) {
279
        foreach ($values as $value) {
Zeile 297... Zeile 295...
297
     * @todo This could be better resolved by creating an abstract protected method in BigNumber, and leaving to
295
     * @todo This could be better resolved by creating an abstract protected method in BigNumber, and leaving to
298
     *       concrete classes the responsibility to perform the addition themselves or delegate it to the given number,
296
     *       concrete classes the responsibility to perform the addition themselves or delegate it to the given number,
299
     *       depending on their ability to perform the operation. This will also require a version bump because we're
297
     *       depending on their ability to perform the operation. This will also require a version bump because we're
300
     *       potentially breaking custom BigNumber implementations (if any...)
298
     *       potentially breaking custom BigNumber implementations (if any...)
301
     *
299
     *
302
     * @param BigNumber $a
-
 
303
     * @param BigNumber $b
-
 
304
     *
-
 
305
     * @return BigNumber
-
 
306
     *
-
 
307
     * @psalm-pure
300
     * @psalm-pure
308
     */
301
     */
309
    private static function add(BigNumber $a, BigNumber $b) : BigNumber
302
    private static function add(BigNumber $a, BigNumber $b) : BigNumber
310
    {
303
    {
311
        if ($a instanceof BigRational) {
304
        if ($a instanceof BigRational) {
Zeile 332... Zeile 325...
332
    /**
325
    /**
333
     * Removes optional leading zeros and + sign from the given number.
326
     * Removes optional leading zeros and + sign from the given number.
334
     *
327
     *
335
     * @param string $number The number, validated as a non-empty string of digits with optional leading sign.
328
     * @param string $number The number, validated as a non-empty string of digits with optional leading sign.
336
     *
329
     *
337
     * @return string
-
 
338
     *
-
 
339
     * @psalm-pure
330
     * @psalm-pure
340
     */
331
     */
341
    private static function cleanUp(string $number) : string
332
    private static function cleanUp(string $number) : string
342
    {
333
    {
343
        $firstChar = $number[0];
334
        $firstChar = $number[0];
Zeile 359... Zeile 350...
359
        return $number;
350
        return $number;
360
    }
351
    }
Zeile 361... Zeile 352...
361
 
352
 
362
    /**
353
    /**
363
     * Checks if this number is equal to the given one.
-
 
364
     *
-
 
365
     * @param BigNumber|int|float|string $that
-
 
366
     *
-
 
367
     * @return bool
354
     * Checks if this number is equal to the given one.
368
     */
355
     */
369
    public function isEqualTo($that) : bool
356
    public function isEqualTo(BigNumber|int|float|string $that) : bool
370
    {
357
    {
371
        return $this->compareTo($that) === 0;
358
        return $this->compareTo($that) === 0;
Zeile 372... Zeile 359...
372
    }
359
    }
373
 
360
 
374
    /**
-
 
375
     * Checks if this number is strictly lower than the given one.
-
 
376
     *
-
 
377
     * @param BigNumber|int|float|string $that
-
 
378
     *
361
    /**
379
     * @return bool
362
     * Checks if this number is strictly lower than the given one.
380
     */
363
     */
381
    public function isLessThan($that) : bool
364
    public function isLessThan(BigNumber|int|float|string $that) : bool
382
    {
365
    {
Zeile 383... Zeile 366...
383
        return $this->compareTo($that) < 0;
366
        return $this->compareTo($that) < 0;
384
    }
367
    }
385
 
-
 
386
    /**
-
 
387
     * Checks if this number is lower than or equal to the given one.
-
 
388
     *
-
 
389
     * @param BigNumber|int|float|string $that
368
 
390
     *
369
    /**
391
     * @return bool
370
     * Checks if this number is lower than or equal to the given one.
392
     */
371
     */
393
    public function isLessThanOrEqualTo($that) : bool
372
    public function isLessThanOrEqualTo(BigNumber|int|float|string $that) : bool
Zeile 394... Zeile 373...
394
    {
373
    {
395
        return $this->compareTo($that) <= 0;
374
        return $this->compareTo($that) <= 0;
396
    }
-
 
397
 
-
 
398
    /**
-
 
399
     * Checks if this number is strictly greater than the given one.
-
 
400
     *
375
    }
401
     * @param BigNumber|int|float|string $that
376
 
402
     *
377
    /**
403
     * @return bool
378
     * Checks if this number is strictly greater than the given one.
404
     */
379
     */
Zeile 405... Zeile 380...
405
    public function isGreaterThan($that) : bool
380
    public function isGreaterThan(BigNumber|int|float|string $that) : bool
406
    {
381
    {
407
        return $this->compareTo($that) > 0;
-
 
408
    }
-
 
409
 
-
 
410
    /**
-
 
411
     * Checks if this number is greater than or equal to the given one.
382
        return $this->compareTo($that) > 0;
412
     *
383
    }
413
     * @param BigNumber|int|float|string $that
384
 
414
     *
385
    /**
415
     * @return bool
386
     * Checks if this number is greater than or equal to the given one.
Zeile 416... Zeile 387...
416
     */
387
     */
417
    public function isGreaterThanOrEqualTo($that) : bool
388
    public function isGreaterThanOrEqualTo(BigNumber|int|float|string $that) : bool
418
    {
-
 
419
        return $this->compareTo($that) >= 0;
-
 
420
    }
389
    {
421
 
390
        return $this->compareTo($that) >= 0;
422
    /**
391
    }
423
     * Checks if this number equals zero.
392
 
424
     *
393
    /**
Zeile 425... Zeile 394...
425
     * @return bool
394
     * Checks if this number equals zero.
426
     */
395
     */
427
    public function isZero() : bool
-
 
428
    {
-
 
429
        return $this->getSign() === 0;
396
    public function isZero() : bool
430
    }
397
    {
431
 
398
        return $this->getSign() === 0;
432
    /**
399
    }
433
     * Checks if this number is strictly negative.
400
 
Zeile 434... Zeile 401...
434
     *
401
    /**
435
     * @return bool
402
     * Checks if this number is strictly negative.
436
     */
-
 
437
    public function isNegative() : bool
-
 
438
    {
403
     */
439
        return $this->getSign() < 0;
404
    public function isNegative() : bool
440
    }
405
    {
441
 
406
        return $this->getSign() < 0;
442
    /**
407
    }
Zeile 443... Zeile 408...
443
     * Checks if this number is negative or zero.
408
 
444
     *
409
    /**
445
     * @return bool
-
 
446
     */
-
 
447
    public function isNegativeOrZero() : bool
410
     * Checks if this number is negative or zero.
448
    {
411
     */
449
        return $this->getSign() <= 0;
412
    public function isNegativeOrZero() : bool
450
    }
413
    {
451
 
414
        return $this->getSign() <= 0;
Zeile 452... Zeile 415...
452
    /**
415
    }
453
     * Checks if this number is strictly positive.
416
 
454
     *
-
 
455
     * @return bool
-
 
456
     */
417
    /**
457
    public function isPositive() : bool
418
     * Checks if this number is strictly positive.
458
    {
419
     */
459
        return $this->getSign() > 0;
420
    public function isPositive() : bool
460
    }
421
    {
Zeile 477... Zeile 438...
477
    abstract public function getSign() : int;
438
    abstract public function getSign() : int;
Zeile 478... Zeile 439...
478
 
439
 
479
    /**
440
    /**
480
     * Compares this number to the given one.
441
     * Compares this number to the given one.
481
     *
-
 
482
     * @param BigNumber|int|float|string $that
-
 
483
     *
442
     *
484
     * @return int [-1,0,1] If `$this` is lower than, equal to, or greater than `$that`.
443
     * @return int [-1,0,1] If `$this` is lower than, equal to, or greater than `$that`.
485
     *
444
     *
486
     * @throws MathException If the number is not valid.
445
     * @throws MathException If the number is not valid.
487
     */
446
     */
Zeile 488... Zeile 447...
488
    abstract public function compareTo($that) : int;
447
    abstract public function compareTo(BigNumber|int|float|string $that) : int;
489
 
448
 
490
    /**
449
    /**
491
     * Converts this number to a BigInteger.
-
 
492
     *
-
 
493
     * @return BigInteger The converted number.
450
     * Converts this number to a BigInteger.
494
     *
451
     *
495
     * @throws RoundingNecessaryException If this number cannot be converted to a BigInteger without rounding.
452
     * @throws RoundingNecessaryException If this number cannot be converted to a BigInteger without rounding.
Zeile 496... Zeile 453...
496
     */
453
     */
497
    abstract public function toBigInteger() : BigInteger;
454
    abstract public function toBigInteger() : BigInteger;
498
 
455
 
499
    /**
-
 
500
     * Converts this number to a BigDecimal.
-
 
501
     *
456
    /**
502
     * @return BigDecimal The converted number.
457
     * Converts this number to a BigDecimal.
503
     *
458
     *
Zeile 504... Zeile 459...
504
     * @throws RoundingNecessaryException If this number cannot be converted to a BigDecimal without rounding.
459
     * @throws RoundingNecessaryException If this number cannot be converted to a BigDecimal without rounding.
505
     */
460
     */
506
    abstract public function toBigDecimal() : BigDecimal;
-
 
507
 
-
 
508
    /**
461
    abstract public function toBigDecimal() : BigDecimal;
509
     * Converts this number to a BigRational.
462
 
Zeile 510... Zeile 463...
510
     *
463
    /**
511
     * @return BigRational The converted number.
464
     * Converts this number to a BigRational.
512
     */
465
     */
513
    abstract public function toBigRational() : BigRational;
466
    abstract public function toBigRational() : BigRational;
514
 
467
 
515
    /**
468
    /**
516
     * Converts this number to a BigDecimal with the given scale, using rounding if necessary.
-
 
517
     *
-
 
518
     * @param int $scale        The scale of the resulting `BigDecimal`.
469
     * Converts this number to a BigDecimal with the given scale, using rounding if necessary.
519
     * @param int $roundingMode A `RoundingMode` constant.
470
     *
520
     *
471
     * @param int $scale        The scale of the resulting `BigDecimal`.
521
     * @return BigDecimal
472
     * @param int $roundingMode A `RoundingMode` constant.
Zeile 529... Zeile 480...
529
     * Returns the exact value of this number as a native integer.
480
     * Returns the exact value of this number as a native integer.
530
     *
481
     *
531
     * If this number cannot be converted to a native integer without losing precision, an exception is thrown.
482
     * If this number cannot be converted to a native integer without losing precision, an exception is thrown.
532
     * Note that the acceptable range for an integer depends on the platform and differs for 32-bit and 64-bit.
483
     * Note that the acceptable range for an integer depends on the platform and differs for 32-bit and 64-bit.
533
     *
484
     *
534
     * @return int The converted value.
-
 
535
     *
-
 
536
     * @throws MathException If this number cannot be exactly converted to a native integer.
485
     * @throws MathException If this number cannot be exactly converted to a native integer.
537
     */
486
     */
538
    abstract public function toInt() : int;
487
    abstract public function toInt() : int;
Zeile 539... Zeile 488...
539
 
488
 
Zeile 543... Zeile 492...
543
     * Note that this method can discard information as the precision of a floating-point value
492
     * Note that this method can discard information as the precision of a floating-point value
544
     * is inherently limited.
493
     * is inherently limited.
545
     *
494
     *
546
     * If the number is greater than the largest representable floating point number, positive infinity is returned.
495
     * If the number is greater than the largest representable floating point number, positive infinity is returned.
547
     * If the number is less than the smallest representable floating point number, negative infinity is returned.
496
     * If the number is less than the smallest representable floating point number, negative infinity is returned.
548
     *
-
 
549
     * @return float The converted value.
-
 
550
     */
497
     */
551
    abstract public function toFloat() : float;
498
    abstract public function toFloat() : float;
Zeile 552... Zeile 499...
552
 
499
 
553
    /**
500
    /**
554
     * Returns a string representation of this number.
501
     * Returns a string representation of this number.
555
     *
502
     *
556
     * The output of this method can be parsed by the `of()` factory method;
503
     * The output of this method can be parsed by the `of()` factory method;
557
     * this will yield an object equal to this one, without any information loss.
-
 
558
     *
-
 
559
     * @return string
504
     * this will yield an object equal to this one, without any information loss.
560
     */
505
     */
Zeile 561... Zeile -...
561
    abstract public function __toString() : string;
-
 
562
 
-
 
563
    /**
-
 
564
     * {@inheritdoc}
506
    abstract public function __toString() : string;
565
     */
507
 
566
    public function jsonSerialize() : string
508
    public function jsonSerialize() : string
567
    {
509
    {
568
        return $this->__toString();
510
        return $this->__toString();