From 19564c9e18ae456e39bc2c8a306f9e79dd619e11 Mon Sep 17 00:00:00 2001 From: Fbenas Date: Mon, 7 May 2018 15:24:00 +0100 Subject: Docblock and fix toString functions --- src/File/Handler.php | 13 +++++ src/Model/Collection.php | 54 ++++++++++++++++-- src/Model/Menu.php | 49 ++++++++++++++++- src/Model/Model.php | 13 +---- src/Model/Vendor.php | 139 ++++++++++++++++++++++++++++++++++++----------- src/Script/Console.php | 6 +- src/Script/Input.php | 13 +++++ src/Script/Output.php | 24 ++++++-- 8 files changed, 252 insertions(+), 59 deletions(-) (limited to 'src') diff --git a/src/File/Handler.php b/src/File/Handler.php index eecedc0..e8e30b0 100644 --- a/src/File/Handler.php +++ b/src/File/Handler.php @@ -6,10 +6,23 @@ use Exception; /** * File handler + * + * @author Phil Burton */ class Handler { + /** + * Filename + * + * @var string + */ protected $filename; + + /** + * Contents of file + * + * @var string + */ protected $file; /** diff --git a/src/Model/Collection.php b/src/Model/Collection.php index 62bbf98..c4900fb 100644 --- a/src/Model/Collection.php +++ b/src/Model/Collection.php @@ -3,19 +3,19 @@ namespace App\Model; use App\Model\Model; +use App\Script\Input; use ArrayAccess; +use Countable; +use DateTime; use Exception; use Iterator; -use App\Script\Input; -use DateTime; -use Countable; /** * A collection of models * * @author Phil Burton */ -class Collection implements ArrayAccess, Iterator, Countable +class Collection implements ArrayAccess, Countable, Iterator { /** * Raw array of models @@ -170,6 +170,17 @@ class Collection implements ArrayAccess, Iterator, Countable ++$this->position; } + /** + * Return the count of the models in this collection + * + * @author Phil Burton + * @return int + */ + public function count(): int + { + return count($this->models); + } + /** * Filter by input * @@ -193,6 +204,13 @@ class Collection implements ArrayAccess, Iterator, Countable } } + /** + * Filter this collection by a given date and time + * + * @author Phil Burton + * @param string $date + * @param string $time + */ public function filterByDateTime(string $date, string $time) { $dateTime = DateTime::createFromFormat('d/m/y G:i', $date . ' ' . $time); @@ -213,6 +231,12 @@ class Collection implements ArrayAccess, Iterator, Countable $this->models = $out; } + /** + * Filter this collection by a given location postcode + * + * @author Phil Burton + * @param string $location + */ public function filterByLocation(string $location) { $location = substr($location, 0, 2); @@ -228,6 +252,12 @@ class Collection implements ArrayAccess, Iterator, Countable $this->models = $out; } + /** + * Filter this collection by a given max covers integer + * + * @author Phil Burton + * @param int $covers + */ public function filterByCovers(int $covers) { $out = []; @@ -241,8 +271,20 @@ class Collection implements ArrayAccess, Iterator, Countable $this->models = $out; } - public function count() + /** + * Return string representation of this collection + * + * @author Phil Burton + * @return string + */ + public function toString() : string { - return count($this->models); + $out = []; + + foreach ($this->models as $model) { + $out[] = $model->toString(); + } + + return implode("\n\n", $out); } } diff --git a/src/Model/Menu.php b/src/Model/Menu.php index d0a281e..3fea03f 100644 --- a/src/Model/Menu.php +++ b/src/Model/Menu.php @@ -6,14 +6,40 @@ use App\Model\Model; /** * Menu Model + * + * @author Phil Burton */ class Menu extends Model { + /** + * Name of this menu + * + * @var string + */ protected $name; + + /** + * Allergies string + * + * @var string + */ protected $allergies; - protected $advanceTime; + /** + * Required advanced notice time in horus for this menu + * + * @var int + */ + protected $advanceTime; + /** + * Set the class attributes + * + * @author Phil Burton + * @param $name + * @param $allergies + * @param $advanceTime + */ public function __construct($name, $allergies, $advanceTime) { $this->name = $name; @@ -21,17 +47,36 @@ class Menu extends Model $this->setTime($advanceTime); } + /** + * Set the time removing the `h` if given + * e.g. 12h becomes 12 + * + * @author Phil Burton + * @param mixed $time + */ public function setTime($time) { $this->advanceTime = str_replace('h', '', $time); } + /** + * Returbn the advancedd time + * + * @author Phil Burton + * @return int + */ public function getAdvanceTime(): int { return $this->advanceTime; } - public function toString() + /** + * Return a string representation of this menu + * + * @author Phil Burton + * @return string + */ + public function toString(): string { return $this->name . ';' . $this->allergies; } diff --git a/src/Model/Model.php b/src/Model/Model.php index f59ab0e..a5775d7 100644 --- a/src/Model/Model.php +++ b/src/Model/Model.php @@ -4,18 +4,9 @@ namespace App\Model; /** * Base model class + * + * @author Phil Burton */ class Model { - protected static $inputFile = APP_ROOT . 'data/input.txt'; - - /** - * Init - * - * @author Phil Burton - */ - public function __construct() - { - // initalise Model - } } diff --git a/src/Model/Vendor.php b/src/Model/Vendor.php index 6b98704..dfed157 100644 --- a/src/Model/Vendor.php +++ b/src/Model/Vendor.php @@ -11,46 +11,46 @@ use DateInterval; /** * Vendor Model + * + * @author Phil Burton */ class Vendor extends Model { + /** + * Name of vendor + * + * @var string + */ protected $name; + + /** + * Vendor postcode + * + * @var string + */ protected $postcode; - protected $maxCovers; - protected $menus; - public function __construct($name, $postcode, $maxCovers) - { - $this->name = $name; - $this->postcode = $postcode; - $this->maxCovers = $maxCovers; - $this->menus = []; - } + /** + * Max no of people this vendor can cover + * + * @var int + */ + protected $maxCovers; - public function addMenu(Menu $menu) - { - $this->menus[] = $menu; - } + /** + * The array of menus this vendor provides + * + * @var Menu[] + */ + protected $menus; /** - * Load vendors from file, parse them into a model collection and Return + * Load all the vendor data from a given file * * @author Phil Burton - * @param FileHandler $handler + * @param string $filename * @return Collection */ - public function loadFromFile(FileHandler $handler): Collection - { - // initalise a Vendor Collection - $collection = new Collection; - - foreach ($handler->getVendorArray() as $vendorRaw) { - $collection[] = new Vendor($vendorRaw); - } - - return $collection; - } - public static function loadAll(string $filename): Collection { $fileHandler = new FileHandler($filename); @@ -104,7 +104,62 @@ class Vendor extends Model return $collection; } - public function checkDate(DateTime $date) + /** + * Set vendor data and intiate empty menus array + * + * @author Phil Burton + * @param $name + * @param $postcode + * @param $maxCovers + */ + public function __construct($name, $postcode, $maxCovers) + { + $this->name = $name; + $this->postcode = $postcode; + $this->maxCovers = $maxCovers; + $this->menus = []; + } + + /** + * Add a menu to the menus array + * + * @author Phil Burton + * @param Menu $menu + */ + public function addMenu(Menu $menu) + { + $this->menus[] = $menu; + } + + /** + * Load vendors from file, parse them into a model collection and Return + * + * @author Phil Burton + * @param FileHandler $handler + * @return Collection + */ + public function loadFromFile(FileHandler $handler): Collection + { + // initalise a Vendor Collection + $collection = new Collection; + + foreach ($handler->getVendorArray() as $vendorRaw) { + $collection[] = new Vendor($vendorRaw); + } + + return $collection; + } + + /** + * Filter the menus base on a given date + * Return true if at least one menu is still valid + * Otherwose return false + * + * @author Phil Burton + * @param DateTime $date + * @return bool + */ + public function checkDate(DateTime $date): bool { $out = []; $now = new DateTime(); @@ -126,7 +181,14 @@ class Vendor extends Model return true; } - public function checkLocation(string $location) + /** + * Return true if the first charaters of the given string matche the first characters of the postcode + * + * @author Phil Burton + * @param string $location + * @return bool + */ + public function checkLocation(string $location): bool { $postPrefix = ''; @@ -141,7 +203,14 @@ class Vendor extends Model return strtoupper($postPrefix) === strtoupper($location); } - public function checkMaxCovers(int $covers) + /** + * Return true if the given covers int is less or equal to the max covers int this vendor supports + * + * @author Phil Burton + * @param int $covers + * @return bool + */ + public function checkMaxCovers(int $covers): bool { if ($this->maxCovers >= $covers) { return true; @@ -150,7 +219,13 @@ class Vendor extends Model return false; } - public function toString() + /** + * Return a string representation of this vendor + * + * @author Phil Burton + * @return string + */ + public function toString(): string { $out = [ $this->name . ';' . $this->postcode . ';' . $this->maxCovers @@ -160,6 +235,6 @@ class Vendor extends Model $out[] = $menu->toString(); } - return $out; + return implode("\n", $out); } } diff --git a/src/Script/Console.php b/src/Script/Console.php index 3228750..ee8114e 100644 --- a/src/Script/Console.php +++ b/src/Script/Console.php @@ -46,12 +46,14 @@ class Console public function exec() { $output = new Output; + try { $input = new Input; + // Get vendor data $vendors = Vendor::loadAll($input->getOption('f')); - + // Filter data $vendors->filterByInput($input); - + // Output data $output->printCollection($vendors); } catch (Exception $e) { $output->printException($e); diff --git a/src/Script/Input.php b/src/Script/Input.php index 772bb8c..6a91a72 100644 --- a/src/Script/Input.php +++ b/src/Script/Input.php @@ -6,6 +6,8 @@ use Exception; /** * Input handler for CLI arugments and options + * + * @author Phil Burton */ class Input { @@ -21,6 +23,16 @@ class Input */ protected $shortOpts = 'f:d::t::l::c::'; + /** + * 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 $longOpts = [ 'filename:', 'day::', @@ -48,6 +60,7 @@ class Input /** * Load options, make sure required options are set + * Throw an exception if input data is badly formatted * * @author Phil Burton */ diff --git a/src/Script/Output.php b/src/Script/Output.php index e61cff8..4598dc1 100644 --- a/src/Script/Output.php +++ b/src/Script/Output.php @@ -5,8 +5,19 @@ namespace App\Script; use App\Model\Collection; use Exception; +/** + * Handle output to CLI + * + * @author Phil Burton + */ class Output { + /** + * Echo out the output data + * + * @author Phil Burton + * @param Collection $collection + */ public function printCollection(Collection $collection) { if (count($collection) === 0) { @@ -14,14 +25,15 @@ class Output return; } - $out = []; - foreach ($collection as $vendor) { - $out[] = implode("\n", $vendor->toString()); - } - - echo implode("\n\n", $out) . "\n"; + echo $collection->toString() . "\n"; } + /** + * Output the exception message + * + * @author Phil Burton + * @param Exception $e + */ public function printException(Exception $e) { echo "An unexpected exception occured:\n" . $e->getMessage() . "\n"; -- cgit v1.2.3