Skip to content

Commit 71d5545

Browse files
Update readme
1 parent f6bcb3f commit 71d5545

File tree

2 files changed

+73
-33
lines changed

2 files changed

+73
-33
lines changed

README.md

Lines changed: 71 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,13 @@ Simple example without any configuration.
2121
$string = RandomString::new()->generate(); // Output: RIKdjFzuDaN12RiJ
2222
```
2323

24+
### Length definition
25+
You can control the length of string. By default, it's 16 characters length.
26+
27+
```php
28+
$string = RandomString::new(6)->generate(); // Output: dzGcot
29+
````
30+
2431
### Predefined charset
2532

2633
If you want to generate string consist of numbers only, you can do it like this:
@@ -45,54 +52,87 @@ $string = RandomString::fromConfig($config)->generate();
4552
```
4653

4754
### Custom charset
48-
Or you can use your custom charset for generating random string:
55+
56+
Or you can use your custom charset for generating random string
57+
4958
```php
5059
$config = StringConfig::make()
51-
->charset("ABCD1234");
60+
->charset("ABCDEFG1234");
5261

5362
$string = RandomString::fromConfig($config)->generate(); // Output: 3B41B32C2A12A3A1
5463
```
5564

56-
### Shorthands
57-
You can use shorthand for config.
65+
### Skipping
66+
67+
Sometimes you may want to generate random string, but under certain conditions.
68+
For example, give me a string that is not part of this array.
69+
5870
```php
59-
$string = RandomString::fromArray([
60-
'length' => 6,
61-
'charset' => 'ABCD1234'
62-
])->generate();
71+
$config = StringConfig::make()
72+
->numbersOnly()
73+
->length(6)
74+
->skip(function ($string) {
75+
return in_array($string, ["025922", "104923"]);
76+
});
6377

64-
echo $string; // Output: 3B41B32C2A12A3A1
78+
$string = RandomString::fromConfig($config)->generate(); // Output: 083712
6579
```
6680

67-
If you want to generate more than one string, with more than one configuration option, you can do it like this:
81+
### Prefix and Suffix
82+
83+
If you want to add prefix or suffix to generated string, you can do it like this.
84+
6885
```php
6986
use Stfn\RandomString\StringConfig;
7087

7188
$config = StringConfig::make()
72-
->charset("ABCD1234")
73-
->length(5)
74-
->prefix("PREFIX_")
75-
->suffix("_SUFFIX")
76-
->count(10)
77-
->unique()
78-
->skip(function ($string) {
79-
return in_array($string, ["PREFIX_BCD1234A_SUFFIX"]);
80-
});
89+
->length(6)
90+
->prefix("PRE_")
91+
->suffix("_AFTER");
92+
93+
$string = RandomString::fromConfig($config)->generate(); // Output: PRE_rkM7Jl_AFTER
94+
```
95+
96+
### Array of random strings
97+
98+
RandomString can generate more than just one random string.
99+
100+
```php
101+
$config = StringConfig::make()
102+
->length(6)
103+
->count(3);
81104

82105
$strings = RandomString::fromConfig($config)->generate();
83106

84-
echo $string; // Output: [
85-
"PREFIX_CAC23_SUFFIX"
86-
"PREFIX_3AAD2_SUFFIX"
87-
"PREFIX_CC21D_SUFFIX"
88-
"PREFIX_121C3_SUFFIX"
89-
"PREFIX_43ABC_SUFFIX"
90-
"PREFIX_D432A_SUFFIX"
91-
"PREFIX_43BC3_SUFFIX"
92-
"PREFIX_11BBB_SUFFIX"
93-
"PREFIX_31121_SUFFIX"
94-
"PREFIX_3AB1B_SUFFIX"
95-
];
107+
// Output: ["ozBYeT", "BYjCtr", "Sw7O5b"];
108+
```
109+
110+
### Uniqueness
111+
112+
By default, it may happen (rarely, but it's possible) to have not unique strings in the generated array. If you want to avoid it, just change the config.
113+
114+
```php
115+
$config = StringConfig::make()
116+
->length(6)
117+
->count(3)
118+
->unique();
119+
120+
$strings = RandomString::fromConfig($config)->generate();
121+
122+
// Output: ["92ONRj", "Me6oym", "WbBPVc"];
123+
```
124+
125+
### Shorthands
126+
127+
You can use shorthands if you don't want to create 2 objects every time.
128+
129+
```php
130+
$string = RandomString::fromArray([
131+
'length' => 6,
132+
'charset' => 'ABCD1234'
133+
])->generate();
134+
135+
echo $string; // Output: CCDA1D
96136
```
97137

98138
## Testing

src/RandomString.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,9 @@ public function __construct(StringConfig $config)
1717
$this->config = $config;
1818
}
1919

20-
public static function new(): self
20+
public static function new($length = 16): self
2121
{
22-
return new self(new StringConfig());
22+
return new self(new StringConfig($length));
2323
}
2424

2525
public static function fromConfig(StringConfig $config): self

0 commit comments

Comments
 (0)