Skip to content

Commit d066a4a

Browse files
committed
Extracting method out.
1 parent ec443c4 commit d066a4a

File tree

1 file changed

+35
-18
lines changed

1 file changed

+35
-18
lines changed

cake/console/libs/tasks/model.php

Lines changed: 35 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,32 @@ function &_getModelObject($className) {
142142
return $object;
143143
}
144144

145+
/**
146+
* Generate a key value list of options and a prompt.
147+
*
148+
* @param array $options Array of options to use for the selections. indexes must start at 0
149+
* @param string $prompt Prompt to use for options list.
150+
* @param integer $default The default option for the given prompt.
151+
* @return result of user choice.
152+
**/
153+
function inOptions($options, $prompt = null, $default = null) {
154+
$valid = false;
155+
$max = count($options);
156+
while (!$valid) {
157+
foreach ($options as $i => $option) {
158+
$this->out($i + 1 .'. ' . $option);
159+
}
160+
if (empty($prompt)) {
161+
$prompt = __('Make a selection from the choices above', true);
162+
}
163+
$choice = $this->in($prompt, null, $default);
164+
if (intval($choice) > 0 && intval($choice) <= $max) {
165+
$valid = true;
166+
}
167+
}
168+
return $choice - 1;
169+
}
170+
145171
/**
146172
* Handles interactive baking
147173
*
@@ -578,28 +604,24 @@ function doMoreAssociations($model, $associations) {
578604
$wannaDoMoreAssoc = $this->in($prompt, array('y','n'), 'n');
579605
$possibleKeys = $this->_generatePossibleKeys();
580606
while (low($wannaDoMoreAssoc) == 'y') {
581-
$assocs = array(1 => 'belongsTo', 2 => 'hasOne', 3 => 'hasMany', 4 => 'hasAndBelongsToMany');
607+
$assocs = array('belongsTo', 'hasOne', 'hasMany', 'hasAndBelongsToMany');
582608
$this->out(__('What is the association type?', true));
583-
$prompt = "1. belongsTo\n";
584-
$prompt .= "2. hasOne\n";
585-
$prompt .= "3. hasMany\n";
586-
$prompt .= "4. hasAndBelongsToMany\n";
587-
$assocType = intval($this->in($prompt, array_keys($assocs), __("Enter a number", true)));
588-
609+
$assocType = intval($this->inOptions($assocs, __('Enter a number',true)));
610+
589611
$this->out(__("For the following options be very careful to match your setup exactly.\nAny spelling mistakes will cause errors.", true));
590612
$this->hr();
591613

592614
$alias = $this->in(__('What is the alias for this association?', true));
593615
$className = $this->in(sprintf(__('What className will %s use?', true), $alias), null, $alias );
594616
$suggestedForeignKey = null;
595617

596-
if ($assocType == '1') {
618+
if ($assocType == 0) {
597619
$showKeys = $possibleKeys[$model->table];
598620
$suggestedForeignKey = $this->_modelKey($alias);
599621
} else {
600622
$otherTable = Inflector::tableize($className);
601623
if (in_array($otherTable, $this->__tables)) {
602-
if ($assocType < '4') {
624+
if ($assocType < 3) {
603625
$showKeys = $possibleKeys[$otherTable];
604626
} else {
605627
$showKeys = null;
@@ -612,18 +634,13 @@ function doMoreAssociations($model, $associations) {
612634
}
613635
if (!empty($showKeys)) {
614636
$this->out(__('A helpful List of possible keys', true));
615-
for ($i = 0; $i < count($showKeys); $i++) {
616-
$this->out($i + 1 . ". " . $showKeys[$i]);
617-
}
618-
$foreignKey = $this->in(__('What is the foreignKey?', true), null, __("Enter a number", true));
619-
if (intval($foreignKey) > 0 && intval($foreignKey) <= $i ) {
620-
$foreignKey = $showKeys[intval($foreignKey) - 1];
621-
}
637+
$foreignKey = $this->inOptions($showKeys, __('What is the foreignKey?', true));
638+
$foreignKey = $showKeys[intval($foreignKey)];
622639
}
623640
if (!isset($foreignKey)) {
624641
$foreignKey = $this->in(__('What is the foreignKey? Specify your own.', true), null, $suggestedForeignKey);
625642
}
626-
if ($assocType == '4') {
643+
if ($assocType == 3) {
627644
$associationForeignKey = $this->in(__('What is the associationForeignKey?', true), null, $this->_modelKey($model->name));
628645
$joinTable = $this->in(__('What is the joinTable?', true));
629646
}
@@ -633,7 +650,7 @@ function doMoreAssociations($model, $associations) {
633650
$associations[$assocs[$assocType]][$i]['alias'] = $alias;
634651
$associations[$assocs[$assocType]][$i]['className'] = $className;
635652
$associations[$assocs[$assocType]][$i]['foreignKey'] = $foreignKey;
636-
if ($assocType == '4') {
653+
if ($assocType == 3) {
637654
$associations[$assocs[$assocType]][$i]['associationForeignKey'] = $associationForeignKey;
638655
$associations[$assocs[$assocType]][$i]['joinTable'] = $joinTable;
639656
}

0 commit comments

Comments
 (0)