diff options
| author | Fbenas <philbeansburton@gmail.com> | 2018-05-07 14:42:15 +0100 | 
|---|---|---|
| committer | Fbenas <philbeansburton@gmail.com> | 2018-05-07 14:42:15 +0100 | 
| commit | 33f00442f1667d57f94433834e0da4985670025b (patch) | |
| tree | aa65df03e719f3d8024b0ea42a62aa7fd61f196e /src/Model/Collection.php | |
| parent | c223ab602cbe7f6db7321ba547164971d63d7bcd (diff) | |
Get filtering working with output
Diffstat (limited to 'src/Model/Collection.php')
| -rw-r--r-- | src/Model/Collection.php | 85 | 
1 files changed, 71 insertions, 14 deletions
| 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 <phil@pgburton.com> +     * @author Phil Burton <phil@pgburton.com>"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 <phil@pgburton.com> -     * @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 <phil@pgburton.com> -     * @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);      } | 
