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(
'',
'',
''
);
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;
}
Aucun commentaire:
Enregistrer un commentaire