1: <?php
  2:   3:   4:   5:   6:   7:   8:   9:  10:  11:  12:  13:  14:  15:  16: 
 17: 
 18:  19:  20:  21:  22:  23:  24: 
 25: class ConsoleInputOption {
 26: 
 27:  28:  29:  30:  31: 
 32:     protected $_name;
 33: 
 34:  35:  36:  37:  38: 
 39:     protected $_short;
 40: 
 41:  42:  43:  44:  45: 
 46:     protected $_help;
 47: 
 48:  49:  50:  51:  52: 
 53:     protected $_boolean;
 54: 
 55:  56:  57:  58:  59: 
 60:     protected $_default;
 61: 
 62:  63:  64:  65:  66: 
 67:     protected $_choices;
 68: 
 69:  70:  71:  72:  73:  74:  75:  76:  77:  78:  79: 
 80:     public function __construct($name, $short = null, $help = '', $boolean = false, $default = '', $choices = array()) {
 81:         if (is_array($name) && isset($name['name'])) {
 82:             foreach ($name as $key => $value) {
 83:                 $this->{'_' . $key} = $value;
 84:             }
 85:         } else {
 86:             $this->_name = $name;
 87:             $this->_short = $short;
 88:             $this->_help = $help;
 89:             $this->_boolean = $boolean;
 90:             $this->_default = $default;
 91:             $this->_choices = $choices;
 92:         }
 93:         if (strlen($this->_short) > 1) {
 94:             throw new ConsoleException(
 95:                 __d('cake_console', 'Short option "%s" is invalid, short options must be one letter.', $this->_short)
 96:             );
 97:         }
 98:     }
 99: 
100: 101: 102: 103: 104: 
105:     public function name() {
106:         return $this->_name;
107:     }
108: 
109: 110: 111: 112: 113: 
114:     public function short() {
115:         return $this->_short;
116:     }
117: 
118: 119: 120: 121: 122: 123: 
124:     public function help($width = 0) {
125:         $default = $short = '';
126:         if (!empty($this->_default) && $this->_default !== true) {
127:             $default = __d('cake_console', ' <comment>(default: %s)</comment>', $this->_default);
128:         }
129:         if (!empty($this->_choices)) {
130:             $default .= __d('cake_console', ' <comment>(choices: %s)</comment>', implode('|', $this->_choices));
131:         }
132:         if (!empty($this->_short)) {
133:             $short = ', -' . $this->_short;
134:         }
135:         $name = sprintf('--%s%s', $this->_name, $short);
136:         if (strlen($name) < $width) {
137:             $name = str_pad($name, $width, ' ');
138:         }
139:         return sprintf('%s%s%s', $name, $this->_help, $default);
140:     }
141: 
142: 143: 144: 145: 146: 
147:     public function usage() {
148:         $name = empty($this->_short) ? '--' . $this->_name : '-' . $this->_short;
149:         $default = '';
150:         if (!empty($this->_default) && $this->_default !== true) {
151:             $default = ' ' . $this->_default;
152:         }
153:         if (!empty($this->_choices)) {
154:             $default = ' ' . implode('|', $this->_choices);
155:         }
156:         return sprintf('[%s%s]', $name, $default);
157:     }
158: 
159: 160: 161: 162: 163: 
164:     public function defaultValue() {
165:         return $this->_default;
166:     }
167: 
168: 169: 170: 171: 172: 
173:     public function isBoolean() {
174:         return (bool)$this->_boolean;
175:     }
176: 
177: 178: 179: 180: 181: 182: 183: 
184:     public function validChoice($value) {
185:         if (empty($this->_choices)) {
186:             return true;
187:         }
188:         if (!in_array($value, $this->_choices)) {
189:             throw new ConsoleException(
190:                 __d('cake_console', '"%s" is not a valid value for --%s. Please use one of "%s"',
191:                 $value, $this->_name, implode(', ', $this->_choices)
192:             ));
193:         }
194:         return true;
195:     }
196: 
197: 198: 199: 200: 201: 202: 
203:     public function xml(SimpleXmlElement $parent) {
204:         $option = $parent->addChild('option');
205:         $option->addAttribute('name', '--' . $this->_name);
206:         $short = '';
207:         if (strlen($this->_short)) {
208:             $short = $this->_short;
209:         }
210:         $option->addAttribute('short', '-' . $short);
211:         $option->addAttribute('boolean', $this->_boolean);
212:         $option->addChild('default', $this->_default);
213:         $choices = $option->addChild('choices');
214:         foreach ($this->_choices as $valid) {
215:             $choices->addChild('choice', $valid);
216:         }
217:         return $parent;
218:     }
219: 
220: }
221: