Source of file FakerProvider.php
Size: 1,780 Bytes - Last Modified: 2021-12-23T10:02:17+00:00
/var/www/docs.ssmods.com/process/src/code/Providers/FakerProvider.php
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283 | <?php namespace Seeder; use Exception; use Faker\Factory; /** * Class FakerProvider * @package Seeder */ class FakerProvider extends Provider { /** * @var */ private $faker; /** * @var string */ public static $shorthand = 'Faker'; /** * */ public function __construct() { parent::__construct(); $this->faker = Factory::create(); } /** * @param $argumentString * @return array */ public static function parseOptions($argumentString) { $options = array(); $arguments = array_map(function ($arg) { return trim($arg); }, explode(',', $argumentString)); $options['type'] = array_shift($arguments); $options['arguments'] = $arguments; return $options; } /** * @param $field * @param $state * @return mixed|string * @throws Exception */ protected function generateField($field, $state) { if (empty($field->options['type'])) { var_dump($field); throw new Exception('faker provider requires a \'type\''); } $type = $field->options['type']; // todo are there any faker methods without an argument? if (!empty($field->options['arguments'])) { $value = call_user_func_array(array($this->faker, $type), $field->options['arguments']); } else { $value = $this->faker->$type; } if (is_array($value)) { $join = ' '; if (stripos($field->dataType, 'text') !== false || stripos($type, 'paragraph') !== false) { $join = "\n"; } $value = implode($join, $value); } return $value; } } |