summaryrefslogtreecommitdiff
path: root/src/Model/Vendor.php
diff options
context:
space:
mode:
authorFbenas <philbeansburton@gmail.com>2018-05-07 14:42:15 +0100
committerFbenas <philbeansburton@gmail.com>2018-05-07 14:42:15 +0100
commit33f00442f1667d57f94433834e0da4985670025b (patch)
treeaa65df03e719f3d8024b0ea42a62aa7fd61f196e /src/Model/Vendor.php
parentc223ab602cbe7f6db7321ba547164971d63d7bcd (diff)
Get filtering working with output
Diffstat (limited to 'src/Model/Vendor.php')
-rw-r--r--src/Model/Vendor.php126
1 files changed, 121 insertions, 5 deletions
diff --git a/src/Model/Vendor.php b/src/Model/Vendor.php
index 99a95dd..6b98704 100644
--- a/src/Model/Vendor.php
+++ b/src/Model/Vendor.php
@@ -4,8 +4,10 @@ namespace App\Model;
use App\File\Handler as FileHandler;
use App\Model\Collection;
+use App\Model\Menu;
use App\Model\Model;
-use App\Script\Input;
+use DateTime;
+use DateInterval;
/**
* Vendor Model
@@ -17,6 +19,19 @@ class Vendor extends Model
protected $maxCovers;
protected $menus;
+ public function __construct($name, $postcode, $maxCovers)
+ {
+ $this->name = $name;
+ $this->postcode = $postcode;
+ $this->maxCovers = $maxCovers;
+ $this->menus = [];
+ }
+
+ public function addMenu(Menu $menu)
+ {
+ $this->menus[] = $menu;
+ }
+
/**
* Load vendors from file, parse them into a model collection and Return
*
@@ -36,14 +51,115 @@ class Vendor extends Model
return $collection;
}
+ public static function loadAll(string $filename): Collection
+ {
+ $fileHandler = new FileHandler($filename);
+
+ $fileContents = $fileHandler->getFileContents();
+
+ return static::parseVendors($fileContents);
+ }
+
+
/**
- * Filter by input
+ * Parse file and return array
*
* @author Phil Burton <phil@pgburton.com>
- * @param Input $input
+ * @return array
*/
- public function filterByInput(Input $input)
+ public static function parseVendors($fileContents): Collection
+ {
+ $collection = new Collection;
+
+ // split the various vendor data out
+ $array = explode("\n\n", $fileContents);
+
+ // For each vendor
+ foreach ($array as $value) {
+ $rawVendor = explode("\n", $value);
+ // Split off the vendor data
+ $vendorHead = explode(";", $rawVendor[0]);
+
+ $vendor = new Vendor($vendorHead[0], $vendorHead[1], $vendorHead[2]);
+
+ // Remove the head
+ unset($rawVendor[0]);
+ // Reset the keys
+ $rawVendor = array_values($rawVendor);
+
+ // Now parse menus
+ foreach ($rawVendor as $item) {
+ // Ignore empty line (usually last line of file)
+ if ($item === "") {
+ continue;
+ }
+ $rawMenu = explode(";", $item);
+ $menu = new Menu($rawMenu[0], $rawMenu[1], $rawMenu[2]);
+ $vendor->addMenu($menu);
+ }
+
+ $collection[] = $vendor;
+ }
+
+ return $collection;
+ }
+
+ public function checkDate(DateTime $date)
{
- // Amend the colletion so we've filtered by the input
+ $out = [];
+ $now = new DateTime();
+
+ foreach ($this->menus as $menu) {
+ $earliestDelivery = $now->add(new DateInterval('PT' . $menu->getAdvanceTime() . 'H'));
+
+ if ($earliestDelivery <= $date) {
+ $out[] = $menu;
+ }
+ }
+
+ $this->menus = $out;
+
+ if (!$this->menus) {
+ return false;
+ }
+
+ return true;
+ }
+
+ public function checkLocation(string $location)
+ {
+ $postPrefix = '';
+
+ foreach (str_split($this->postcode) as $char) {
+ if (is_numeric($char)) {
+ break;
+ }
+
+ $postPrefix .= $char;
+ }
+
+ return strtoupper($postPrefix) === strtoupper($location);
+ }
+
+ public function checkMaxCovers(int $covers)
+ {
+ if ($this->maxCovers >= $covers) {
+ return true;
+ }
+
+ return false;
+ }
+
+ public function toString()
+ {
+ $out = [
+ $this->name . ';' . $this->postcode . ';' . $this->maxCovers
+ ];
+
+ foreach ($this->menus as $menu) {
+ $out[] = $menu->toString();
+ }
+
+ return $out;
}
}