diff options
Diffstat (limited to 'src/Script')
-rw-r--r-- | src/Script/Console.php | 52 | ||||
-rw-r--r-- | src/Script/Input.php | 87 | ||||
-rw-r--r-- | src/Script/Output.php | 0 |
3 files changed, 139 insertions, 0 deletions
diff --git a/src/Script/Console.php b/src/Script/Console.php new file mode 100644 index 0000000..bedbc1d --- /dev/null +++ b/src/Script/Console.php @@ -0,0 +1,52 @@ +<?php + +namespace App\Script; + +// use App\Model\Vendor; +use App\Script\Input; +// use App\Script\Output; + +/** + * Main application class + * + * @author Phil Burton <phil@pgburton.com> + */ +class Console +{ + /** + * Create symfony application and add commands + * + * @author Phil Burton <phil@pgburton.com> + */ + public function __construct() + { + $this->init(); + } + + /** + * Initalise + * + * @author Phil Burton <phil@pgburton.com> + */ + protected function init() + { + // Define the root of the application + define('APP_ROOT', realpath(dirname(__FILE__) . '/../../') . '/'); + } + + /** + * Run main application code + * + * @author Phil Burton <phil@pgburton.com> + */ + public function exec() + { + $vendors = Vendor::loadAll(); + $input = new Input; + + $vendors->filterByInput($input); + // $output = new Output; + // + // $output->printCollection($vendors); + } +} 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]; + } +} diff --git a/src/Script/Output.php b/src/Script/Output.php new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/src/Script/Output.php |