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/Collection.php | 85 ++++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 71 insertions(+), 14 deletions(-) (limited to 'src/Model/Collection.php') diff --git a/src/Model/Collection.php b/src/Model/Collection.php index 8715f07..803e777 100644 --- a/src/Model/Collection.php +++ b/src/Model/Collection.php @@ -4,9 +4,12 @@ namespace App\Model; use App\Model\Model; use ArrayAccess; -use Countable; use Exception; use Iterator; +use App\Script\Input; +use DateTime; +use DateTimeZone; +use Countable; /** * A collection of models @@ -129,7 +132,7 @@ class Collection implements ArrayAccess, Iterator, Countable * Return true if the current index exists in user array * Otherwise return false * - * @author Phil Burton + * @author Phil Burton "dump" * @return bool */ public function valid() @@ -169,24 +172,78 @@ class Collection implements ArrayAccess, Iterator, Countable } /** - * Create and return new collection of the merged arrays from thsi and a given collection + * Filter by input * * @author Phil Burton - * @param Collection $collection - * @return Collection + * @param Input $input */ - public function merge(Collection $collection): Collection + public function filterByInput(Input $input) { - return new Collection(array_merge($collection->toArray(), $this->toArray())); + // filter by time + if ($time = $input->getOption('t')) { + $day = $input->getOption('d'); + $this->filterByDateTime($day, $time); + } + + if ($location = $input->getOption('l')) { + $this->filterByLocation($location); + } + + if ($covers = (int) $input->getOption('c')) { + $this->filterByCovers($covers); + } + + + // day - delivery day (dd/mm/yy) + // time - delivery time in 24h format (hh:mm) + // location - delivery location (postcode without spaces, e.g. NW43QB) + // covers - number of people to feed } - /** - * Return count of users - * - * @author Phil Burton - * @return int - */ - public function count(): int + public function filterByDateTime(string $date, string $time) + { + $dateTime = DateTime::createFromFormat('d/m/y G:i', $date . ' ' . $time); + $dateTime->setTImezone(new DateTImeZone('Europe/London')); + + $out = []; + foreach ($this->models as $model) { + if ($model->checkDate($dateTime)) { + $out[] = $model; + } + } + + $this->models = $out; + } + + public function filterByLocation(string $location) + { + $location = substr($location, 0, 2); + + $out = []; + + foreach ($this->models as $model) { + if ($model->checkLocation($location)) { + $out[] = $model; + } + } + + $this->models = $out; + } + + public function filterByCovers(int $covers) + { + $out = []; + + foreach ($this->models as $model) { + if ($model->checkMaxCovers($covers)) { + $out[] = $model; + } + } + + $this->models = $out; + } + + public function count() { return count($this->models); } -- cgit v1.2.3