/** * Set a new option. * * @param string $name No use here, we use option name. * @param Option $option The option object to set in this set. * * @return void * * @since 1.0 */ public function offsetSet($name, $option) { $name = $option->getName(); $aliases = $option->getAlias(); $this->setAlias($aliases, $name); parent::offsetSet($name, $option); }
/** * @param string|Option $name * @param float|null $price * @param StockItem|null $item */ public function addOption($name, $price = null, StockItem $item = null) { if (!$name instanceof Option) { $name = (string) $name; if (empty($name)) { throw new \InvalidArgumentException('option name cannot be empty'); } $name = new Option($name, $price, $item); unset($price, $item); } if (null !== $this->_findOption($name->getName()) || in_array($name->getStockItem(), $this->_getStockItems(), true)) { throw new \InvalidArgumentException('cannot add option with the same name twice'); } $this->options[] = $name; }
protected function registerOption(Option $opt) { $registered = FALSE; foreach (array('short', 'long') as $prefix) { if (($name = $opt->{'get' . lcfirst($prefix) . 'Name'}()) === NULL) { continue; } $registered = TRUE; if (isset($this->{$prefix . 'Opts'}[$name])) { throw new OptionAlreadyRegisteredException($name, $prefix === 'long'); } $this->{$prefix . 'Opts'}[$name] = $opt; } if ($registered === FALSE) { throw new InvalidArgumentException("Either short or long option name must be set for option '{$opt->getName()}'."); } }
/** * Set a new option. * * @param string $name No use here, we use option name. * @param Option $option The option object to set in this set. * * @return void * * @since 2.0 */ public function offsetSet($name, $option) { $name = $option->getName(); $this->options[$name] = $option; }
/** * @param $option * @param string $value */ public function triggerOption(Option $option, $value = '') { if ($option->getName() == 'help') { $this->outputHelp(); } elseif ($option->getName() == 'version') { $this->outputVersion(); } else { $this->createProperty($option->getName(), $value); } }
/** * 添加一个选项 * @param Option $option 选项 * @throws \LogicException * @api */ public function addOption(Option $option) { if (isset($this->options[$option->getName()]) && !$option->equals($this->options[$option->getName()])) { throw new \LogicException(sprintf('An option named "%s" already exists.', $option->getName())); } if ($option->getShortcut()) { foreach (explode('|', $option->getShortcut()) as $shortcut) { if (isset($this->shortcuts[$shortcut]) && !$option->equals($this->options[$this->shortcuts[$shortcut]])) { throw new \LogicException(sprintf('An option with shortcut "%s" already exists.', $shortcut)); } } } $this->options[$option->getName()] = $option; if ($option->getShortcut()) { foreach (explode('|', $option->getShortcut()) as $shortcut) { $this->shortcuts[$shortcut] = $option->getName(); } } }
/** * @covers Phossa\Console\Option::getName */ public function testGetName() { $this->assertEquals('user', $this->object->getName()); }
/** * Add an Option object. * * @param Option $option An Option object */ public function addOption(Option $option) { if (isset($this->options[$option->getName()])) { throw new \LogicException(sprintf('An option named "%s" already exist.', $option->getName())); } else { if (isset($this->shortcuts[$option->getShortcut()])) { throw new \LogicException(sprintf('An option with shortcut "%s" already exist.', $option->getShortcut())); } } $this->options[$option->getName()] = $option; if ($option->getShortcut()) { $this->shortcuts[$option->getShortcut()] = $option->getName(); } }
/** * 检查所给选项是否是当前这个 * @param Option $option * @return bool */ public function equals(Option $option) { return $option->getName() === $this->getName() && $option->getShortcut() === $this->getShortcut() && $option->getDefault() === $this->getDefault() && $option->isArray() === $this->isArray() && $option->isValueRequired() === $this->isValueRequired() && $option->isValueOptional() === $this->isValueOptional(); }
function test_getIdsFromNames() { //arrange $name = "peanut-free"; $test_option = new Option($name); $test_option->save(); $name2 = "soy-free"; $test_option2 = new Option($name2); $test_option2->save(); //act $suitable_options_names = array($test_option->getName(), $test_option2->getName()); $result = Option::getIdsFromNames($suitable_options_names); //assert $this->assertEquals([$test_option->getId(), $test_option2->getId()], $result); }