Search code examples
phprandomgeneratorcurly-braces

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


I stumbled on a script that generates random names from two different types of words in an array. Code is following:

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

Basically, the script should create and return a random combination of words as defined in $companyNameFormats. This script requires Faker\Factory, but I'd like to make it independent. At this point, there are 2 problems:

randomElement as an undefined method, and generator->parse as Call to a member function parse() on null

I've managed to modify the script and make it work, but I am interested in how can I use the {{}} as given in $companyNameFormats and return the result without using an external library?

The modified script is as follows:

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

Solution

  • They're probably doing something like preg_replace.

    Quick and dirty example:

    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