PHP class 中的双花括号 - 如何使用它们从 2 个字符串和 return 值生成随机字符串?

Double curly braces in PHP class - how ti use them to generate random string from 2 strings and return the value?

我偶然发现了一个脚本,它可以从数组中的两种不同类型的单词中生成随机名称。代码如下:

    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);
    }

基本上,脚本应该创建 return $companyNameFormats 中定义的随机单词组合。此脚本需要 Faker\Factory,但我想让它独立。此时,有2个问题:

randomElement 作为未定义的方法,generator->parse 作为 Call to a member function parse() on null

我已经设法修改脚本并使其工作,但我感兴趣的是如何使用 $companyNameFormats 中给出的 {{}} 和 return 结果而不使用外部图书馆?

修改后的脚本如下:

    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

基于@Kyrre 代码,这是最终的工作示例:

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::getRandomElement(static::$techTerms);
    }

    public static function culinaryTerm()
    {
        return static::getRandomElement(static::$culinaryTerms);
    }

    protected static function getRandomElement(array $elements): string
    {
        return $elements[random_int(0, count($elements) - 1)];
    }

    public function get(): string
    {
        $format = $this->getRandomElement(self::$companyNameFormats);


        $format = preg_replace(
            "/{{techTerm}}/",
            $this->getRandomElement(self::$techTerms),
            $format
        );

        return preg_replace(
            "/{{culinaryTerm}}/",
            $this->getRandomElement(self::$culinaryTerms),
            $format
        );
    }