summaryrefslogtreecommitdiff
path: root/src/Script/Input.php
diff options
context:
space:
mode:
Diffstat (limited to 'src/Script/Input.php')
-rw-r--r--src/Script/Input.php87
1 files changed, 87 insertions, 0 deletions
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 @@
+<?php
+
+namespace App\Script;
+
+use Exception;
+
+/**
+ * Input handler for CLI arugments and options
+ */
+class Input
+{
+ /**
+ * Available CLI options
+ * f filename - input file with the vendors data
+ * d day - delivery day (dd/mm/yy)
+ * t time - delivery time in 24h format (hh:mm)
+ * l location - delivery location (postcode without spaces, e.g. NW43QB)
+ * c covers - number of people to feed
+ *
+ * @var string
+ */
+ protected $availableOptions = 'fdtlc::';
+
+ /**
+ * The loaded CLI options
+ *
+ * @var string
+ */
+ protected $options;
+
+ /**
+ * Parse arguments and options
+ *
+ * @author Phil Burton <phil@pgburton.com>
+ */
+ public function __construct()
+ {
+ $this->loadOptions();
+ }
+
+ /**
+ * Load options, make sure required options are set
+ *
+ * @author Phil Burton <phil@pgburton.com>
+ */
+ 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 <phil@pgburton.com>
+ * @return string
+ */
+ public function getOptionString(): string
+ {
+ return $this->availableOptions;
+ }
+
+ /**
+ * Return an option value
+ * In it doesn't exist return false
+ *
+ * @author Phil Burton <phil@pgburton.com>
+ * @param string $key
+ * @return mixed
+ */
+ public function getOption(string $key)
+ {
+ if (!array_key_exists($key, $this->options)) {
+ return false;
+ }
+
+ return $this->options[$key];
+ }
+}