From c223ab602cbe7f6db7321ba547164971d63d7bcd Mon Sep 17 00:00:00 2001 From: Fbenas Date: Sun, 29 Apr 2018 22:24:25 +0100 Subject: WIP --- src/Script/Input.php | 87 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 87 insertions(+) create mode 100644 src/Script/Input.php (limited to 'src/Script/Input.php') diff --git a/src/Script/Input.php b/src/Script/Input.php new file mode 100644 index 0000000..6b15c51 --- /dev/null +++ b/src/Script/Input.php @@ -0,0 +1,87 @@ + + */ + public function __construct() + { + $this->loadOptions(); + } + + /** + * Load options, make sure required options are set + * + * @author Phil Burton + */ + protected function loadOptions() + { + $this->options = getopt($this->getOptionString()); + + if (!array_key_exists('f', $this->options)) { + throw new Exception('Filename Option `-f` is required'); + } + + // If only a day or a time is given, we throw exception as we need neither or both + if ((array_key_exists('d', $this->options) <=> array_key_exists('t', $this->options)) !== 0) { + throw new Exception('Both day and time options (`-d` and `-t`) are required for time based filtering'); + } + } + + /** + * Return the option string for the options we want to load + * + * @author Phil Burton + * @return string + */ + public function getOptionString(): string + { + return $this->availableOptions; + } + + /** + * Return an option value + * In it doesn't exist return false + * + * @author Phil Burton + * @param string $key + * @return mixed + */ + public function getOption(string $key) + { + if (!array_key_exists($key, $this->options)) { + return false; + } + + return $this->options[$key]; + } +} -- cgit v1.2.3