|
|
@@ -296,6 +296,51 @@ class Set extends Object {
|
|
|
}
|
|
|
return $return;
|
|
|
}
|
|
|
+/**
|
|
|
+ * Normalizes a string or array list
|
|
|
+ *
|
|
|
+ * @param mixed $list
|
|
|
+ * @param boolean $assoc If true, $list will be converted to an associative array
|
|
|
+ * @param string $sep If $list is a string, it will be split into an array with $sep
|
|
|
+ * @param boolean $trim If true, separated strings will be trimmed
|
|
|
+ * @return array
|
|
|
+ */
|
|
|
+ function normalize($list, $assoc = true, $sep = ',', $trim = true) {
|
|
|
+ if (is_string($list)) {
|
|
|
+ $list = explode($sep, $list);
|
|
|
+ if ($trim) {
|
|
|
+ $list = array_map('trim', $list);
|
|
|
+ }
|
|
|
+ if ($assoc) {
|
|
|
+ return Set::normalize($list);
|
|
|
+ }
|
|
|
+ } elseif (is_array($list)) {
|
|
|
+ $keys = array_keys($list);
|
|
|
+ $count = count($keys);
|
|
|
+ $numeric = true;
|
|
|
+
|
|
|
+ if (!$assoc) {
|
|
|
+ for ($i = 0; $i < $count; $i++) {
|
|
|
+ if (!is_int($keys[$i])) {
|
|
|
+ $numeric = false;
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if (!$numeric || $assoc) {
|
|
|
+ $newList = array();
|
|
|
+ for ($i = 0; $i < $count; $i++) {
|
|
|
+ if (is_int($keys[$i])) {
|
|
|
+ $newList[$list[$keys[$i]]] = null;
|
|
|
+ } else {
|
|
|
+ $newList[$keys[$i]] = $list[$keys[$i]];
|
|
|
+ }
|
|
|
+ }
|
|
|
+ $list = $newList;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return $list;
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
?>
|