So working in this new environment requires me to create classes differently. They take a lot longer to write but I can see the benefits.
I created the following class this morning, at the end of a 20 hour educational marathon. I think it can be improved significantly so I'm looking for feedback from you. Please copy and paste the code into your editor and have a look at it. Let me know what you would do differently and why. Even if it's a very minor issue, please let me know. I want to ensure that I write the best code I possibly can and I hope to learn from this.
The class:
Code: Select all
<?php
class randomString
{
/**
* Contains a string of numbers used in random strings.
* @var string A string of numeric characters
* @access private
*/
private $_numbers;
/**
* Contains a string of uppercase characters used in random strings.
* @var string A string of uppercase alpha characters
* @access private
*/
private $_upper;
/**
* Contains a string of lowercase characters used in random strings.
* @var string A string of lowercase characters
* @access private
*/
private $_lower;
/**
* Contains a string of symbols used in random strings.
* @var string A string of symbol characters
* @access private
*/
private $_symbols;
/**
* Whether to use numbers in the generated string or not
* @var bool
* @access private
*/
private $_use_numbers;
/**
* Whether to use uppercase letters in the generated string or not
* @var bool
* @access private
*/
private $_use_upper;
/**
* Whether to use lowercase letters in the generated string or not
* @var bool
* @access private
*/
private $_use_lower;
/**
* Whether to use symbols in the generated string or not
* @var bool
* @access private
*/
private $_use_symbols;
/**
* The length of the string to be generated
* @var int
* @access private
*/
private $_length;
/**
* The randomly generated string
* @var string
* @access private
*/
private $_random_string;
/**
* A map of options to their corresponding set method
* @var array
* @access private
*/
private $_option_map;
/**
* Class constructor
*
* @param array $options An array of options
*/
public function __construct($options = array()) {
$this->_option_map = array(
'lower' => 'setLower',
'upper' => 'setUpper',
'numbers' => 'setNumbers',
'symbols' => 'setSymbols',
'use_lower' => 'enableLower',
'use_upper' => 'enableUpper',
'use_numbers' => 'enableNumbers',
'use_symbols' => 'enableSymbols',
'length' => 'setLength',
);
$this->setDefaults();
$this->setOptions($options);
return $this;
}
/**
* <p>Sets all settings to default for random string creation.</p>
*
* <p>Default Settings:</p>
* numbers: 0123456789<br>
* upper: ABCDEFGHIJKLMNOPQRSTUVWXYZ<br>
* lower: abcdefghijklmnopqrstuvwxyz<br>
* symbols: !"#$%&'()*+,-./:;<=>?@[\]^_`{|}~<br>
* <br>
* use_lower: false<br>
* use_upper: true<br>
* use_numbers: true<br>
* use_symbols: false<br>
*
* @access public
*/
public function setDefaults() {
$this->_numbers = '0123456789';
$this->_upper = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
$this->_lower = 'abcdefghijklmnopqrstuvwxyz';
$this->_symbols = '!"#$%&\'()*+,-./:;<=>?@[\]^_`{|}~';
$this->_use_lower = false;
$this->_use_upper = true;
$this->_use_numbers = true;
$this->_use_symbols = false;
$this->_length = 10;
$this->_random_string = false;
}
/**
* Generates a random string
*
* @returns string A random string
*/
public function create() {
$charPool = '';
if ($this->_use_lower == true) {
$charPool .= $this->_lower;
}
if ($this->_use_upper == true) {
$charPool .= $this->_upper;
}
if ($this->_use_numbers == true) {
$charPool .= $this->_numbers;
}
if ($this->_use_symbols == true) {
$charPool .= $this->_symbols;
}
# subtract 1 because the string offsets start at 0
$charPoolLen = strlen($charPool) - 1;
$this->_random_string = '';
for ($i = 0; $i < $this->_length; $i++) {
$this->_random_string .= $charPool[mt_rand(0, $charPoolLen)];
}
return $this->_random_string;
}
/**
*
* @access public
*/
public function get() {
if ($this->_random_string === false) {
$this->create();
}
return $this->_random_string;
}
/**
* Sets the string of lowercase characters used for creating the random
* string.
*
* @var string $string The string of characters. The value will be
* converted to lowercase.
* @access public
* @returns An instance of this class.
*/
public function setLower($string) {
$this->_lower = strtolower((string) $string);
return $this;
}
/**
* Sets the string of upper case characters used for creating the random
* string.
*
* @var string $string The string of characters. The value will be
* converted to uppercase.
* @access public
* @returns An instance of this class.
*/
public function setUpper($string) {
$this->_upper = strtoupper((string) $string);
return $this;
}
/**
* Sets the string of numerical characters used for creating the random
* string.
*
* @var string $string The string of numbers.
* @access public
* @returns An instance of this class.
*/
public function setNumbers($string) {
$this->_numbers = (string) $string;
return $this;
}
/**
* Sets the string of symbols used for creating the random string.
*
* @var string $string The string of symbols.
* @access public
* @returns An instance of this class.
*/
public function setSymbols($string) {
$this->_symbols = (string) $string;
return $this;
}
/**
* Sets whether lower case characters will be used when creating the
* random string
*
* @var bool $bool true to enable, false to disable
* @access public
* @returns An instance of this class.
*/
public function enableLower($bool) {
$this->_use_lower = (boolean) $bool;
return $this;
}
/**
* Sets whether upper case characters will be used when creating the
* random string
*
* @var bool $bool true to enable, false to disable
* @access public
* @returns An instance of this class
*/
public function enableUpper($bool) {
$this->_use_upper = (boolean) $bool;
return $this;
}
/**
* Sets whether symbols will be used when creating the random string
*
* @var bool $bool true to enable, false to disable
* @access public
* @returns An instance of this class
*/
public function enableSymbols($bool) {
$this->_use_symbols = (boolean) $bool;
return $this;
}
/**
* Sets the length of the generated random string
*
* @var int $int The length of the string
* @access public
* @returns An instance of this class
*/
public function setLength($int) {
$this->_length = (int) $int;
return $this;
}
/**
* Sets class options
* @var array $options An array of options. defaults, numbers, upper, lower,
* symbols
* @access public
* @returns An instance of this class
*/
public function setOptions($options) {
if (!is_array($options)) {
trigger_error("Argument passed to randomString->setOptions() must be an array", E_USER_ERROR);
return $this;
}
foreach ($this->_option_map as $option => $method) {
if (isset($options[$option])) {
$this->$method($options[$option]);
unset($options[$option]);
}
}
if (count($options) > 0) {
foreach ($options as $option) {
trigger_error(sprintf("%s passed to randomString->setOptions() is not an allowable option", $option), E_USER_ERROR);
}
}
$this->_random_string = false;
return $this;
}
/**
* Returns the random string and generates one if it has not been created
* yet
* @return string A random string
* @see getString()
*/
public function __toString() {
return $this->get();
}
}
Code: Select all
<?php
require_once dirname(__FILE__).'/../bootstrap/unit.php';
$t = new lime_test(182, new lime_output_color());
/*
* generate 20 random strings
* 4 tests on each string: total = 80
* default settings
*/
for($i = 0; $i < 20; $i++) {
$ran = new randomString();
$randomString = $ran->get();
$t->comment("randomString() - generated $randomString");
$t->is(strlen($randomString), 10, 'randomString()->createString() length is 10 characters');
$t->unlike($randomString, '#[a-z]+#', 'randomString()->createString() does not contain lower case characters');
$t->like($randomString, '#^[A-Z0-9]+$#', 'randomString()->createString() contains only upper case letters and numbers');
$t->unlike($randomString, '#[!"\#\$%&\'()*+,-./:;<=>?@[\]^_`{|}~]+#', 'randomString()->createString() does not contain symbols');
}
/*
* generate 20 random strings
* 5 tests on each string: total = 100
* turn on symbols and lowercase letters
* change length to 40
*/
for($i = 0; $i < 20; $i++) {
$ran = new randomString(array(
'length' => 40,
'use_lower' => true,
'use_symbols' => true,
));
$randomString = $ran->get();
$t->comment("randomString() - generated $randomString");
$t->is(strlen($randomString), 40, 'randomString()->createString() length is 40 characters');
$t->like($randomString, '#[a-z]+#', 'randomString()->createString() does contain lower case characters');
$t->like($randomString, '#[A-Z]+#', 'randomString()->createString() does contain upper case characters');
$t->like($randomString, '#^[a-zA-Z0-9!"\#\$%&\'()*+,\-\./:;\\\=>?@[\]^_`{|}~]+$#', 'randomString()->createString() contains lower, upper, numbers and symbols');
$t->like($randomString, '#[!"\#\$%&\'()*+,\-\./:;\\\=>?@[\]^_`{|}~]+#', 'randomString()->createString() does contain symbols');
}
$ran = new randomString();
$string = $ran->enableLower(false)->enableSymbols(true)->get();
$t->comment("enableLower(false)->enableSymbols(true)->get() - generated $string");
$t->unlike($string, '#[a-z]+#', 'randomStenableLower(false)->enableSymbols(true)->get() does contain lower case characters');
$t->like($string, '#[!"\#\$%&\'()*+,\-\./:;\\\=>?@[\]^_`{|}~]+#', 'enableLower(false)->enableSymbols(true)->get() does contain symbols');
Code: Select all
$ran = (string) new randomString();
echo "$ran\n";
$ran = (string) new randomString(array('length' => 25));
echo "$ran\n";
$ran = new randomString();
$string = $ran->setLength(20)->
setLower('abcde')->
setUpper('FGHIJ')->
setNumbers('123')->
enableLower(true)->
get();
echo "$string\n";
$ran = new randomString(array(
'length' => 40,
'use_lower' => true,
'use_symbols' => true,
));
$string = $ran->get();
echo "$string\n";