php - PHP类中的双花括号-如何使用它们从2个字符串生成随机字符串并返回值?
问题描述
我偶然发现了一个脚本,它从数组中的两种不同类型的单词生成随机名称。代码如下:
protected static $techTerms = array('AddOn', 'Algorithm', 'Architect', 'Array', 'Asynchronous', 'Avatar', 'Band', 'Base', 'Beta', 'Binary');
protected static $culinaryTerms = array('Appetit', 'Bake', 'Beurre', 'Bistro', 'Blend', 'Boil', 'Bouchees', 'Brew', 'Buffet', 'Caffe', 'Caffeine', 'Cake');
protected static $companyNameFormats = array(
'{{techTerm}}{{culinaryTerm}}',
'{{techTerm}}{{techTerm}}',
'{{culinaryTerm}}{{techTerm}}'
);
public static function techTerm()
{
return static::randomElement(static::$techTerms);
}
public static function culinaryTerm()
{
return static::randomElement(static::$culinaryTerms);
}
public function companyName()
{
$format = static::randomElement(static::$companyNameFormats);
return $this->generator->parse($format);
}
基本上,脚本应该创建并返回$companyNameFormats
. 这个脚本需要 Faker\Factory,但我想让它独立。此时,有2个问题:
randomElement
作为一个未定义的方法,并且generator->parse
作为Call to a member function parse() on null
我已设法修改脚本并使其工作,但我对如何使用给出的 {{}}$companyNameFormats
并返回结果而不使用外部库感兴趣?
修改后的脚本如下:
protected static function companyNameFormats()
{
$techArray = [];
$techArray[] = self::techTerm();
$culinaryArray = [];
$culinaryArray[] = self::culinaryTerm();
$result = array(
array_merge($techArray, $culinaryArray),
array_merge($techArray, $culinaryArray),
array_merge($culinaryArray, $techArray),
array_merge($techArray, $culinaryArray),
array_merge($culinaryArray, $techArray)
);
return $result;
}
public static function techTerm()
{
$techTermKey = array_rand(static::$techTerms, 1);
$techTermValue = static::$techTerms[$techTermKey];
return $techTermValue;
}
public static function culinaryTerm()
{
$culinaryTermsKey = array_rand(static::$culinaryTerms, 1);
$culinaryTermsValue = static::$culinaryTerms[$culinaryTermsKey];
return $culinaryTermsValue;
}
public function companyName()
{
$companyNameKey = array_rand(static::companyNameFormats(), 1);
$companyNameValue = static::companyNameFormats()[$companyNameKey];
return $companyNameValue;
}
解决方案
他们可能正在做类似的事情preg_replace
。
快速而肮脏的例子:
class Foo {
protected static array $foods = ["Pie", "Cake", "Yoghurt"];
protected static array $animals = ["Dog", "Cat", "Giraffe"];
protected static array $formats = [
"{{food}} {{animal}}",
"{{animal}} {{food}}"
];
public function get():string{
$format = $this->getRandomElement(self::$formats);
$format = preg_replace(
"/{{animal}}/",
$this->getRandomElement(self::$animals),
$format
);
return preg_replace(
"/{{food}}/",
$this->getRandomElement(self::$foods),
$format
);
}
protected function getRandomElement(array $elements):string{
return $elements[random_int(0, count($elements) - 1)];
}
}
echo (new Foo())->get(); // Cat Yoghurt
推荐阅读
- apache - Apache 虚拟主机 -- 如何设置子域以指向游戏服务器
- android - Beacon beacon = new Beacon.Builder () .setId1 (sysid) .setId2 (IdUser) Android Beacon Library 中发布和扫描IdUser时出错
- python - 烧瓶中 URL 中没有 cookie 和 sid 的双向通信
- html - 在 Heroku 上部署时 CSS 无法正确应用
- python-3.x - 内核中 printf 的 PyOpenCL 问题
- php - php中的数组格式化(使用laravel框架)
- typescript - 在 TypeScript 中迭代 Json 文件
- c++ - codechef 的主要游戏问题中的 SIGSTP 错误。TLG问题
- reactjs - react-testing-library 正确测试条件渲染
- c# - 如何使用 Selenium WebDriver 和 C# 在 div 中定位按钮