From 33f00442f1667d57f94433834e0da4985670025b Mon Sep 17 00:00:00 2001 From: Fbenas Date: Mon, 7 May 2018 14:42:15 +0100 Subject: Get filtering working with output --- src/Model/Vendor.php | 126 +++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 121 insertions(+), 5 deletions(-) (limited to 'src/Model/Vendor.php') 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 - * @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; } } -- cgit v1.2.3