Last active
May 21, 2020 16:14
-
-
Save blood72/ac843bcc89b3ea999f4dab7a4656190b to your computer and use it in GitHub Desktop.
Add randomBody method in Faker\Generator class
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| namespace App\Packages; | |
| use Faker\Generator; | |
| /** | |
| * @method string randomBody($maxDepth = 4, $maxWidth = 4) | |
| */ | |
| class FakerGenerator extends Generator | |
| { | |
| /** | |
| * FakerGenerator constructor. | |
| * | |
| * @return void | |
| */ | |
| public function __construct() | |
| { | |
| $this->addProvider(new class ($this) extends \Faker\Provider\HtmlLorem { | |
| /** | |
| * @param int $maxDepth | |
| * @param int $maxWidth | |
| * | |
| * @return string | |
| */ | |
| public function randomBody($maxDepth = 4, $maxWidth = 4) | |
| { | |
| $bodyTags = [ | |
| 'div', 'p', 'a', 'span', 'ul', 'li', 'h', 'b', 'i', | |
| 'table', 'thead', 'tbody', 'tr', 'td', 'th', | |
| ]; | |
| return strip_tags($this->randomHtml($maxDepth, $maxWidth), $bodyTags); | |
| } | |
| }); | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| namespace App\Providers; | |
| use Illuminate\Support\ServiceProvider; | |
| class FakerServiceProvider extends ServiceProvider | |
| { | |
| /** | |
| * Register any application services. | |
| * | |
| * @return void | |
| */ | |
| public function register() | |
| { | |
| $this->app->singleton(\Faker\Generator::class, function ($app) { | |
| $faker = (new class extends \Faker\Factory { | |
| /** | |
| * @param string $locale | |
| * @return \App\Packages\FakerGenerator|\Faker\Generator | |
| */ | |
| public static function create($locale = parent::DEFAULT_LOCALE) | |
| { | |
| $generator = new \App\Packages\FakerGenerator(); | |
| foreach (parent::$defaultProviders as $provider) { | |
| $providerClassName = parent::getProviderClassname($provider, $locale); | |
| $generator->addProvider(new $providerClassName($generator)); | |
| } | |
| return $generator; | |
| } | |
| })->create(); | |
| return $faker; | |
| }); | |
| } | |
| } |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Tinker output
Purpose: generate dummy data to be inserted into the post or comment content