SortData.php 1.49 KB
<?php


namespace App\Classes;


class SortData
{
    protected $builder;
    protected $request;

    public function __construct($builder, $request) {
        $this->builder = $builder;
        $this->request = $request;
    }

    public function apply() {
        foreach ($this->sorter() as $filter => $value) {
            if (method_exists($this, $filter)) {
                $this->$filter($value);
            }
        }
        return $this->builder;
    }

    public function sort_price($value) {
        switch ($value) {
            case 1: $this->builder = $this->builder->orderBy('price');break;
            case 2: $this->builder = $this->builder->orderByDesc('price');break;
            default: $this->builder = $this->builder->orderBy('price');break;
        }
    }

    public function sort_new($value) {
        switch ($value) {
            case 1: $this->builder = $this->builder->orderByDesc('created_at');break;
            case 2: $this->builder = $this->builder->orderBy('created_at');break;
            default: $this->builder = $this->builder->orderByDesc('created_at');break;
        }
    }

    public function sort_area($value) {
        switch ($value) {
            case 1: $this->builder = $this->builder->orderByDesc('area');break;
            case 2: $this->builder = $this->builder->orderBy('area');break;
            default: $this->builder = $this->builder->orderByDesc('area');break;
        }
    }

    public function sorter() {
        return $this->request->all();
    }

}