summaryrefslogtreecommitdiff
path: root/src/Model
diff options
context:
space:
mode:
authorFbenas <philbeansburton@gmail.com>2018-05-07 15:24:00 +0100
committerFbenas <philbeansburton@gmail.com>2018-05-07 15:24:00 +0100
commit19564c9e18ae456e39bc2c8a306f9e79dd619e11 (patch)
treebcaa30f4cdc273bd55af7948ed252bda3b4b8412 /src/Model
parent8350b6bd36d3a4bb3a99dfbf67e746f4f815927c (diff)
Docblock and fix toString functions
Diffstat (limited to 'src/Model')
-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
4 files changed, 204 insertions, 51 deletions
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);
}
}