/** * @param int $width * @param int $height * @param string $extension * @return string */ private function buildFolderPath($width, $height, $extension) { if (null === ($disposition = Image::resolveDisposition($width, $height))) { $disposition = Faker::randomElement([Image::LANDSCAPE, Image::PORTRAIT]); } return sprintf('%s/%s/%s', $this->folder, $disposition, $extension); }
/** * @param \Faker\Generator $generator * @param Mandango $mandango * @return array */ public function guessColumnFormatters(\Faker\Generator $generator, Mandango $mandango) { $formatters = array(); $nameGuesser = new \Faker\Guesser\Name($generator); $columnTypeGuesser = new \Faker\ORM\Mandango\ColumnTypeGuesser($generator); $metadata = $mandango->getMetadata($this->class); // fields foreach ($metadata['fields'] as $fieldName => $field) { if ($formatter = $nameGuesser->guessFormat($fieldName)) { $formatters[$fieldName] = $formatter; continue; } if ($formatter = $columnTypeGuesser->guessFormat($field)) { $formatters[$fieldName] = $formatter; continue; } } // references foreach (array_merge($metadata['referencesOne'], $metadata['referencesMany']) as $referenceName => $reference) { if (!isset($reference['class'])) { continue; } $referenceClass = $reference['class']; $formatters[$referenceName] = function ($insertedEntities) use($referenceClass) { if (isset($insertedEntities[$referenceClass])) { return Base::randomElement($insertedEntities[$referenceClass]); } }; } return $formatters; }
/** * {@inheritdoc} * * @param FixtureMatchReferenceValue $value * * @throws UnresolvableValueException */ public function resolve(ValueInterface $value, FixtureInterface $fixture, ResolvedFixtureSet $fixtureSet, array $scope, GenerationContext $context) : ResolvedValueWithFixtureSet { if (null === $this->resolver) { throw ResolverNotFoundExceptionFactory::createUnexpectedCall(__METHOD__); } $possibleIds = $this->getSuitableIds($value, $fixtureSet); $id = Base::randomElement($possibleIds); if (null === $id) { throw UnresolvableValueExceptionFactory::createForNoFixtureOrObjectMatchingThePattern($value); } return $this->resolver->resolve(new FixtureReferenceValue($id), $fixture, $fixtureSet, $scope, $context); }
/** * {@inheritdoc} */ protected function execute(InputInterface $input, OutputInterface $output) { $imagine = new Imagine(); $width = $input->getOption('width') ?: Faker::numberBetween(200, 1200); $height = $input->getOption('height') ?: Faker::numberBetween(200, 1200); $extension = $input->getOption('extension') ?: Faker::randomElement($this->getAvailableExtensions()); $selector = new Selector($input->getOption('samples')); $images = $selector->select($width, $height, $extension); $filter = array_filter($images, function (Image $image) use($width, $height) { return $width * $height >= $image->getPixels(); }); if (true === empty($filter)) { $image = Faker::randomElement($images); } else { $image = Faker::randomElement($filter); } $transformed = tempnam(sys_get_temp_dir(), uniqid()); $transformation = new Transformation(); $transformation->thumbnail(new Box($width, $height), ImageInterface::THUMBNAIL_OUTBOUND)->save($transformed); $transformation->apply($imagine->open($image->getPath())); $this->getFormatter($input->getOption('formatter'))->output(new Image($transformed), $output); }
public function testRandomElementReturnsElementFromArray() { $elements = array('23', 'e', 32, '#'); $this->assertContains(BaseProvider::randomElement($elements), $elements); }
/** * Transforms a basic regular expression into a random string satisfying the expression. * * @example $faker->regexify('[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}'); // sm0@y8k96a.ej * * Regex delimiters '/.../' and begin/end markers '^...$' are ignored. * * Only supports a small subset of the regex syntax. For instance, * unicode, negated classes, unbouned ranges, subpatterns, back references, * assertions, recursive patterns, and comments are not supported. Escaping * support is extremely fragile. * * This method is also VERY slow. Use it only when no other formatter * can generate the fake data you want. For instance, prefer calling * `$faker->email` rather than `regexify` with the previous regular * expression. * * Also note than `bothify` can probably do most of what this method does, * but much faster. For instance, for a dummy email generation, try * `$faker->bothify('?????????@???.???')`. * * @see https://github.com/icomefromthenet/ReverseRegex for a more robust implementation * * @param string $regex A regular expression (delimiters are optional) * @return string */ public static function regexify($regex = '') { // ditch the anchors $regex = preg_replace('/^\\/?\\^?/', '', $regex); $regex = preg_replace('/\\$?\\/?$/', '', $regex); // All {2} become {2,2} $regex = preg_replace('/\\{(\\d+)\\}/', '{\\1,\\1}', $regex); // Single-letter quantifiers (?, *, +) become bracket quantifiers ({0,1}, {0,rand}, {1, rand}) $regex = preg_replace('/(?<!\\\\)\\?/', '{0,1}', $regex); $regex = preg_replace('/(?<!\\\\)\\*/', '{0,' . static::randomDigitNotNull() . '}', $regex); $regex = preg_replace('/(?<!\\\\)\\+/', '{1,' . static::randomDigitNotNull() . '}', $regex); // [12]{1,2} becomes [12] or [12][12] $regex = preg_replace_callback('/(\\[[^\\]]+\\])\\{(\\d+),(\\d+)\\}/', function ($matches) { return str_repeat($matches[1], Base::randomElement(range($matches[2], $matches[3]))); }, $regex); // (12|34){1,2} becomes (12|34) or (12|34)(12|34) $regex = preg_replace_callback('/(\\([^\\)]+\\))\\{(\\d+),(\\d+)\\}/', function ($matches) { return str_repeat($matches[1], Base::randomElement(range($matches[2], $matches[3]))); }, $regex); // A{1,2} becomes A or AA or \d{3} becomes \d\d\d $regex = preg_replace_callback('/(\\\\?.)\\{(\\d+),(\\d+)\\}/', function ($matches) { return str_repeat($matches[1], Base::randomElement(range($matches[2], $matches[3]))); }, $regex); // (this|that) becomes 'this' or 'that' $regex = preg_replace_callback('/(?<!\\\\)\\((.*?)\\)/', function ($matches) { return Base::randomElement(explode('|', str_replace(array('(', ')'), '', $matches[1]))); }, $regex); // All A-F inside of [] become ABCDEF $regex = preg_replace_callback('/\\[([^\\]]+)\\]/', function ($matches) { return '[' . preg_replace_callback('/(\\w|\\d)\\-(\\w|\\d)/', function ($range) { return join(range($range[1], $range[2]), ''); }, $matches[1]) . ']'; }, $regex); // All [ABC] become B (or A or C) $regex = preg_replace_callback('/\\[([^\\]]+)\\]/', function ($matches) { return Base::randomElement(str_split($matches[1])); }, $regex); // replace \d with number and \w with letter and . with ascii $regex = preg_replace_callback('/\\\\w/', 'static::randomLetter', $regex); $regex = preg_replace_callback('/\\\\d/', 'static::randomDigit', $regex); $regex = preg_replace_callback('/(?<!\\\\)\\./', 'static::randomAscii', $regex); // remove remaining backslashes $regex = str_replace('\\', '', $regex); // phew return $regex; }
public function testRandomElementReturnsElementFromAssociativeArray() { $elements = array('tata' => '23', 'toto' => 'e', 'tutu' => 32, 'titi' => '#'); $this->assertContains(BaseProvider::randomElement($elements), $elements); }
public static function randomElement($array) { return parent::randomElement($array); }
/** * @return string Random job abbreviation title */ public function jobAbbreviation() { return BaseProvider::randomElement($this->abbreviationProvider); }
/** * The first call generate unique values. This is to ensure all values are called before generating duplicates. * * @return string Random Symfony role. * * TODO: take into account users hierarchy too! */ public function userRole() { return BaseProvider::randomElement($this->userRoles->getRoles()); }