summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/File/Handler.php13
-rw-r--r--src/Model/Collection.php54
-rw-r--r--src/Model/Menu.php49
-rw-r--r--src/Model/Model.php13
-rw-r--r--src/Model/Vendor.php139
-rw-r--r--src/Script/Console.php6
-rw-r--r--src/Script/Input.php13
-rw-r--r--src/Script/Output.php24
8 files changed, 252 insertions, 59 deletions
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 <phil@pgburton.com>
*/
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 <phil@pgburton.com>
*/
-class Collection implements ArrayAccess, Iterator, Countable
+class Collection implements ArrayAccess, Countable, Iterator
{
/**
* Raw array of models
@@ -171,6 +171,17 @@ class Collection implements ArrayAccess, Iterator, Countable
}
/**
+ * Return the count of the models in this collection
+ *
+ * @author Phil Burton <phil@pgburton.com>
+ * @return int
+ */
+ public function count(): int
+ {
+ return count($this->models);
+ }
+
+ /**
* Filter by input
*
* @author Phil Burton <phil@pgburton.com>
@@ -193,6 +204,13 @@ class Collection implements ArrayAccess, Iterator, Countable
}
}
+ /**
+ * Filter this collection by a given date and time
+ *
+ * @author Phil Burton <phil@pgburton.com>
+ * @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 <phil@pgburton.com>
+ * @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 <phil@pgburton.com>
+ * @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 <phil@pgburton.com>
+ * @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 <phil@pgburton.com>
*/
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 <phil@pgburton.com>
+ * @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 <phil@pgburton.com>
+ * @param mixed $time
+ */
public function setTime($time)
{
$this->advanceTime = str_replace('h', '', $time);
}
+ /**
+ * Returbn the advancedd time
+ *
+ * @author Phil Burton <phil@pgburton.com>
+ * @return int
+ */
public function getAdvanceTime(): int
{
return $this->advanceTime;
}
- public function toString()
+ /**
+ * Return a string representation of this menu
+ *
+ * @author Phil Burton <phil@pgburton.com>
+ * @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 <phil@pgburton.com>
*/
class Model
{
- protected static $inputFile = APP_ROOT . 'data/input.txt';
-
- /**
- * Init
- *
- * @author Phil Burton <phil@pgburton.com>
- */
- 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 <phil@pgburton.com>
*/
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 <phil@pgburton.com>
- * @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 <phil@pgburton.com>
+ * @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 <phil@pgburton.com>
+ * @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 <phil@pgburton.com>
+ * @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 <phil@pgburton.com>
+ * @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 <phil@pgburton.com>
+ * @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 <phil@pgburton.com>
+ * @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 <phil@pgburton.com>
+ * @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 <phil@pgburton.com>
*/
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 <phil@pgburton.com>
*/
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 <phil@pgburton.com>
+ */
class Output
{
+ /**
+ * Echo out the output data
+ *
+ * @author Phil Burton <phil@pgburton.com>
+ * @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 <phil@pgburton.com>
+ * @param Exception $e
+ */
public function printException(Exception $e)
{
echo "An unexpected exception occured:\n" . $e->getMessage() . "\n";