Passed
Push — master ( 9ba3b5...c4bcc6 )
by Michael
02:46
created

CustomOrderTrait::setColumn()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 9
rs 9.6666
c 0
b 0
f 0
cc 3
eloc 4
nc 2
nop 1
1
<?php
2
3
namespace Blasttech\EloquentRelatedPlus;
4
5
use DB;
6
use Illuminate\Database\Eloquent\Builder;
7
use Illuminate\Database\Eloquent\Model;
8
9
/**
10
 * Trait RelatedPlusTrait
11
 *
12
 * @property array order_fields
13
 * @property array order_defaults
14
 * @property array order_relations
15
 * @property array order_with
16
 * @property array search_fields
17
 * @property string connection
18
 */
19
trait CustomOrderTrait
20
{
21
    use HelperMethodTrait, RelatedPlusTrait;
22
23
    /**
24
     * Check if column being sorted by is from a related model
25
     *
26
     * @param Builder $query
27
     * @param string $column
28
     * @param string $direction
29
     * @return Builder
30
     */
31
    public function scopeOrderByCheckModel(Builder $query, $column, $direction)
32
    {
33
        /** @var Model $query */
34
        $query->orderBy(DB::raw($column), $direction);
35
36
        $periodPos = strpos($column, '.');
37
        if (isset($this->order_relations) && ($periodPos !== false || isset($this->order_relations[$column]))) {
38
            $table = ($periodPos !== false ? substr($column, 0, $periodPos) : $column);
39
            $query = $this->joinRelatedTable($query, $table);
40
        }
41
42
        return $query;
43
    }
44
45
    /**
46
     * Set the model order
47
     *
48
     * @param Builder $query
49
     * @param string $column
50
     * @param string $direction
51
     * @return Builder
52
     */
53
    public function scopeSetCustomOrder(Builder $query, $column, $direction)
54
    {
55
        if (isset($this->order_defaults)) {
56
            $column = $this->setColumn($column);
57
            $direction = $this->setDirection($direction);
58
        }
59
60
        return $this->setOrder($query, $column, $direction);
61
    }
62
63
    /**
64
     * Override column if provided column not valid
65
     *
66
     * @param string $column
67
     * @return string
68
     */
69
    private function setColumn($column)
70
    {
71
        // If $column not in order_fields list, use default
72
        if ($column == '' || !isset($this->order_fields[$column])) {
73
            $column = $this->order_defaults['field'];
74
        }
75
76
        return $column;
77
    }
78
79
    /**
80
     * Override direction if provided direction not valid
81
     *
82
     * @param string $direction
83
     * @return string
84
     */
85
    private function setDirection($direction)
86
    {
87
        // If $direction not asc or desc, use default
88
        if ($direction == '' || !in_array(strtoupper($direction), ['ASC', 'DESC'])) {
89
            $direction = $this->order_defaults['dir'];
90
        }
91
92
        return $direction;
93
    }
94
95
    /**
96
     * Set order based on order_fields
97
     *
98
     * @param Builder $query
99
     * @param string $column
100
     * @param string $direction
101
     * @return Builder
102
     */
103
    private function setOrder($query, $column, $direction)
104
    {
105
        if (!is_array($this->order_fields[$column])) {
106
            $query->orderByCheckModel($this->order_fields[$column], $direction);
107
        } else {
108
            foreach ($this->order_fields[$column] as $dbField) {
109
                $query->orderByCheckModel($dbField, $direction);
110
            }
111
        }
112
113
        return $query;
114
    }
115
116
    /**
117
     * Join a related table if not already joined
118
     *
119
     * @param Builder $query
120
     * @param string $table
121
     * @return Builder
122
     */
123
    private function joinRelatedTable($query, $table)
124
    {
125
        if (isset($this->order_relations[$table]) &&
126
            !$this->hasJoin($query, $table, $this->order_relations[$table])) {
127
            $columnRelations = $this->order_relations[$table];
128
129
            $query->modelJoin(
130
                $columnRelations,
131
                '=',
132
                'left',
133
                false,
134
                false
135
            );
136
        }
137
138
        return $query;
139
    }
140
}
141