Skip to content

Commit cc07063

Browse files
authored
Update the Generator::getFormatter to support extensions (#267)
1 parent 4e3bc9d commit cc07063

File tree

2 files changed

+56
-11
lines changed

2 files changed

+56
-11
lines changed

src/Faker/Generator.php

Lines changed: 33 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -617,31 +617,44 @@ public function seed($seed = null)
617617
}
618618
}
619619

620-
public function format($formatter, $arguments = [])
620+
public function format($format, $arguments = [])
621621
{
622-
return call_user_func_array($this->getFormatter($formatter), $arguments);
622+
return call_user_func_array($this->getFormatter($format), $arguments);
623623
}
624624

625625
/**
626-
* @param string $formatter
626+
* @param string $format
627627
*
628628
* @return callable
629629
*/
630-
public function getFormatter($formatter)
630+
public function getFormatter($format)
631631
{
632-
if (isset($this->formatters[$formatter])) {
633-
return $this->formatters[$formatter];
632+
if (isset($this->formatters[$format])) {
633+
return $this->formatters[$format];
634+
}
635+
636+
if (method_exists($this, $format)) {
637+
$this->formatters[$format] = [$this, $format];
638+
639+
return $this->formatters[$format];
640+
}
641+
642+
// "Faker\Core\Barcode->ean13"
643+
if (preg_match('|^([a-zA-Z0-9\\\]+)->([a-zA-Z0-9]+)$|', $format, $matches)) {
644+
$this->formatters[$format] = [$this->ext($matches[1]), $matches[2]];
645+
646+
return $this->formatters[$format];
634647
}
635648

636649
foreach ($this->providers as $provider) {
637-
if (method_exists($provider, $formatter)) {
638-
$this->formatters[$formatter] = [$provider, $formatter];
650+
if (method_exists($provider, $format)) {
651+
$this->formatters[$format] = [$provider, $format];
639652

640-
return $this->formatters[$formatter];
653+
return $this->formatters[$format];
641654
}
642655
}
643656

644-
throw new \InvalidArgumentException(sprintf('Unknown formatter "%s"', $formatter));
657+
throw new \InvalidArgumentException(sprintf('Unknown format "%s"', $format));
645658
}
646659

647660
/**
@@ -653,7 +666,11 @@ public function getFormatter($formatter)
653666
*/
654667
public function parse($string)
655668
{
656-
return preg_replace_callback('/\{\{\s?(\w+)\s?\}\}/u', [$this, 'callFormatWithMatches'], $string);
669+
$callback = function ($matches) {
670+
return $this->format($matches[1]);
671+
};
672+
673+
return preg_replace_callback('/\{\{\s?(\w+)\s?\}\}/u', $callback, $string);
657674
}
658675

659676
/**
@@ -839,8 +856,13 @@ public function semver(bool $preRelease = false, bool $build = false): string
839856
return $this->ext(Extension\VersionExtension::class)->semver($preRelease, $build);
840857
}
841858

859+
/**
860+
* @deprecated
861+
*/
842862
protected function callFormatWithMatches($matches)
843863
{
864+
trigger_deprecation('fakerphp/faker', '1.14', 'Protected method "callFormatWithMatches()" is deprecated and will be removed.');
865+
844866
return $this->format($matches[1]);
845867
}
846868

test/Faker/GeneratorTest.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,11 @@
22

33
namespace Faker\Test;
44

5+
use Faker\Core\Blood;
56
use Faker\Core\File;
7+
use Faker\Extension\BloodExtension;
68
use Faker\Extension\Container;
9+
use Faker\Extension\ContainerBuilder;
710
use Faker\Extension\ExtensionNotFound;
811
use Faker\Extension\FileExtension;
912
use Faker\Generator;
@@ -111,6 +114,26 @@ public function testFormatTransfersArgumentsToFormatter()
111114
self::assertEquals('bazfoo', $this->faker->format('fooFormatterWithArguments', ['foo']));
112115
}
113116

117+
public function testFormatterCallsGenerator()
118+
{
119+
$builder = new ContainerBuilder();
120+
$builder->add(Blood::class, BloodExtension::class);
121+
$faker = new Generator($builder->build());
122+
123+
$output = $faker->format('bloodType');
124+
self::assertTrue(in_array($output, ['A', 'AB', 'B', '0'], true));
125+
}
126+
127+
public function testFormatterCallsExtension()
128+
{
129+
$builder = new ContainerBuilder();
130+
$builder->add(Blood::class);
131+
$faker = new Generator($builder->build());
132+
133+
$output = $faker->format('Faker\Core\Blood->bloodType');
134+
self::assertTrue(in_array($output, ['A', 'AB', 'B', '0'], true));
135+
}
136+
114137
public function testParseReturnsSameStringWhenItContainsNoCurlyBraces()
115138
{
116139
self::assertEquals('fooBar#?', $this->faker->parse('fooBar#?'));

0 commit comments

Comments
 (0)