Subversion-Projekte lars-tiefland.laravel_shop

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
148 lars 1
<?php
2
 
3
declare(strict_types=1);
4
 
5
namespace Faker\Core;
6
 
7
use Faker\Extension;
8
 
9
/**
10
 * @experimental This class is experimental and does not fall under our BC promise
11
 */
12
final class Number implements Extension\NumberExtension
13
{
14
    public function numberBetween(int $min = 0, int $max = 2147483647): int
15
    {
16
        $int1 = min($min, $max);
17
        $int2 = max($min, $max);
18
 
19
        return mt_rand($int1, $int2);
20
    }
21
 
22
    public function randomDigit(): int
23
    {
24
        return mt_rand(0, 9);
25
    }
26
 
27
    public function randomDigitNot(int $except): int
28
    {
29
        $result = self::numberBetween(0, 8);
30
 
31
        if ($result >= $except) {
32
            ++$result;
33
        }
34
 
35
        return $result;
36
    }
37
 
38
    public function randomDigitNotZero(): int
39
    {
40
        return mt_rand(1, 9);
41
    }
42
 
43
    public function randomFloat(?int $nbMaxDecimals = null, float $min = 0, ?float $max = null): float
44
    {
45
        if (null === $nbMaxDecimals) {
46
            $nbMaxDecimals = $this->randomDigit();
47
        }
48
 
49
        if (null === $max) {
50
            $max = $this->randomNumber();
51
 
52
            if ($min > $max) {
53
                $max = $min;
54
            }
55
        }
56
 
57
        if ($min > $max) {
58
            $tmp = $min;
59
            $min = $max;
60
            $max = $tmp;
61
        }
62
 
63
        return round($min + mt_rand() / mt_getrandmax() * ($max - $min), $nbMaxDecimals);
64
    }
65
 
66
    public function randomNumber(int $nbDigits = null, bool $strict = false): int
67
    {
68
        if (null === $nbDigits) {
69
            $nbDigits = $this->randomDigitNotZero();
70
        }
71
        $max = 10 ** $nbDigits - 1;
72
 
73
        if ($max > mt_getrandmax()) {
74
            throw new \InvalidArgumentException('randomNumber() can only generate numbers up to mt_getrandmax()');
75
        }
76
 
77
        if ($strict) {
78
            return mt_rand(10 ** ($nbDigits - 1), $max);
79
        }
80
 
81
        return mt_rand(0, $max);
82
    }
83
}