Random
![Software License][ico-license]
, (*1)
Generate random strings, numbers, bytes, patterns, and a lot more
Features
It generates cryptographically secure pseudo-random bytes (using random_bytes()
and random_int()
) to make:, (*2)
- Strings
- Numbers (strings or integers)
- Upper, lower and mixed case
- Prefixed and suffixed random strings
- Hexadecimal
- Regex patterns ([abcd], [aeiou], [A-Z0123], [0-9a-f])
- Raw strings, giving you whatever
random_bytes()
generates
Faker
If you have Faker installed it falls back to it, giving you access to random names, dates, cities, phones, and a lot more., (*3)
Install
Via Composer, (*4)
``` bash
$ composer require pragmarx/random, (*5)
## Usage
#### Basic array usage
``` php
$this->random = new PragmaRX\Random\Random();
$this->random->get(); /// will generate an alpha string which is the same of
$this->random->alpha()->get();
Should give you 16 chars (default size) string, (*6)
``` text
Ajv3ejknLmqwC36z, (*7)
#### Defining the size
``` php
$this->random->size(32)->get();
Upper and lower case
``` php
$this->random->uppercase()->get();
$this->random->lowercase()->size(255)->get();, (*8)
To get back to mixed case you can just:
``` php
$this->random->mixedcase()->get();
Defining a pattern
The pattern method uses regex, so you can:, (*9)
``` php
$this->random->pattern('[abcd]')->get();, (*10)
$this->random->pattern('[A-F0-9]')->get(); /// Hexadecimal, (*11)
To get
``` text
abcddcbabbacbbdabbcb
Numeric and Integer
The pattern method uses regex, so you can:, (*12)
``` php
$this->random->numeric()->start(10)->end(20)->get();, (*13)
To get
``` text
(int) 18
But if you set the size, (*14)
``` php
$this->random->numeric()->size(3)->get();, (*15)
You'll get a string
``` text
(string) 123
Hexadecimal
``` php
$this->random->hex()->size(10)->get();, (*16)
Hexadecimal is uppercase by default, but you can get a lowercase by doing:
``` php
$this->random->hex()->lowercase()->get();
Prefix && Suffix
``` php
$this->random->hex()->prefix('#')->size(6)->lowercase()->get();, (*17)
And you should get a random CSS color:
``` text
#fafafa
Of course, the same works for suffixes, (*18)
``` php
$this->random->prefix('!')->suffix('@')->get();, (*19)
#### Trivia
There are currently 43982 questions in the trivia database, and this is how you get them:
``` php
$this->random->trivia()->get();
$this->random->trivia()->count(2)->get();
You'll need to install the Trivia database package:, (*20)
``` bash
$ composer require pragmarx/trivia, (*21)
#### Faker
If you install Faker
``` bash
composer require fzaninotto/faker
You'll also have access to all of the Faker features, like:, (*22)
``` php
$this->random->city()->get();, (*23)
And also use all other features of Random
``` php
$this->random->prefix('city: ')->city()->lowercase()->get();
You can also change the faker class, you another one pleases you more:, (*24)
``` php
$this->random->fakerClass(AnotherFaker\Factory::class)->date()->get();, (*25)
#### Raw strings
Usually the package returns characters in the range of Base64 (A to Z, a to z and 0 to 9), but you can completely disable this feature and make it return whatever `random_bytes()` generates:
``` php
$this->random->raw()->get();
Change log
Please see CHANGELOG for more information on what has changed recently., (*26)
Testing
bash
$ composer update
$ vendor/bin/phpunit
, (*27)
Contributing
Please see CONTRIBUTING for details., (*28)
Security
If you discover any security related issues, please email acr@antoniocarlosribeiro.com instead of using the issue tracker., (*29)
Credits
License
The MIT License (MIT). Please see License File for more information., (*30)