Skip to content
This repository has been archived by the owner on Mar 18, 2022. It is now read-only.

Commit

Permalink
Added getRandomKey method to get random item of arrays
Browse files Browse the repository at this point in the history
  • Loading branch information
Alireza Josheghani committed Feb 4, 2017
1 parent 7a63927 commit a47a1fd
Showing 1 changed file with 34 additions and 30 deletions.
64 changes: 34 additions & 30 deletions src/Generator.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,7 @@ public function __construct()
*/
public function firstname()
{
$name = array_rand($this->objects['names']);

return $this->objects['names'][$name];
return $this->getRandomKey('names');
}

/**
Expand All @@ -46,9 +44,7 @@ public function firstname()
*/
public function lastname()
{
$name = array_rand($this->objects['families']);

return $this->objects['families'][$name];
return $this->getRandomKey('families');
}


Expand All @@ -61,9 +57,7 @@ public function lastname()
*/
public function company()
{
$name = array_rand($this->objects['companies']);

return $this->objects['companies'][$name];
return $this->getRandomKey('companies');
}

/**
Expand All @@ -87,9 +81,7 @@ public function fullname()
*/
public function mobile()
{
$prefix = array_rand($this->objects['prefixTelePhones']);

$prefix = $this->objects['prefixTelePhones'][$prefix];
$prefix = $this->getRandomKey('prefixTelePhones');

return string('0' . $prefix . randomNumber(7));
}
Expand All @@ -103,9 +95,7 @@ public function mobile()
*/
public function telephone()
{
$prefix = array_rand($this->objects['prefixPhones']);

$prefix = $this->objects['prefixPhones'][$prefix];
$prefix = $this->getRandomKey('prefixPhones');

return string('0' . $prefix . randomNumber(7));
}
Expand All @@ -119,9 +109,7 @@ public function telephone()
*/
public function email()
{
$service = array_rand($this->objects['mailServices']);

$service = $this->objects['mailServices'][$service];
$service = $this->getRandomKey('mailServices');

return string(randomString(30,'lowercase') . '@' . $service);
}
Expand All @@ -135,9 +123,7 @@ public function email()
*/
public function domain()
{
$domain = array_rand($this->objects['domains']);

$domain = $this->objects['domains'][$domain];
$domain = $this->getRandomKey('domains');

return string(randomString(20,'lowercase') . $domain);
}
Expand All @@ -151,9 +137,7 @@ public function domain()
*/
public function website()
{
$protocol = array_rand($this->objects['protocols']);

$protocol = $this->objects['protocols'][$protocol];
$protocol = $this->getRandomKey('domains');

return string($protocol . '://www' . $this->domain());
}
Expand Down Expand Up @@ -203,9 +187,7 @@ public function age($min = 16, $max = 70)
*/
public function address()
{
$name = array_rand($this->objects['address']);

return string($this->objects['address'][$name]);
return $this->getRandomKey('address');
}

/**
Expand All @@ -217,9 +199,7 @@ public function address()
*/
public function city()
{
$name = array_rand($this->objects['city']);

return string($this->objects['city'][$name]);
return $this->getRandomKey('city');
}

/**
Expand Down Expand Up @@ -273,4 +253,28 @@ public function meliCode()
return $code;
}

/**
* Get random key from array
*
* @author Alireza Josheghani <josheghani.dev@gmail.com>
* @since 4 Feb 2017
* @param $object
* @return string
*/
private function getRandomKey($object = null)
{
$name = null;
$array = [];

if(is_array($object)){
$array = $object;
$name = array_rand($object);
} elseif(is_string($object)) {
$array = $this->objects[$object];
$name = array_rand($array);
}

return string($array[$name]);
}

}

0 comments on commit a47a1fd

Please sign in to comment.