Install package via composer:
composer req marvin255/random-string-generator
It will be configured automatically.
Inject the generator to a service or a controller via DI:
use Marvin255\RandomStringGenerator\Generator\RandomStringGenerator;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
class SiteController extends AbstractController
{
public function __construct(private readonly RandomStringGenerator $randomStringGenerator)
{
}
}
Use one of the generators methods:
$this->randomStringGenerator->alphanumeric(10); // 10 symbols of latin alphabet or digits
$this->randomStringGenerator->alpha(10); // 10 symbols of latin alphabet
$this->randomStringGenerator->numeric(10); // 10 symbols of digits
$this->randomStringGenerator->password(10); // 10 symbols that can be used as password
$this->randomStringGenerator->string(10, 'qwe'); // 10 symbols of provided vocabulary
Bundle can be configured to return a mock string in the test environment.
# config/packages/test/marvin255_random_string_generator.yaml
marvin255_random_string_generator:
dummy: true
dummy_string: mock_string
All methods calls will return mock_string
.