Moving Object::enum() to Set::enum()

git-svn-id: https://svn.cakephp.org/repo/branches/1.2.x.x@4498 3807eeeb-6ff5-0310-8944-8be069107fe0
This commit is contained in:
nate 2007-02-10 16:02:45 +00:00
parent d8a8cbfb92
commit 6331a6d4c4
2 changed files with 29 additions and 25 deletions

View file

@ -165,6 +165,35 @@ class Set extends Object {
}
return $numeric;
}
/**
* Return a value from an array list if the key exists.
*
* If a comma separated $list is passed arrays are numeric with the key of the first being 0
* $list = 'no, yes' would translate to $list = array(0 => 'no', 1 => 'yes');
*
* If an array is used, keys can be strings example: array('no' => 0, 'yes' => 1);
*
* $list defaults to 0 = no 1 = yes if param is not passed
*
* @param mixed $selected
* @param mixed $list can be an array or a comma-separated list.
* @return string the value of the array key or null if no match
*/
function enum($select, $list = null) {
if (empty($list) && is_a($this, 'Set')) {
$list = $this->get();
} elseif (empty($list)) {
$list = array('no', 'yes');
}
$return = null;
$list = Set::normalize($list, false);
if (array_key_exists($select, $list)) {
$return = $list[$select];
}
return $return;
}
/**
* Gets a value from an array or object.
* The special {n}, as seen in the Model::generateList method, is taken care of here.