Skip to content

Commit a5695e7

Browse files
author
TS
committed
Add new methods for generating hashes, tokens, URLs, and addresses; update README and tests
1 parent 891f521 commit a5695e7

4 files changed

Lines changed: 239 additions & 0 deletions

File tree

README.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131

3232
$faker->fullname(5, 10); // Sutida Ohisovis
3333
$faker->firstname(7, 7, Faker::FIRST_VOWEL); // Emubefy
34+
$faker->lastname(5, 7); // Zusey
3435
$faker->email(); // punuw.honiw@ajesug.gax
3536
$faker->password(4, 4, 3, '.'); // W2gg.lsgq.FET0
3637
$faker->word(5); // focix
@@ -49,6 +50,16 @@
4950
$faker->pick(['CEO', 'CTO', 'Founder', 'Director'], 3, ', '); // Director, CEO, CTO
5051
$faker->date(); // 2001-07-21
5152
$faker->date('2024-01-01', '2024-12-31', 'Y.m.'); // 2024.02.
53+
$faker->hash('sha256'); // random hash
54+
$faker->token(32); // api token
55+
$faker->url(); // https://example.com/random
56+
$faker->domain(); // example.com
57+
$faker->city(); // Random City
58+
$faker->zipcode(4); // 1234
59+
$faker->country(); // Random Country
60+
$faker->streetName(); // Random Street Name
61+
$faker->street(); // Random Street Name 123
62+
$faker->address(); // Random Street Name 123, 1234 Random City, Random Country
5263

5364
```
5465

examples/demo.php

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,3 +160,55 @@
160160
$words[] = strtoupper($faker->words(14, '.', 2, 4));
161161

162162
print_r($words);
163+
164+
// New examples for the added methods
165+
166+
$hashes = [];
167+
$hashes[] = $faker->hash('sha256');
168+
$hashes[] = $faker->hash('md5');
169+
print_r($hashes);
170+
171+
$tokens = [];
172+
$tokens[] = $faker->token(32);
173+
$tokens[] = $faker->token(64);
174+
print_r($tokens);
175+
176+
$urls = [];
177+
$urls[] = $faker->url();
178+
$urls[] = $faker->url();
179+
print_r($urls);
180+
181+
$domains = [];
182+
$domains[] = $faker->domain();
183+
$domains[] = $faker->domain();
184+
print_r($domains);
185+
186+
$cities = [];
187+
$cities[] = $faker->city();
188+
$cities[] = $faker->city();
189+
print_r($cities);
190+
191+
$zipcodes = [];
192+
$zipcodes[] = $faker->zipcode(4);
193+
$zipcodes[] = $faker->zipcode(6);
194+
print_r($zipcodes);
195+
196+
$countries = [];
197+
$countries[] = $faker->country();
198+
$countries[] = $faker->country();
199+
print_r($countries);
200+
201+
$streetNames = [];
202+
$streetNames[] = $faker->streetName();
203+
$streetNames[] = $faker->streetName();
204+
print_r($streetNames);
205+
206+
$streets = [];
207+
$streets[] = $faker->street();
208+
$streets[] = $faker->street();
209+
print_r($streets);
210+
211+
$addresses = [];
212+
$addresses[] = $faker->address();
213+
$addresses[] = $faker->address();
214+
print_r($addresses);

src/Faker.php

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -490,4 +490,110 @@ public function date($min = null, $max = null, $format = 'Y-m-d')
490490
$timestamp = random_int(strtotime($min), strtotime($max));
491491
return date($format, $timestamp);
492492
}
493+
494+
/**
495+
* hash
496+
*
497+
* @param string $algorithm
498+
*
499+
* @return string
500+
*/
501+
public function hash($algorithm = 'sha256')
502+
{
503+
return hash($algorithm, $this->word(10) . random_int(1000, 9999));
504+
}
505+
506+
/**
507+
* token
508+
*
509+
* @param int $length
510+
*
511+
* @return string
512+
*/
513+
public function token($length = 32)
514+
{
515+
return $this->repeat($this->abcLower . $this->abcUpper . $this->numbers, $length, $length);
516+
}
517+
518+
/**
519+
* url
520+
*
521+
* @return string
522+
*/
523+
public function url()
524+
{
525+
return 'https://' . $this->domain() . '/' . $this->word(5);
526+
}
527+
528+
/**
529+
* domain
530+
*
531+
* @return string
532+
*/
533+
public function domain()
534+
{
535+
return $this->word(5) . '.' . $this->word(3);
536+
}
537+
538+
/**
539+
* city
540+
*
541+
* @return string
542+
*/
543+
public function city()
544+
{
545+
return ucfirst($this->word(6));
546+
}
547+
548+
/**
549+
* zipcode
550+
*
551+
* @param int $length
552+
*
553+
* @return string
554+
*/
555+
public function zipcode($length = 4)
556+
{
557+
return $this->repeat($this->numbers, $length, $length);
558+
}
559+
560+
/**
561+
* country
562+
*
563+
* @return string
564+
*/
565+
public function country()
566+
{
567+
return ucfirst($this->word(7));
568+
}
569+
570+
/**
571+
* streetName
572+
*
573+
* @return string
574+
*/
575+
public function streetName()
576+
{
577+
return ucfirst($this->word(8));
578+
}
579+
580+
/**
581+
* street
582+
*
583+
* @return string
584+
*/
585+
public function street()
586+
{
587+
return $this->streetName() . ' ' . $this->int(1, 999);
588+
}
589+
590+
/**
591+
* address
592+
*
593+
* @return string
594+
*/
595+
public function address()
596+
{
597+
return $this->street() . ', ' . $this->zipcode() . ' ' . $this->city() . ', ' . $this->country();
598+
}
493599
}

tests/FakerTest.php

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -276,4 +276,74 @@ public function testDates()
276276
$this->assertSame('1999-12', \substr($dates[1], 0, 7));
277277
$this->assertSame('2200-04-01 00:00:00', $dates[2]);
278278
}
279+
280+
/**
281+
* testNewMethods
282+
*
283+
* @return void
284+
*/
285+
public function testNewMethods()
286+
{
287+
$faker = Faker::init();
288+
289+
$hashes = [];
290+
$hashes[] = $faker->hash('sha256');
291+
$hashes[] = $faker->hash('md5');
292+
$this->assertNotEmpty($hashes[0]);
293+
$this->assertNotEmpty($hashes[1]);
294+
295+
$tokens = [];
296+
$tokens[] = $faker->token(32);
297+
$tokens[] = $faker->token(64);
298+
$this->assertEquals(32, strlen($tokens[0]));
299+
$this->assertEquals(64, strlen($tokens[1]));
300+
301+
$urls = [];
302+
$urls[] = $faker->url();
303+
$urls[] = $faker->url();
304+
$this->assertStringStartsWith('https://', $urls[0]);
305+
$this->assertStringStartsWith('https://', $urls[1]);
306+
307+
$domains = [];
308+
$domains[] = $faker->domain();
309+
$domains[] = $faker->domain();
310+
$this->assertStringContainsString('.', $domains[0]);
311+
$this->assertStringContainsString('.', $domains[1]);
312+
313+
$cities = [];
314+
$cities[] = $faker->city();
315+
$cities[] = $faker->city();
316+
$this->assertNotEmpty($cities[0]);
317+
$this->assertNotEmpty($cities[1]);
318+
319+
$zipcodes = [];
320+
$zipcodes[] = $faker->zipcode(4);
321+
$zipcodes[] = $faker->zipcode(6);
322+
$this->assertEquals(4, strlen($zipcodes[0]));
323+
$this->assertEquals(6, strlen($zipcodes[1]));
324+
325+
$countries = [];
326+
$countries[] = $faker->country();
327+
$countries[] = $faker->country();
328+
$this->assertNotEmpty($countries[0]);
329+
$this->assertNotEmpty($countries[1]);
330+
331+
$streetNames = [];
332+
$streetNames[] = $faker->streetName();
333+
$streetNames[] = $faker->streetName();
334+
$this->assertNotEmpty($streetNames[0]);
335+
$this->assertNotEmpty($streetNames[1]);
336+
337+
$streets = [];
338+
$streets[] = $faker->street();
339+
$streets[] = $faker->street();
340+
$this->assertNotEmpty($streets[0]);
341+
$this->assertNotEmpty($streets[1]);
342+
343+
$addresses = [];
344+
$addresses[] = $faker->address();
345+
$addresses[] = $faker->address();
346+
$this->assertNotEmpty($addresses[0]);
347+
$this->assertNotEmpty($addresses[1]);
348+
}
279349
}

0 commit comments

Comments
 (0)