Subversion-Projekte lars-tiefland.laravel_shop

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
148 lars 1
<?php
2
 
3
namespace Faker\Extension;
4
 
5
/**
6
 * A class with some methods that may make building extensions easier.
7
 *
8
 * @experimental This class is experimental and does not fall under our BC promise
9
 */
10
final class Helper
11
{
12
    /**
13
     * Returns a random element from a passed array.
14
     */
15
    public static function randomElement(array $array)
16
    {
17
        if ($array === []) {
18
            return null;
19
        }
20
 
21
        return $array[array_rand($array, 1)];
22
    }
23
 
24
    /**
25
     * Replaces all hash sign ('#') occurrences with a random number
26
     * Replaces all percentage sign ('%') occurrences with a non-zero number.
27
     *
28
     * @param string $string String that needs to bet parsed
29
     */
30
    public static function numerify(string $string): string
31
    {
32
        // instead of using randomDigit() several times, which is slow,
33
        // count the number of hashes and generate once a large number
34
        $toReplace = [];
35
 
36
        if (($pos = strpos($string, '#')) !== false) {
37
            for ($i = $pos, $last = strrpos($string, '#', $pos) + 1; $i < $last; ++$i) {
38
                if ($string[$i] === '#') {
39
                    $toReplace[] = $i;
40
                }
41
            }
42
        }
43
 
44
        if ($nbReplacements = count($toReplace)) {
45
            $maxAtOnce = strlen((string) mt_getrandmax()) - 1;
46
            $numbers = '';
47
            $i = 0;
48
 
49
            while ($i < $nbReplacements) {
50
                $size = min($nbReplacements - $i, $maxAtOnce);
51
                $numbers .= str_pad((string) mt_rand(0, 10 ** $size - 1), $size, '0', STR_PAD_LEFT);
52
                $i += $size;
53
            }
54
 
55
            for ($i = 0; $i < $nbReplacements; ++$i) {
56
                $string[$toReplace[$i]] = $numbers[$i];
57
            }
58
        }
59
 
60
        return self::replaceWildcard($string, '%', static function () {
61
            return mt_rand(1, 9);
62
        });
63
    }
64
 
65
    /**
66
     * Replaces all question mark ('?') occurrences with a random letter.
67
     *
68
     * @param string $string String that needs to bet parsed
69
     */
70
    public static function lexify(string $string): string
71
    {
72
        return self::replaceWildcard($string, '?', static function () {
73
            return chr(mt_rand(97, 122));
74
        });
75
    }
76
 
77
    /**
78
     * Replaces hash signs ('#') and question marks ('?') with random numbers and letters
79
     * An asterisk ('*') is replaced with either a random number or a random letter.
80
     *
81
     * @param string $string String that needs to bet parsed
82
     */
83
    public static function bothify(string $string): string
84
    {
85
        $string = self::replaceWildcard($string, '*', static function () {
86
            return mt_rand(0, 1) ? '#' : '?';
87
        });
88
 
89
        return static::lexify(static::numerify($string));
90
    }
91
 
92
    private static function replaceWildcard(string $string, string $wildcard, callable $callback): string
93
    {
94
        if (($pos = strpos($string, $wildcard)) === false) {
95
            return $string;
96
        }
97
 
98
        for ($i = $pos, $last = strrpos($string, $wildcard, $pos) + 1; $i < $last; ++$i) {
99
            if ($string[$i] === $wildcard) {
100
                $string[$i] = call_user_func($callback);
101
            }
102
        }
103
 
104
        return $string;
105
    }
106
}