| 1 | package Faker::Maker; | ||
| 2 | |||
| 3 | use 5.014; | ||
| 4 | |||
| 5 | use strict; | ||
| 6 | use warnings; | ||
| 7 | |||
| 8 | use registry; | ||
| 9 | use routines; | ||
| 10 | |||
| 11 | use Data::Object::Role; | ||
| 12 | |||
| 13 | with 'Data::Object::Role::Pluggable'; | ||
| 14 | with 'Data::Object::Role::Throwable'; | ||
| 15 | |||
| 16 | requires 'plugin'; | ||
| 17 | requires 'throw'; | ||
| 18 | |||
| 19 | method format_lex_markers(Str $string) { | ||
| 20 | $string =~ s/\?/$self->random_letter/eg; | ||
| 21 | |||
| 22 | return $string; | ||
| 23 | } | ||
| 24 | |||
| 25 | method format_line_markers(Str $string) { | ||
| 26 | $string =~ s/\\n/\n/g; | ||
| 27 | |||
| 28 | return $string; | ||
| 29 | } | ||
| 30 | |||
| 31 | method format_number_markers(Str $string = '###') { | ||
| 32 | $string =~ s/\#/$self->random_digit/eg; | ||
| 33 | $string =~ s/\%/$self->random_digit_not_zero/eg; | ||
| 34 | |||
| 35 | return $string; | ||
| 36 | } | ||
| 37 | |||
| 38 | method parse_format(Str $string = '') { | ||
| 39 | $string =~ s/\{\{\s?([#\.\w]+)\s?\}\}/$self->process_format($1)/eg; | ||
| 40 | |||
| 41 | return $string; | ||
| 42 | } | ||
| 43 | |||
| 44 | method process(Tuple[Str, Str] $lookup, Maybe[HashRef[Int]] $options) { | ||
| 45 | my $string; | ||
| 46 | |||
| 47 | $string = $self->process_lookup($lookup); | ||
| 48 | |||
| 49 | if ($options->{all_markers}) { | ||
| 50 | $string = $self->process_markers($string); | ||
| 51 | } | ||
| 52 | if ($options->{lex_markers}) { | ||
| 53 | $string = $self->format_lex_markers($string); | ||
| 54 | } | ||
| 55 | if ($options->{line_markers}) { | ||
| 56 | $string = $self->format_line_markers($string); | ||
| 57 | } | ||
| 58 | if ($options->{number_markers}) { | ||
| 59 | $string = $self->format_number_markers($string); | ||
| 60 | } | ||
| 61 | |||
| 62 | return $string; | ||
| 63 | } | ||
| 64 | |||
| 65 | method process_format(Str $token) { | ||
| 66 | my ($source, $content) = split /[#\.]/, $token; | ||
| 67 | |||
| 68 | my $plugin = $self->plugin($source, faker => $self); | ||
| 69 | |||
| 70 | return $plugin->$content if $plugin->can($content); | ||
| 71 | |||
| 72 | return $self->process_lookup([$source, $content]); | ||
| 73 | } | ||
| 74 | |||
| 75 | method process_lookup(Tuple[Str, Str] $lookup) { | ||
| 76 | my ($source, $datatype) = @$lookup; | ||
| 77 | |||
| 78 | my @samples = ( | ||
| 79 | $datatype, | ||
| 80 | "data_for_${datatype}", | ||
| 81 | "format_for_${datatype}" | ||
| 82 | ); | ||
| 83 | |||
| 84 | my $plugin = $self->plugin($source, faker => $self); | ||
| 85 | |||
| 86 | for my $sample (@samples) { | ||
| 87 | $plugin->can($sample) or next; | ||
| 88 | |||
| 89 | my $content = $plugin->$sample; | ||
| 90 | my $format = $content->[rand @$content]; | ||
| 91 | |||
| 92 | $format = join ' ', @$format if ref $format eq 'ARRAY'; | ||
| 93 | |||
| 94 | return $self->parse_format($format); | ||
| 95 | } | ||
| 96 | |||
| 97 | $plugin = $self->plugin("${source}_${datatype}", faker => $self); | ||
| 98 | |||
| 99 | return $plugin->execute; | ||
| 100 | } | ||
| 101 | |||
| 102 | method process_markers(Str $string = '') { | ||
| 103 | my @markers = qw( | ||
| 104 | lex_markers | ||
| 105 | line_markers | ||
| 106 | number_markers | ||
| 107 | ); | ||
| 108 | |||
| 109 | for my $marker (@markers) { | ||
| 110 | my $filter = "format_${marker}"; | ||
| 111 | |||
| 112 | $string = $self->$filter($string); | ||
| 113 | } | ||
| 114 | |||
| 115 | return $string; | ||
| 116 | } | ||
| 117 | |||
| 118 | method random_between(Maybe[Int] $from, Maybe[Int] $to) { | ||
| 119 | my $max = 2147483647; | ||
| 120 | |||
| 121 | $from = 0 if !$from || $from > $max; | ||
| 122 | $to = $max if !$to || $to > $max; | ||
| 123 | |||
| 124 | return $from + int rand($to - $from); | ||
| 125 | } | ||
| 126 | |||
| 127 | method random_digit() { | ||
| 128 | |||
| 129 | return int rand(10); | ||
| 130 | } | ||
| 131 | |||
| 132 | method random_digit_not_zero() { | ||
| 133 | |||
| 134 | return 1 + int rand(9); | ||
| 135 | } | ||
| 136 | |||
| 137 | method random_float(Maybe[Int] $place, Maybe[Int] $min, Maybe[Int] $max) { | ||
| 138 | $min //= 0; | ||
| 139 | $max //= $self->random_number; | ||
| 140 | |||
| 141 | my $tmp; $tmp = $min and $min = $max and $max = $tmp if $min > $max; | ||
| 142 | |||
| 143 | $place //= $self->random_digit_not_zero; | ||
| 144 | |||
| 145 | return sprintf "%.${place}f", $min + rand() * ($max - $min); | ||
| 146 | } | ||
| 147 | |||
| 148 | method random_item(ArrayRef | HashRef $items) { | ||
| 149 | return $self->random_array_item($items) if 'ARRAY' eq ref $items; | ||
| 150 | |||
| 151 | return $self->random_hash_item($items) if 'HASH' eq ref $items; | ||
| 152 | |||
| 153 | return undef; | ||
| 154 | } | ||
| 155 | |||
| 156 | method random_array_item(ArrayRef $items) { | ||
| 157 | |||
| 158 | return $items->[$self->random_between(0, $#{$items})]; | ||
| 159 | } | ||
| 160 | |||
| 161 | method random_hash_item(HashRef $items) { | ||
| 162 | |||
| 163 | return $items->{$self->random_item([keys %$items])}; | ||
| 164 | } | ||
| 165 | |||
| 166 | method random_letter() { | ||
| 167 | |||
| 168 | return chr $self->random_between(97, 122); | ||
| 169 | } | ||
| 170 | |||
| 171 | method random_number(Maybe[Int] $from, Maybe[Int] $to) { | ||
| 172 | $to //= 0; | ||
| 173 | $from //= $self->random_digit; | ||
| 174 | |||
| 175 | return $self->random_between($from, $to) if $to; | ||
| 176 | |||
| 177 | return int rand 10 ** $from - 1; | ||
| 178 | } | ||
| 179 | |||
| 180 | 1; | ||
| 181 | |||
| 182 | =encoding utf8 | ||
| 183 | |||
| 184 | =head1 NAME | ||
| 185 | |||
| 186 | Faker::Maker | ||
| 187 | |||
| 188 | =cut | ||
| 189 | |||
| 190 | =head1 ABSTRACT | ||
| 191 | |||
| 192 | Utility Role for Generating Fake Data | ||
| 193 | |||
| 194 | =cut | ||
| 195 | |||
| 196 | =head1 SYNOPSIS | ||
| 197 | |||
| 198 | package Example; | ||
| 199 | |||
| 200 | use Data::Object::Class; | ||
| 201 | |||
| 202 | with 'Faker::Maker'; | ||
| 203 | |||
| 204 | package main; | ||
| 205 | |||
| 206 | my $example = Example->new; | ||
| 207 | |||
| 208 | =cut | ||
| 209 | |||
| 210 | =head1 DESCRIPTION | ||
| 211 | |||
| 212 | This package provides methods for generating and selecting data randomly. | ||
| 213 | |||
| 214 | =cut | ||
| 215 | |||
| 216 | =head1 LIBRARIES | ||
| 217 | |||
| 218 | This package uses type constraints from: | ||
| 219 | |||
| 220 | L<Types::Standard> | ||
| 221 | |||
| 222 | =cut | ||
| 223 | |||
| 224 | =head1 METHODS | ||
| 225 | |||
| 226 | This package implements the following methods: | ||
| 227 | |||
| 228 | =cut | ||
| 229 | |||
| 230 | =head2 format_lex_markers | ||
| 231 | |||
| 232 | format_lex_markers(Str $string) : Str | ||
| 233 | |||
| 234 | The format_lex_markers method replaces each C<?> character in the string with a | ||
| 235 | random letter. | ||
| 236 | |||
| 237 | =over 4 | ||
| 238 | |||
| 239 | =item format_lex_markers example #1 | ||
| 240 | |||
| 241 | # given: synopsis | ||
| 242 | |||
| 243 | $example->format_lex_markers('???') | ||
| 244 | |||
| 245 | =back | ||
| 246 | |||
| 247 | =cut | ||
| 248 | |||
| 249 | =head2 format_line_markers | ||
| 250 | |||
| 251 | format_line_markers(Str $string) : Str | ||
| 252 | |||
| 253 | The format_line_markers method replaces each escaped C<\n> (newline) character | ||
| 254 | in the string with a proper newline character. | ||
| 255 | |||
| 256 | =over 4 | ||
| 257 | |||
| 258 | =item format_line_markers example #1 | ||
| 259 | |||
| 260 | # given: synopsis | ||
| 261 | |||
| 262 | $example->format_line_markers('foo\nbar') | ||
| 263 | |||
| 264 | =back | ||
| 265 | |||
| 266 | =cut | ||
| 267 | |||
| 268 | =head2 format_number_markers | ||
| 269 | |||
| 270 | format_number_markers(Str $string) : Str | ||
| 271 | |||
| 272 | The format_number_markers method replaces each C<#> in the string with a random | ||
| 273 | digit, and each C<%> with a random non-zero digit. | ||
| 274 | |||
| 275 | =over 4 | ||
| 276 | |||
| 277 | =item format_number_markers example #1 | ||
| 278 | |||
| 279 | # given: synopsis | ||
| 280 | |||
| 281 | $example->format_number_markers('#-%-#') | ||
| 282 | |||
| 283 | =back | ||
| 284 | |||
| 285 | =cut | ||
| 286 | |||
| 287 | =head2 parse_format | ||
| 288 | |||
| 289 | parse_format(Str $string) : Str | ||
| 290 | |||
| 291 | The parse_format method replaces each C<{{plugin.name}}> in the string with the | ||
| 292 | result of the evaluation of the plugin specified in the token, e.g. | ||
| 293 | C<{{person.name}}> is replaced with the result of executing the | ||
| 294 | L<Faker::Plugin::PersonName> plugin. | ||
| 295 | |||
| 296 | =over 4 | ||
| 297 | |||
| 298 | =item parse_format example #1 | ||
| 299 | |||
| 300 | # given: synopsis | ||
| 301 | |||
| 302 | $example->parse_format('{{person.name}}') | ||
| 303 | |||
| 304 | =back | ||
| 305 | |||
| 306 | =cut | ||
| 307 | |||
| 308 | =head2 process | ||
| 309 | |||
| 310 | process(Tuple[Str, Str] $lookup, Maybe[HashRef[Int]] $options) : Str | ||
| 311 | |||
| 312 | The process method performs a lookup and replacement in the lookup results | ||
| 313 | based on the options specified. Valid options are C<all_markers>, | ||
| 314 | C<lex_markers>, C<line_markers>, and C<number_markers>. | ||
| 315 | |||
| 316 | =over 4 | ||
| 317 | |||
| 318 | =item process example #1 | ||
| 319 | |||
| 320 | # given: synopsis | ||
| 321 | |||
| 322 | $example->process(['person', 'username'], { | ||
| 323 | all_markers => 1 | ||
| 324 | }) | ||
| 325 | |||
| 326 | =back | ||
| 327 | |||
| 328 | =cut | ||
| 329 | |||
| 330 | =head2 process_format | ||
| 331 | |||
| 332 | process_format(Str $format) : Str | ||
| 333 | |||
| 334 | The process_format method dispatches to a plugin or performs a lookup and | ||
| 335 | returns the result. | ||
| 336 | |||
| 337 | =over 4 | ||
| 338 | |||
| 339 | =item process_format example #1 | ||
| 340 | |||
| 341 | # given: synopsis | ||
| 342 | |||
| 343 | $example->process_format('person.username') | ||
| 344 | |||
| 345 | =back | ||
| 346 | |||
| 347 | =cut | ||
| 348 | |||
| 349 | =head2 process_lookup | ||
| 350 | |||
| 351 | process_lookup(Tuple[Str, Str] $lookup) : Str | ||
| 352 | |||
| 353 | The process_lookup method performs a lookup and returns the result. | ||
| 354 | |||
| 355 | =over 4 | ||
| 356 | |||
| 357 | =item process_lookup example #1 | ||
| 358 | |||
| 359 | # given: synopsis | ||
| 360 | |||
| 361 | $example->process(['person', 'username']) | ||
| 362 | |||
| 363 | =back | ||
| 364 | |||
| 365 | =cut | ||
| 366 | |||
| 367 | =head2 process_markers | ||
| 368 | |||
| 369 | process_markers(Str $string) : Str | ||
| 370 | |||
| 371 | The process_markers method replaces all markers, e.g. C<?>, C<#>, C<%>, with | ||
| 372 | their random character counterparts. | ||
| 373 | |||
| 374 | =over 4 | ||
| 375 | |||
| 376 | =item process_markers example #1 | ||
| 377 | |||
| 378 | # given: synopsis | ||
| 379 | |||
| 380 | $example->process_markers('#.%.#.?%?') | ||
| 381 | |||
| 382 | =back | ||
| 383 | |||
| 384 | =cut | ||
| 385 | |||
| 386 | =head2 random_array_item | ||
| 387 | |||
| 388 | random_array_item(ArrayRef $items) : Any | ||
| 389 | |||
| 390 | The random_array_item method returns a random element from the C<arrayref> | ||
| 391 | provided. | ||
| 392 | |||
| 393 | =over 4 | ||
| 394 | |||
| 395 | =item random_array_item example #1 | ||
| 396 | |||
| 397 | # given: synopsis | ||
| 398 | |||
| 399 | $example->random_item(['a', 'b', 'c']) | ||
| 400 | |||
| 401 | =back | ||
| 402 | |||
| 403 | =cut | ||
| 404 | |||
| 405 | =head2 random_between | ||
| 406 | |||
| 407 | random_between(Maybe[Int] $from, Maybe[Int] $to) : Int | ||
| 408 | |||
| 409 | The random_between method returns a random number between the integers | ||
| 410 | provided, or between C<0> and C<2147483647>. | ||
| 411 | |||
| 412 | =over 4 | ||
| 413 | |||
| 414 | =item random_between example #1 | ||
| 415 | |||
| 416 | # given: synopsis | ||
| 417 | |||
| 418 | $example->random_between | ||
| 419 | |||
| 420 | =back | ||
| 421 | |||
| 422 | =cut | ||
| 423 | |||
| 424 | =head2 random_digit | ||
| 425 | |||
| 426 | random_digit() : Int | ||
| 427 | |||
| 428 | The random_digit method returns a random digit betwee C<0> and C<10>. | ||
| 429 | |||
| 430 | =over 4 | ||
| 431 | |||
| 432 | =item random_digit example #1 | ||
| 433 | |||
| 434 | # given: synopsis | ||
| 435 | |||
| 436 | $example->random_digit | ||
| 437 | |||
| 438 | =back | ||
| 439 | |||
| 440 | =cut | ||
| 441 | |||
| 442 | =head2 random_digit_not_zero | ||
| 443 | |||
| 444 | random_digit_not_zero() : Int | ||
| 445 | |||
| 446 | The random_digit_not_zero method returns a random digit betwee C<1> and C<10>. | ||
| 447 | |||
| 448 | =over 4 | ||
| 449 | |||
| 450 | =item random_digit_not_zero example #1 | ||
| 451 | |||
| 452 | # given: synopsis | ||
| 453 | |||
| 454 | $example->random_digit_not_zero | ||
| 455 | |||
| 456 | =back | ||
| 457 | |||
| 458 | =cut | ||
| 459 | |||
| 460 | =head2 random_float | ||
| 461 | |||
| 462 | random_float(Maybe[Int] $place, Maybe[Int] $min, Maybe[Int] $max) : Num | ||
| 463 | |||
| 464 | The random_float method returns a random floating-point number between the | ||
| 465 | integers provided. | ||
| 466 | |||
| 467 | =over 4 | ||
| 468 | |||
| 469 | =item random_float example #1 | ||
| 470 | |||
| 471 | # given: synopsis | ||
| 472 | |||
| 473 | $example->random_float | ||
| 474 | |||
| 475 | =back | ||
| 476 | |||
| 477 | =over 4 | ||
| 478 | |||
| 479 | =item random_float example #2 | ||
| 480 | |||
| 481 | # given: synopsis | ||
| 482 | |||
| 483 | $example->random_float(1, 10, 200) | ||
| 484 | |||
| 485 | =back | ||
| 486 | |||
| 487 | =over 4 | ||
| 488 | |||
| 489 | =item random_float example #3 | ||
| 490 | |||
| 491 | # given: synopsis | ||
| 492 | |||
| 493 | $example->random_float(2, 10, 200) | ||
| 494 | |||
| 495 | =back | ||
| 496 | |||
| 497 | =cut | ||
| 498 | |||
| 499 | =head2 random_hash_item | ||
| 500 | |||
| 501 | random_hash_item(HashRef $items) : Any | ||
| 502 | |||
| 503 | The random_hash_item method returns a random element from the C<hashref> | ||
| 504 | provided. | ||
| 505 | |||
| 506 | =over 4 | ||
| 507 | |||
| 508 | =item random_hash_item example #1 | ||
| 509 | |||
| 510 | # given: synopsis | ||
| 511 | |||
| 512 | $example->random_item({'a' => 1, 'b' => 2, 'c' => 3}) | ||
| 513 | |||
| 514 | =back | ||
| 515 | |||
| 516 | =cut | ||
| 517 | |||
| 518 | =head2 random_item | ||
| 519 | |||
| 520 | random_item(ArrayRef | HashRef $items) : Any | ||
| 521 | |||
| 522 | The random_item method returns a random element from the C<arrayref> or | ||
| 523 | C<hashref> provided. | ||
| 524 | |||
| 525 | =over 4 | ||
| 526 | |||
| 527 | =item random_item example #1 | ||
| 528 | |||
| 529 | # given: synopsis | ||
| 530 | |||
| 531 | $example->random_item(['a', 'b', 'c']) | ||
| 532 | |||
| 533 | =back | ||
| 534 | |||
| 535 | =over 4 | ||
| 536 | |||
| 537 | =item random_item example #2 | ||
| 538 | |||
| 539 | # given: synopsis | ||
| 540 | |||
| 541 | $example->random_item({'a' => 1, 'b' => 2, 'c' => 3}) | ||
| 542 | |||
| 543 | =back | ||
| 544 | |||
| 545 | =cut | ||
| 546 | |||
| 547 | =head2 random_letter | ||
| 548 | |||
| 549 | random_letter() : Str | ||
| 550 | |||
| 551 | The random_letter method returns a random alphabetic character. | ||
| 552 | |||
| 553 | =over 4 | ||
| 554 | |||
| 555 | =item random_letter example #1 | ||
| 556 | |||
| 557 | # given: synopsis | ||
| 558 | |||
| 559 | $example->random_letter | ||
| 560 | |||
| 561 | =back | ||
| 562 | |||
| 563 | =cut | ||
| 564 | |||
| 565 | =head2 random_number | ||
| 566 | |||
| 567 | random_number() : Num | ||
| 568 | |||
| 569 | The random_number method returns a random number. | ||
| 570 | |||
| 571 | =over 4 | ||
| 572 | |||
| 573 | =item random_number example #1 | ||
| 574 | |||
| 575 | # given: synopsis | ||
| 576 | |||
| 577 | $example->random_number | ||
| 578 | |||
| 579 | =back | ||
| 580 | |||
| 581 | =cut | ||
| 582 | |||
| 583 | =head1 AUTHOR | ||
| 584 | |||
| 585 | Al Newkirk, C<awncorp@cpan.org> | ||
| 586 | |||
| 587 | =head1 LICENSE | ||
| 588 | |||
| 589 | Copyright (C) 2011-2019, Al Newkirk, et al. | ||
| 590 | |||
| 591 | This is free software; you can redistribute it and/or modify it under the terms | ||
| 592 | of the The Apache License, Version 2.0, as elucidated in the L<"license | ||
| 593 | file"|https://github.com/iamalnewkirk/faker/blob/master/LICENSE>. | ||
| 594 | |||
| 595 | =head1 ACKNOWLEDGEMENTS | ||
| 596 | |||
| 597 | Parts of this library were inspired by the following implementations: | ||
| 598 | |||
| 599 | L<PHP Faker|https://github.com/fzaninotto/Faker> | ||
| 600 | |||
| 601 | L<Ruby Faker|https://github.com/stympy/faker> | ||
| 602 | |||
| 603 | L<Python Faker|https://github.com/joke2k/faker> | ||
| 604 | |||
| 605 | L<JS Faker|https://github.com/Marak/faker.js> | ||
| 606 | |||
| 607 | L<Elixir Faker|https://github.com/elixirs/faker> | ||
| 608 | |||
| 609 | =head1 PROJECT | ||
| 610 | |||
| 611 | L<Wiki|https://github.com/iamalnewkirk/faker/wiki> | ||
| 612 | |||
| 613 | L<Project|https://github.com/iamalnewkirk/faker> | ||
| 614 | |||
| 615 | L<Initiatives|https://github.com/iamalnewkirk/faker/projects> | ||
| 616 | |||
| 617 | L<Milestones|https://github.com/iamalnewkirk/faker/milestones> | ||
| 618 | |||
| 619 | L<Contributing|https://github.com/iamalnewkirk/faker/blob/master/CONTRIBUTE.md> | ||
| 620 | |||
| 621 | L<Issues|https://github.com/iamalnewkirk/faker/issues> | ||
| 622 | |||
| 623 | =cut |