/** * Creates and returns a new Options instance * * @param array $getopt Options data * @param array $validators Validators to validate the options * @param array $defaults Default values for the options * @return Options */ public function newInstance(array $getopt = [], array $validators = [], array $defaults = []) { $this->error = null; // Extend the base config if (isset($this->config['getopt'])) { $getopt = array_merge($this->config['getopt'], $getopt); } if (isset($this->config['validators'])) { $validators = array_merge($this->config['validators'], $validators); } if (isset($this->config['defaults'])) { $defaults = array_merge($this->config['defaults'], $defaults); } // Read command line $cli = $this->context->getopt($getopt); $data = new OptionTransformer($cli->get()); // Create the options container instance $options = new Options($validators, $defaults); $options->loadOptionData($data->getArrayCopy()); // initial load so we can access the config option // Read config file $configLoader = new DotenvLoader((string) new TildeExpander($options->config)); try { $configData = $configLoader->parse()->toArray(); $options->loadOptionData($configData, false); // don't overwrite CLI data } catch (\InvalidArgumentException $e) { $this->error = $e; } return $options; }
/** * Validates all required options * * @return boolean true if all option data is valid, false otherwise * @throws ValidatorException if encountering an invalid option and throwExceptions is true */ public function validateAllRequired() { foreach ($this->validators as $setting => $validators) { foreach ($validators as $validator) { $value = $this->__get($setting); $settings = $this->asArray(); if ($validator instanceof RequiredValidator && !$validator($value, $setting, $settings)) { if ($this->throwExceptions) { $transformer = new OptionTransformer(); $option = $transformer->untransformKey($setting); throw new ValidatorException("--{$option} is required, see help for usage instructions"); } else { return false; } } } } return true; }