- substr: documentation ( source)
- var_dump: documentation ( source)
- mt_rand: documentation ( source)
- mt_srand: documentation ( source)
- json_encode: documentation ( source)
- sprintf: documentation ( source)
<?php mt_srand( 1234 );
// Arrange
$input = [];
for ($i = 0; $i < 10; $i++ ) {
$input[] = wfRandomString( 5 );
}
// Act
$map = [];
foreach ( $input as $str ) {
$map[$str] = 'something computed something';
}
$output = [];
foreach ( $map as $str => $somthing ) {
$output[] = $str;
}
// Assert
foreach ( $output as $str ) {
if ( !is_string( $str ) ) {
echo "WAT: "; var_dump( $str );
} else {
echo "OK: "; var_dump( $str );
}
}
echo json_encode( $output, JSON_PRETTY_PRINT );
// -------
/** From MediaWiki 1.35 - https://w.wiki/oyY */
function wfRandomString( $length ) : string {
$str = '';
for ( $n = 0; $n < $length; $n += 7 ) {
$str .= sprintf( '%07x', mt_rand() & 0xfffffff );
}
return substr( $str, 0, $length );
}