Airship\Engine\Security\Util::randomString PHP Méthode

randomString() public static méthode

Generate a random string of a given length and character set
public static randomString ( integer $length = 64, string $characters = self::PRINTABLE_ASCII ) : string
$length integer How many characters do you want?
$characters string Which characters to choose from
Résultat string
    public static function randomString(int $length = 64, string $characters = self::PRINTABLE_ASCII) : string
    {
        $str = '';
        $l = self::stringLength($characters) - 1;
        for ($i = 0; $i < $length; ++$i) {
            $r = \random_int(0, $l);
            $str .= $characters[$r];
        }
        return $str;
    }

Usage Example

Exemple #1
0
 /**
  * @covers Util::randomString()
  */
 public function testRandomString()
 {
     $sample = [Util::randomString(), Util::randomString()];
     $this->assertNotSame($sample[0], $sample[1]);
 }