@@ -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
0 commit comments