*/ class Vendor extends Model { /** * Name of vendor * * @var string */ protected $name; /** * Vendor postcode * * @var string */ protected $postcode; /** * Max no of people this vendor can cover * * @var int */ protected $maxCovers; /** * The array of menus this vendor provides * * @var Menu[] */ protected $menus; /** * Load all the vendor data from a given file * * @author Phil Burton * @param string $filename * @return Collection */ public static function loadAll(string $filename): Collection { $fileHandler = new FileHandler($filename); $fileContents = $fileHandler->getFileContents(); return static::parseVendors($fileContents); } /** * Parse file and return array * * @author Phil Burton * @return array */ 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; } /** * 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(); 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; } /** * 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 = ''; foreach (str_split($this->postcode) as $char) { if (is_numeric($char)) { break; } $postPrefix .= $char; } return strtoupper($postPrefix) === strtoupper($location); } /** * 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; } return false; } /** * Return a string representation of this vendor * * @author Phil Burton * @return string */ public function toString(): string { $out = [ $this->name . ';' . $this->postcode . ';' . $this->maxCovers ]; foreach ($this->menus as $menu) { $out[] = $menu->toString(); } return implode("\n", $out); } }