本文整理汇总了PHP中Faker\Generator::randomElement方法的典型用法代码示例。如果您正苦于以下问题:PHP Generator::randomElement方法的具体用法?PHP Generator::randomElement怎么用?PHP Generator::randomElement使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Faker\Generator
的用法示例。
在下文中一共展示了Generator::randomElement方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: configureOptions
/**
* {@inheritdoc}
*/
protected function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefault('name', function (Options $options) {
return $this->faker->words(3, true);
})->setDefault('code', function (Options $options) {
return StringInflector::nameToCode($options['name']);
})->setDefault('type', function (Options $options) {
return $this->faker->randomElement(array_keys($this->attributeTypes));
})->setAllowedValues('type', array_keys($this->attributeTypes));
}
示例2: load
/**
* {@inheritdoc}
*/
public function load(array $options)
{
$options = $this->optionsResolver->resolve($options);
$this->taxonFixture->load(['custom' => [['code' => 'category', 'name' => 'Category', 'children' => [['code' => 'mugs', 'name' => 'Mugs']]]]]);
$this->productAttributeFixture->load(['custom' => [['name' => 'Mug material', 'code' => 'mug_material', 'type' => TextAttributeType::TYPE]]]);
$this->productOptionFixture->load(['custom' => [['name' => 'Mug type', 'code' => 'mug_type', 'values' => ['mug_type_medium' => 'Medium mug', 'mug_type_double' => 'Double mug', 'mug_type_monster' => 'Monster mug']]]]);
$products = [];
for ($i = 0; $i < $options['amount']; ++$i) {
$products[] = ['name' => sprintf('Mug "%s"', $this->faker->word), 'code' => $this->faker->uuid, 'main_taxon' => 'mugs', 'taxons' => ['mugs'], 'product_attributes' => ['mug_material' => $this->faker->randomElement(['Invisible porcelain', 'Banana skin', 'Porcelain', 'Centipede'])], 'images' => [sprintf('%s/../Resources/fixtures/%s', __DIR__, 'mugs.jpg')]];
}
$this->productFixture->load(['custom' => $products]);
}
示例3: load
/**
* {@inheritdoc}
*/
public function load(array $options)
{
$options = $this->optionsResolver->resolve($options);
$this->taxonFixture->load(['custom' => [['code' => 'category', 'name' => 'Category', 'children' => [['code' => 'stickers', 'name' => 'Stickers']]]]]);
$this->productAttributeFixture->load(['custom' => [['name' => 'Sticker paper', 'code' => 'sticker_paper', 'type' => TextAttributeType::TYPE], ['name' => 'Sticker resolution', 'code' => 'sticker_resolution', 'type' => TextAttributeType::TYPE]]]);
$this->productOptionFixture->load(['custom' => [['name' => 'Sticker size', 'code' => 'sticker_size', 'values' => ['sticker_size-3' => '3"', 'sticker_size_5' => '5"', 'sticker_size_7' => '7"']]]]);
$products = [];
for ($i = 0; $i < $options['amount']; ++$i) {
$products[] = ['name' => sprintf('Sticker "%s"', $this->faker->word), 'code' => $this->faker->uuid, 'main_taxon' => 'stickers', 'taxons' => ['stickers'], 'product_attributes' => ['sticker_paper' => sprintf('Paper from tree %s', $this->faker->randomElement(['Wung', 'Tanajno', 'Lemon-San', 'Me-Gusta'])), 'sticker_resolution' => $this->faker->randomElement(['JKM XD', '476DPI', 'FULL HD', '200DPI'])], 'images' => ['main' => sprintf('%s/../Resources/fixtures/%s', __DIR__, 'stickers.jpg'), 'thumbnail' => sprintf('%s/../Resources/fixtures/%s', __DIR__, 'stickers.jpg')]];
}
$this->productFixture->load(['custom' => $products]);
}
示例4: __construct
/**
* @param AttributeFactoryInterface $productAttributeFactory
* @param RepositoryInterface $localeRepository
* @param array $attributeTypes
*/
public function __construct(AttributeFactoryInterface $productAttributeFactory, RepositoryInterface $localeRepository, array $attributeTypes)
{
$this->productAttributeFactory = $productAttributeFactory;
$this->localeRepository = $localeRepository;
$this->faker = \Faker\Factory::create();
$this->optionsResolver = (new OptionsResolver())->setDefault('name', function (Options $options) {
return $this->faker->words(3, true);
})->setDefault('code', function (Options $options) {
return StringInflector::nameToCode($options['name']);
})->setDefault('type', function (Options $options) use($attributeTypes) {
return $this->faker->randomElement(array_keys($attributeTypes));
})->setAllowedValues('type', array_keys($attributeTypes));
}
示例5: randomPlayer
/**
* @param Player|null $not
* @return Player
*/
private function randomPlayer(Player $not = null)
{
while (true) {
$random = $this->faker->randomElement($this->players);
if ($not === null) {
return $random;
}
if ($random->getName() !== $not->getName()) {
return $random;
}
}
return null;
}
示例6: load
/**
* {@inheritdoc}
*/
public function load(array $options)
{
$options = $this->optionsResolver->resolve($options);
$this->taxonFixture->load(['custom' => [['code' => 'category', 'name' => 'Category', 'children' => [['code' => 't_shirts', 'name' => 'T-Shirts', 'children' => [['code' => 'mens_t_shirts', 'name' => 'Men'], ['code' => 'womens_t_shirts', 'name' => 'Women']]]]]]]);
$this->productAttributeFixture->load(['custom' => [['name' => 'T-Shirt brand', 'code' => 't_shirt_brand', 'type' => TextAttributeType::TYPE], ['name' => 'T-Shirt collection', 'code' => 't_shirt_collection', 'type' => TextAttributeType::TYPE], ['name' => 'T-Shirt material', 'code' => 't_shirt_material', 'type' => TextAttributeType::TYPE]]]);
$this->productOptionFixture->load(['custom' => [['name' => 'T-Shirt color', 'code' => 't_shirt_color', 'values' => ['t_shirt_color_red' => 'Red', 't_shirt_color_black' => 'Black', 't_shirt_color_white' => 'White']], ['name' => 'T-Shirt size', 'code' => 't_shirt_size', 'values' => ['t_shirt_size_s' => 'S', 't_shirt_size_m' => 'M', 't_shirt_size_l' => 'L', 't_shirt_size_xl' => 'XL', 't_shirt_size_xxl' => 'XXL']]]]);
$products = [];
for ($i = 0; $i < $options['amount']; ++$i) {
$categoryTaxonCode = $this->faker->randomElement(['mens_t_shirts', 'womens_t_shirts']);
$products[] = ['name' => sprintf('T-Shirt "%s"', $this->faker->word), 'code' => $this->faker->uuid, 'main_taxon' => $categoryTaxonCode, 'taxons' => [$categoryTaxonCode], 'product_attributes' => ['t_shirt_brand' => $this->faker->randomElement(['Nike', 'Adidas', 'JKM-476 Streetwear', 'Potato', 'Centipede Wear']), 't_shirt_collection' => sprintf('Sylius %s %s', $this->faker->randomElement(['Summer', 'Winter', 'Spring', 'Autumn']), mt_rand(1995, 2012)), 't_shirt_material' => $this->faker->randomElement(['Centipede', 'Wool', 'Centipede 10% / Wool 90%', 'Potato 100%'])], 'product_options' => ['t_shirt_color', 't_shirt_size'], 'images' => ['main' => sprintf('%s/../Resources/fixtures/%s', __DIR__, 't-shirts.jpg'), 'thumbnail' => sprintf('%s/../Resources/fixtures/%s', __DIR__, 't-shirts.jpg')]];
}
$this->productFixture->load(['custom' => $products]);
}
示例7: test
public function test(TestCase $test, Generator $faker)
{
$value = $faker->randomElement(array_filter(array_keys($this->items)), function ($value) {
return !!$value;
});
return $test->select($value, $this->name);
}
示例8: createSchemasAndBlocks
/**
* @param RepositoryContainer $orm
* @param Subject[] $subjects
* @param Video[] $videos
*/
protected function createSchemasAndBlocks($orm, $subjects, $videos)
{
$author = $orm->users->getById(1);
foreach ($subjects as $subject) {
for ($i = 0; $i < 3; ++$i) {
$schema = new Schema();
$schema->name = $this->faker->name;
$schema->subject = $subject;
$schema->author = $author;
$orm->schemas->attach($schema);
for ($k = 0; $k < 10; ++$k) {
$block = new Block();
$block->name = $this->faker->name;
$block->author = $author;
$orm->blocks->attach($block);
$bridge = new BlockSchemaBridge();
$bridge->position = $k;
$bridge->block = $block;
$bridge->schema = $schema;
$orm->blockSchemaBridges->attach($bridge);
for ($l = 0; $l < 7; ++$l) {
$bridge = new ContentBlockBridge();
$bridge->block = $block;
$bridge->content = $this->faker->randomElement($videos);
$bridge->position = $l;
$orm->contentBlockBridges->attach($bridge);
}
}
}
}
}
示例9: selectPayment
/**
* @param OrderInterface $order
*/
private function selectPayment(OrderInterface $order)
{
$paymentMethod = $this->faker->randomElement($this->paymentMethodRepository->findEnabledForChannel($order->getChannel()));
Assert::notNull($paymentMethod, 'Payment method should not be null.');
foreach ($order->getPayments() as $payment) {
$payment->setMethod($paymentMethod);
}
$this->applyCheckoutStateTransition($order, OrderCheckoutTransitions::TRANSITION_SELECT_PAYMENT);
}
示例10: selectPayment
/**
* @param OrderInterface $order
*/
private function selectPayment(OrderInterface $order)
{
$paymentMethod = $this->faker->randomElement($order->getChannel()->getPaymentMethods()->toArray());
Assert::notNull($paymentMethod);
foreach ($order->getPayments() as $payment) {
$payment->setMethod($paymentMethod);
}
$this->applyCheckoutStateTransition($order, OrderCheckoutTransitions::TRANSITION_SELECT_PAYMENT);
}
示例11: generateValueData
/**
* Generate value content based on backend type
*
* @param AbstractAttribute $attribute
* @param string $key
*
* @return string
*/
protected function generateValueData(AbstractAttribute $attribute, $key)
{
$data = "";
if (isset($this->forcedValues[$attribute->getCode()])) {
return $this->forcedValues[$attribute->getCode()];
}
switch ($attribute->getBackendType()) {
case "varchar":
$validationRule = $attribute->getValidationRule();
switch ($validationRule) {
case 'url':
$data = $this->faker->url();
break;
default:
$data = $this->faker->sentence();
break;
}
break;
case "text":
$data = $this->faker->sentence();
break;
case "date":
$data = $this->faker->dateTimeBetween($attribute->getDateMin(), $attribute->getDateMax());
$data = $data->format('Y-m-d');
break;
case "metric":
case "decimal":
case "prices":
if ($attribute->getBackendType() && preg_match('/-' . self::METRIC_UNIT . '$/', $key)) {
$data = $attribute->getDefaultMetricUnit();
} else {
$min = $attribute->getNumberMin() != null ? $attribute->getNumberMin() : self::DEFAULT_NUMBER_MIN;
$max = $attribute->getNumberMax() != null ? $attribute->getNumberMax() : self::DEFAULT_NUMBER_MAX;
$decimals = $attribute->isDecimalsAllowed() ? self::DEFAULT_NB_DECIMALS : 0;
$data = $this->faker->randomFloat($decimals, $min, $max);
}
break;
case "boolean":
$data = $this->faker->boolean() ? "1" : "0";
break;
case "option":
case "options":
$options = [];
foreach ($attribute->getOptions() as $option) {
$options[] = $option;
}
$option = $this->faker->randomElement($options);
if (is_object($option)) {
$data = $option->getCode();
}
break;
default:
$data = '';
break;
}
return (string) $data;
}
示例12: __construct
/**
* @param ChannelFactoryInterface $channelFactory
* @param RepositoryInterface $localeRepository
* @param RepositoryInterface $currencyRepository
* @param RepositoryInterface $paymentMethodRepository
* @param RepositoryInterface $shippingMethodRepository
*/
public function __construct(ChannelFactoryInterface $channelFactory, RepositoryInterface $localeRepository, RepositoryInterface $currencyRepository, RepositoryInterface $paymentMethodRepository, RepositoryInterface $shippingMethodRepository)
{
$this->channelFactory = $channelFactory;
$this->faker = \Faker\Factory::create();
$this->optionsResolver = (new OptionsResolver())->setDefault('name', function (Options $options) {
return $this->faker->words(3, true);
})->setDefault('code', function (Options $options) {
return StringInflector::nameToCode($options['name']);
})->setDefault('hostname', function (Options $options) {
return $options['code'] . '.localhost';
})->setDefault('color', function (Options $options) {
return $this->faker->colorName;
})->setDefault('enabled', function (Options $options) {
return $this->faker->boolean(90);
})->setAllowedTypes('enabled', 'bool')->setDefault('tax_calculation_strategy', 'order_items_based')->setAllowedTypes('tax_calculation_strategy', 'string')->setDefault('default_locale', function (Options $options) {
return $this->faker->randomElement($options['locales']);
})->setAllowedTypes('default_locale', LocaleInterface::class)->setNormalizer('default_locale', LazyOption::findOneBy($localeRepository, 'code'))->setDefault('locales', LazyOption::all($localeRepository))->setAllowedTypes('locales', 'array')->setNormalizer('locales', LazyOption::findBy($localeRepository, 'code'))->setDefault('default_currency', function (Options $options) {
return $this->faker->randomElement($options['currencies']);
})->setAllowedTypes('default_currency', CurrencyInterface::class)->setNormalizer('default_currency', LazyOption::findOneBy($currencyRepository, 'code'))->setDefault('currencies', LazyOption::all($currencyRepository))->setAllowedTypes('currencies', 'array')->setNormalizer('currencies', LazyOption::findBy($currencyRepository, 'code'))->setDefault('payment_methods', LazyOption::all($paymentMethodRepository))->setAllowedTypes('payment_methods', 'array')->setNormalizer('payment_methods', LazyOption::findBy($paymentMethodRepository, 'code'))->setDefault('shipping_methods', LazyOption::all($shippingMethodRepository))->setAllowedTypes('shipping_methods', 'array')->setNormalizer('shipping_methods', LazyOption::findBy($shippingMethodRepository, 'code'))->setDefault('theme_name', null);
}
示例13: createAddress
/**
* @return AddressInterface
*/
protected function createAddress()
{
/* @var $address AddressInterface */
$address = $this->getAddressRepository()->createNew();
$address->setFirstname($this->faker->firstName);
$address->setLastname($this->faker->lastName);
$address->setCity($this->faker->city);
$address->setStreet($this->faker->streetAddress);
$address->setPostcode($this->faker->postcode);
do {
$isoName = $this->faker->countryCode;
} while ('UK' === $isoName);
$country = $this->getReference('Sylius.Country.' . $isoName);
$province = $country->hasProvinces() ? $this->faker->randomElement($country->getProvinces()->toArray()) : null;
$address->setCountry($country);
$address->setProvince($province);
return $address;
}
示例14: createAddress
/**
* @return AddressInterface
*/
protected function createAddress()
{
/* @var $address AddressInterface */
$address = $this->getAddressFactory()->createNew();
$address->setFirstname($this->faker->firstName);
$address->setLastname($this->faker->lastName);
$address->setCity($this->faker->city);
$address->setStreet($this->faker->streetAddress);
$address->setPostcode($this->faker->postcode);
/** @var CountryInterface $country */
$countries = Intl::getRegionBundle()->getCountryNames($this->defaultLocale);
$isoName = array_rand($countries);
$country = $this->getReference("Sylius.Country." . $isoName);
$province = $country->hasProvinces() ? $this->faker->randomElement($country->getProvinces()->toArray()) : null;
$address->setCountry($country);
$address->setProvince($province);
return $address;
}
示例15: configureOptions
/**
* {@inheritdoc}
*/
protected function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefault('name', function (Options $options) {
return $this->faker->words(3, true);
})->setDefault('code', function (Options $options) {
return StringInflector::nameToCode($options['name']);
})->setDefault('hostname', function (Options $options) {
return $options['code'] . '.localhost';
})->setDefault('color', function (Options $options) {
return $this->faker->colorName;
})->setDefault('enabled', function (Options $options) {
return $this->faker->boolean(90);
})->setAllowedTypes('enabled', 'bool')->setDefault('tax_calculation_strategy', 'order_items_based')->setAllowedTypes('tax_calculation_strategy', 'string')->setDefault('default_locale', function (Options $options) {
return $this->faker->randomElement($options['locales']);
})->setAllowedTypes('default_locale', ['string', LocaleInterface::class])->setNormalizer('default_locale', LazyOption::findOneBy($this->localeRepository, 'code'))->setDefault('locales', LazyOption::all($this->localeRepository))->setAllowedTypes('locales', 'array')->setNormalizer('locales', LazyOption::findBy($this->localeRepository, 'code'))->setDefault('base_currency', function (Options $options) {
return $this->faker->randomElement($options['currencies']);
})->setAllowedTypes('base_currency', ['string', CurrencyInterface::class])->setNormalizer('base_currency', LazyOption::findOneBy($this->currencyRepository, 'code'))->setDefault('currencies', LazyOption::all($this->currencyRepository))->setAllowedTypes('currencies', 'array')->setNormalizer('currencies', LazyOption::findBy($this->currencyRepository, 'code'))->setDefault('theme_name', null);
}