From 1f744088f29b64cee99ee70a212f989890817c0c Mon Sep 17 00:00:00 2001 From: "Timothy J. Warren" Date: Thu, 24 Apr 2014 13:08:26 -0400 Subject: [PATCH] Reduce complexity of create_table method --- common.php | 30 ++++++++++++++++++++++++++++++ core/abstract/abstract_util.php | 17 ++++------------- 2 files changed, 34 insertions(+), 13 deletions(-) diff --git a/common.php b/common.php index 5289812..f7898ed 100644 --- a/common.php +++ b/common.php @@ -74,6 +74,36 @@ function db_filter($array, $index) // -------------------------------------------------------------------------- +/** + * Zip a set of arrays together on common keys + * + * The $zipper_input array is an array of arrays indexed by their place in the output + * array. + * + * @param array $zipper_input + * @return array + */ +function array_zipper(Array $zipper_input) +{ + $output = array(); + + foreach($zipper_input as $append_key => $values) + { + foreach($values as $index => $value) + { + if ( ! isset($output[$index])) + { + $output[$index] = array(); + } + $output[$index][$append_key] = $value; + } + } + + return $output; +} + +// -------------------------------------------------------------------------- + /** * Connection function * diff --git a/core/abstract/abstract_util.php b/core/abstract/abstract_util.php index 60afac5..1942e7d 100644 --- a/core/abstract/abstract_util.php +++ b/core/abstract/abstract_util.php @@ -80,19 +80,10 @@ abstract class Abstract_Util { // 'constraint' => ..., // 'index' => ..., // ) - foreach($fields as $colname => $type) - { - $column_array[$colname] = array(); - $column_array[$colname]['type'] = ($type !== $colname) ? $type : ''; - } - - if( ! empty($constraints)) - { - foreach($constraints as $col => $const) - { - $column_array[$col]['constraint'] = $const; - } - } + $column_array = \array_zipper(array( + 'type' => $fields, + 'constraint' => $constraints + )); // Join column definitions together $columns = array();