abstract class Text extends Base

Properties

protected Generator $generator from  Base
protected UniqueGenerator $unique from  Base
static protected $baseText
static protected $separator
static protected $separatorLen
static protected $textStartsWithUppercase
protected $explodedText
protected $consecutiveWords

Methods

__construct(Generator $generator)

No description

from  Base
mixed|null
optional(float|int $weight = 0.5, $default = null)

Chainable method for making any formatter optional.

from  Base
unique(bool $reset = false, int $maxRetries = 10000)

Chainable method for making any formatter unique.

from  Base
valid(Closure $validator = null, int $maxRetries = 10000)

Chainable method for forcing any formatter to return only valid values.

from  Base
static int
randomDigit()

Returns a random number between 0 and 9

from  Base
static int
randomDigitNotNull()

Returns a random number between 1 and 9

from  Base
static int
randomDigitNot(int $except)

Generates a random digit, which cannot be $except

from  Base
static int
randomNumber(int $nbDigits = null, bool $strict = false)

Returns a random integer with 0 to $nbDigits digits.

from  Base
static float
randomFloat(int $nbMaxDecimals = null, int|float $min = 0, int|float $max = null)

Return a random float number

from  Base
static int
numberBetween(int $int1 = 0, int $int2 = 2147483647)

Returns a random number between $int1 and $int2 (any order)

from  Base
static mixed
passthrough(mixed $value)

Returns the passed value

from  Base
static string
randomLetter()

Returns a random letter from a to z

from  Base
static 
randomAscii()

Returns a random ASCII character (excluding accents and special chars)

from  Base
static array
randomElements(array $array = ['a', 'b', 'c'], int $count = 1, bool $allowDuplicates = false)

Returns randomly ordered subsequence of $count elements from a provided array

from  Base
static mixed
randomElement(array|Countable $array = ['a', 'b', 'c'])

Returns a random element from a passed array

from  Base
static int|string|null
randomKey(array $array = [])

Returns a random key from a passed associative array

from  Base
static array|string
shuffle(array|string $arg = '')

Returns a shuffled version of the argument.

from  Base
static array
shuffleArray(array $array = [])

Returns a shuffled version of the array.

from  Base
static string
shuffleString(string $string = '', string $encoding = 'UTF-8')

Returns a shuffled version of the string.

from  Base
static string
numerify(string $string = '###')

Replaces all hash sign ('#') occurrences with a random number Replaces all percentage sign ('%') occurrences with a not null number

from  Base
static string
lexify(string $string = '????')

Replaces all question mark ('?') occurrences with a random letter

from  Base
static string
bothify(string $string = '## ??')

Replaces hash signs ('#') and question marks ('?') with random numbers and letters An asterisk ('*') is replaced with either a random number or a random letter

from  Base
static string
asciify(string $string = '****')

Replaces * signs with random numbers and letters and special characters

from  Base
static string
regexify(string $regex = '')

Transforms a basic regular expression into a random string satisfying the expression.

from  Base
static string
toLower(string $string = '')

Converts string to lowercase.

from  Base
static string
toUpper(string $string = '')

Converts string to uppercase.

from  Base
string
realText(int $maxNbChars = 200, int $indexSize = 2)

Generate a text string by the Markov chain algorithm.

getConsecutiveWords($indexSize)

No description

getExplodedText()

No description

static 
explode($text)

No description

static 
implode($words)

No description

static 
strlen($text)

No description

static 
validStart($word)

No description

static 
appendEnd($text)

No description

Details

in Base at line 31
__construct(Generator $generator)

No description

Parameters

Generator $generator

in Base at line 45
mixed|null optional(float|int $weight = 0.5, $default = null)

Chainable method for making any formatter optional.

Parameters

float|int $weight

Set the probability of receiving a null value. "0" will always return null, "1" will always return the generator. If $weight is an integer value, then the same system works between 0 (always get false) and 100 (always get true).

$default

Return Value

mixed|null

in Base at line 76
UniqueGenerator unique(bool $reset = false, int $maxRetries = 10000)

Chainable method for making any formatter unique.

// will never return twice the same value
$faker->unique()->randomElement(array(1, 2, 3));

Parameters

bool $reset

If set to true, resets the list of existing values

int $maxRetries

Maximum number of retries to find a unique value, After which an OverflowException is thrown.

Return Value

UniqueGenerator

A proxy class returning only non-existing values

Exceptions

OverflowException

in Base at line 108
ValidGenerator valid(Closure $validator = null, int $maxRetries = 10000)

Chainable method for forcing any formatter to return only valid values.

The value validity is determined by a function passed as first argument.

$values = array();
$evenValidator = function ($digit) {
  return $digit % 2 === 0;
};
for ($i=0; $i < 10; $i++) {
  $values []= $faker->valid($evenValidator)->randomDigit;
}
print_r($values); // [0, 4, 8, 4, 2, 6, 0, 8, 8, 6]

Parameters

Closure $validator

A function returning true for valid values

int $maxRetries

Maximum number of retries to find a unique value, After which an OverflowException is thrown.

Return Value

ValidGenerator

A proxy class returning only valid values

Exceptions

OverflowException

in Base at line 118
static int randomDigit()

Returns a random number between 0 and 9

Return Value

int

in Base at line 128
static int randomDigitNotNull()

Returns a random number between 1 and 9

Return Value

int

in Base at line 139
static int randomDigitNot(int $except)

Generates a random digit, which cannot be $except

Parameters

int $except

Return Value

int

in Base at line 159
static int randomNumber(int $nbDigits = null, bool $strict = false)

Returns a random integer with 0 to $nbDigits digits.

The maximum value returned is mt_getrandmax()

Parameters

int $nbDigits

Defaults to a random number between 1 and 9

bool $strict

Whether the returned number should have exactly $nbDigits

Return Value

int

Examples

79907610

in Base at line 188
static float randomFloat(int $nbMaxDecimals = null, int|float $min = 0, int|float $max = null)

Return a random float number

Parameters

int $nbMaxDecimals
int|float $min
int|float $max

Return Value

float

Examples

48.8932

in Base at line 219
static int numberBetween(int $int1 = 0, int $int2 = 2147483647)

Returns a random number between $int1 and $int2 (any order)

Parameters

int $int1

default to 0

int $int2

defaults to 32 bit max integer, ie 2147483647

Return Value

int

Examples

79907610

in Base at line 233
static mixed passthrough(mixed $value)

Returns the passed value

Parameters

mixed $value

Return Value

mixed

in Base at line 243
static string randomLetter()

Returns a random letter from a to z

Return Value

string

in Base at line 251
static randomAscii()

Returns a random ASCII character (excluding accents and special chars)

in Base at line 266
static array randomElements(array $array = ['a', 'b', 'c'], int $count = 1, bool $allowDuplicates = false)

Returns randomly ordered subsequence of $count elements from a provided array

Parameters

array $array

Array to take elements from. Defaults to a-c

int $count

Number of elements to take.

bool $allowDuplicates

Allow elements to be picked several times. Defaults to false

Return Value

array

New array with $count elements from $array

Exceptions

LengthException

in Base at line 312
static mixed randomElement(array|Countable $array = ['a', 'b', 'c'])

Returns a random element from a passed array

Parameters

array|Countable $array

Return Value

mixed

in Base at line 328
static int|string|null randomKey(array $array = [])

Returns a random key from a passed associative array

Parameters

array $array

Return Value

int|string|null

in Base at line 353
static array|string shuffle(array|string $arg = '')

Returns a shuffled version of the argument.

This function accepts either an array, or a string.

Parameters

array|string $arg

The set to shuffle

Return Value

array|string

The shuffled set

See also

shuffleArray()
shuffleString()

Examples

$faker->shuffle([1, 2, 3]); // [2, 1, 3]
$faker->shuffle('hello, world'); // 'rlo,h eold!lw'

in Base at line 379
static array shuffleArray(array $array = [])

Returns a shuffled version of the array.

This function does not mutate the original array. It uses the Fisher–Yates algorithm, which is unbiased, together with a Mersenne twister random generator. This function is therefore more random than PHP's shuffle() function, and it is seedable.

Parameters

array $array

The set to shuffle

Return Value

array

The shuffled set

Examples

$faker->shuffleArray([1, 2, 3]); // [2, 1, 3]

in Base at line 420
static string shuffleString(string $string = '', string $encoding = 'UTF-8')

Returns a shuffled version of the string.

This function does not mutate the original string. It uses the Fisher–Yates algorithm, which is unbiased, together with a Mersenne twister random generator. This function is therefore more random than PHP's shuffle() function, and it is seedable. Additionally, it is UTF8 safe if the mb extension is available.

Parameters

string $string

The set to shuffle

string $encoding

The string encoding (defaults to UTF-8)

Return Value

string

The shuffled set

Examples

$faker->shuffleString('hello, world'); // 'rlo,h eold!lw'

in Base at line 443
static string numerify(string $string = '###')

Replaces all hash sign ('#') occurrences with a random number Replaces all percentage sign ('%') occurrences with a not null number

Parameters

string $string

String that needs to bet parsed

Return Value

string

in Base at line 477
static string lexify(string $string = '????')

Replaces all question mark ('?') occurrences with a random letter

Parameters

string $string

String that needs to bet parsed

Return Value

string

in Base at line 489
static string bothify(string $string = '## ??')

Replaces hash signs ('#') and question marks ('?') with random numbers and letters An asterisk ('*') is replaced with either a random number or a random letter

Parameters

string $string

String that needs to bet parsed

Return Value

string

in Base at line 505
static string asciify(string $string = '****')

Replaces * signs with random numbers and letters and special characters

Parameters

string $string

String that needs to bet parsed

Return Value

string

Examples

$faker->asciify(''********'); // "s5'G!uC3"

in Base at line 536
static string regexify(string $regex = '')

Transforms a basic regular expression into a random string satisfying the expression.

Parameters

string $regex

A regular expression (delimiters are optional)

Return Value

string

See also

https://github.com/icomefromthenet/ReverseRegex for a more robust implementation

Examples

$faker->regexify('[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}'); // sm0@y8k96a.ej

Regex delimiters '/.../' and begin/end markers '^...$' are ignored.

Only supports a small subset of the regex syntax. For instance,
unicode, negated classes, unbounded ranges, subpatterns, back references,
assertions, recursive patterns, and comments are not supported. Escaping
support is extremely fragile.

This method is also VERY slow. Use it only when no other formatter
can generate the fake data you want. For instance, prefer calling
`$faker->email` rather than `regexify` with the previous regular
expression.

Also note than `bothify` can probably do most of what this method does,
but much faster. For instance, for a dummy email generation, try
`$faker->bothify('?????????@???.???')`.

in Base at line 590
static string toLower(string $string = '')

Converts string to lowercase.

Uses mb_string extension if available.

Parameters

string $string

String that should be converted to lowercase

Return Value

string

in Base at line 602
static string toUpper(string $string = '')

Converts string to uppercase.

Uses mb_string extension if available.

Parameters

string $string

String that should be converted to uppercase

Return Value

string

at line 36
string realText(int $maxNbChars = 200, int $indexSize = 2)

Generate a text string by the Markov chain algorithm.

Depending on the $maxNbChars, returns a random valid looking text. The algorithm generates a weighted table with the specified number of words as the index and the possible following words as the value.

Parameters

int $maxNbChars

Maximum number of characters the text should contain (minimum: 10)

int $indexSize

Determines how many words are considered for the generation of the next word. The minimum is 1, and it produces a higher level of randomness, although the generated text usually doesn't make sense. Higher index sizes (up to 5) produce more correct text, at the price of less randomness.

Return Value

string

Examples

'Alice, swallowing down her flamingo, and began by taking the little golden key'

at line 84
protected getConsecutiveWords($indexSize)

No description

Parameters

$indexSize

at line 111
protected getExplodedText()

No description

at line 120
static protected explode($text)

No description

Parameters

$text

at line 125
static protected implode($words)

No description

Parameters

$words

at line 130
static protected strlen($text)

No description

Parameters

$text

at line 135
static protected validStart($word)

No description

Parameters

$word

at line 144
static protected appendEnd($text)

No description

Parameters

$text