/faker-bonus

Extra Faker Formatters/Providers for PHP

Primary LanguagePHPMIT LicenseMIT

faker-bonus

Codeship Status for rootinc/faker-bonus

A handy set of extra Faker Formatters/Providers for fzaninotto/faker built to integrate with Laravel or any PHP project that uses Faker.

Contents

  1. Installation
  2. Usage
    1. Basic Usage
    2. Laravel Installation
  3. Formatters
    1. Hashtag
    2. Mention
    3. Tweet Text
  4. Testing
  5. Acknowledgements

Installation

composer install rootinc/faker-bonus --dev

Usage

We can add new Providers directly to the Faker instance before we use it:

Basic Usage

<?php
$faker = Faker\Factory::create();
ProviderCollectionHelper::addAllProvidersTo($faker);
// Use Faker
$faker->hashtag;

Specific Provider/s

<?php
$faker = Faker\Factory::create();
$faker->addProvider(new Provider\Hashtag($faker));
//...
// Add more Providers 
//...
// Use Faker
$faker->hashtag;

Laravel Usage

We can have Providers/Formatters added to all instances of Faker\Generator by updating our AppServiceProvier as follows:

All Providers

app/Providers/AppServiceProvider.php

<?php

use Faker\Generator;

public function register() {
  //...
  // Whenever Faker\Generator is called, substitute the return value of this block
  $this->app->extend(Generator::class, function ($generator) {
      // Add Providers to the Faker\Generator class
      ProviderCollectionHelper::addAllProvidersTo($generator);
      // Return modified Faker\Generator
      return $generator;
  });
  //...
}

Formatters

Hashtag

Build some fun hashtags.

Definition

$faker->hashtag($includeTag = true)

Usage

$faker->hashtag // '#this_is_fun'
$faker->hashtag(false) // 'this_is_fun'

Mention

Builds handle-ish mentions.

Definition

$faker->mention($includeAt = true)

Usage

$faker->mention // '@bartoletti.barbara'
$faker->mention(false) // 'bartoletti.barbara'

Tweet Text

Create tweet-like text.

Definition

$faker->tweetText($nbParagraphs = 1, $includeEmoji = true)

Usage

$faker->tweetText // '@BSCHADEN The a my were anchors for consider that one man perfectly. 😀 #EXPLOIT_CUTTINGEDGE_EYEBALLS'
$faker->tweetText(2) // '@BSCHADEN The a my were anchors for consider that one man perfectly.\n\n Created, rung and over flows let four it lane.😀 #EXPLOIT_CUTTINGEDGE_EYEBALLS'
$faker->tweetText(3, false) // '@BSCHADEN The a my were anchors for consider that one man perfectly.\n\n Created, rung and over flows let four it lane.\n\n Created, rung and over flows let four it lane. #EXPLOIT_CUTTINGEDGE_EYEBALLS'

Testing

composer install
vendor/bin/phpunit

Acknowledgements