本文整理汇总了PHP中Faker\Generator::optional方法的典型用法代码示例。如果您正苦于以下问题:PHP Generator::optional方法的具体用法?PHP Generator::optional怎么用?PHP Generator::optional使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Faker\Generator
的用法示例。
在下文中一共展示了Generator::optional方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: createComments
/**
* @param int $count
* @param Video[] $videos
* @param User[] $users
* @param Repository $repo
* @throws \Exception
*/
protected function createComments($count, $videos, $users, Repository $repo)
{
$this->out->writeln(Comment::class);
/** @var ProgressHelper $progress */
$progress = $this->getHelperSet()->get('progress');
$progress->start($this->out, count($videos) * $count);
foreach ($videos as $video) {
$comments = [];
for ($i = 0; $i < $count; ++$i) {
$comment = new Comment();
$comment->author = $this->faker->randomElement($users);
$comment->text = $this->faker->realText(100);
$comment->inReplyTo = $this->faker->optional(0.25)->randomElement($comments);
$video->comments->add($comment);
$comments[] = $comment;
$progress->advance();
}
$repo->persist($video);
}
$progress->finish();
}
示例2: createRubrics
private function createRubrics($count = 50)
{
print_r("Create rubrics. Count: " . $count . "...");
$this->faker->unique(true);
for ($i = 1; $i <= $count; $i++) {
$rubric = new Rubric();
$rubric->title = ucfirst($this->faker->word);
if ($i === 1) {
$parent_id = NULL;
} else {
do {
$parent_id = $this->faker->optional(0.7)->numberBetween(1, $i);
} while ($parent_id === $i);
}
$rubric->parent_id = $parent_id;
$rubric->save();
}
print_r("DONE" . PHP_EOL);
}
示例3: generateAttributes
private function generateAttributes(FakerGenerator $faker, $attributes, $code, $scopeId)
{
foreach ($attributes as $attribute) {
$attributeFaker = $faker;
if (1 - $attribute->required > 0.001) {
$attributeFaker = $faker->optional($attribute->required);
}
$value = $attributeFaker->format($attribute->generatorMethod, $attribute->generatorArguments);
if ($value !== null) {
if ($value instanceof \DateTime) {
$value = $value->format('Y-m-d H:i:s');
}
$valueKey = sprintf('%s-%s-%s', $code, $attribute->id, $scopeId);
$this->batch['value'][$attribute->type][$valueKey] = ['entity_id' => null, 'attribute_id' => $attribute->id, 'scope_id' => $scopeId, 'value' => $value];
$this->batch['value_index'][$code][] = [$attribute->type, $valueKey];
}
}
return $this;
}