1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the Tinyissue package. |
5
|
|
|
* |
6
|
|
|
* (c) Mohamed Alsharaf <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Tinyissue\Model\Traits\Project; |
13
|
|
|
|
14
|
|
|
use Illuminate\Database\Eloquent; |
15
|
|
|
use Illuminate\Database\Query; |
16
|
|
|
use Tinyissue\Model\Project; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* SortTrait is trait class containing the methods for sorting database queries of the Project model. |
20
|
|
|
* |
21
|
|
|
* @author Mohamed Alsharaf <[email protected]> |
22
|
|
|
* |
23
|
|
|
* @property int $id |
24
|
|
|
* |
25
|
|
|
* @method Eloquent\Model where($column, $operator = null, $value = null, $boolean = 'and') |
26
|
|
|
*/ |
27
|
|
|
trait SortTrait |
28
|
|
|
{ |
29
|
|
|
/** |
30
|
|
|
* Sort by updated_at column. |
31
|
|
|
* |
32
|
|
|
* @param Query\Builder $query |
33
|
|
|
* @param string $order |
34
|
|
|
* |
35
|
|
|
* @return void |
36
|
|
|
*/ |
37
|
|
|
public function sortByUpdated(Query\Builder $query, $order = 'asc') |
38
|
|
|
{ |
39
|
|
|
$query->orderBy('updated_at', $order); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Sort by issues tag group |
44
|
|
|
* Note: this sort will return the collection. |
45
|
|
|
* |
46
|
|
|
* @param Eloquent\Relations\Relation $query |
47
|
|
|
* @param string $tagGroup |
48
|
|
|
* @param string $order |
49
|
|
|
* |
50
|
|
|
* @return Eloquent\Collection |
51
|
|
|
*/ |
52
|
|
|
public function sortByTag(Eloquent\Relations\Relation $query, $tagGroup, $order = 'asc') |
53
|
|
|
{ |
54
|
|
|
// If tag group is string prefixed with tag: |
55
|
|
|
if (!is_numeric($tagGroup)) { |
56
|
|
|
$tagGroup = substr($tagGroup, strlen('tag:')); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
$results = $query->get() |
60
|
|
|
->sort(function (Project\Issue $issue1, Project\Issue $issue2) use ($tagGroup, $order) { |
61
|
|
|
$tag1 = $issue1->tags->where('parent.id', $tagGroup, false)->first(); |
|
|
|
|
62
|
|
|
$tag2 = $issue2->tags->where('parent.id', $tagGroup, false)->first(); |
|
|
|
|
63
|
|
|
$tag1 = $tag1 ? $tag1->name : ''; |
64
|
|
|
$tag2 = $tag2 ? $tag2->name : ''; |
65
|
|
|
|
66
|
|
|
if ($order === 'asc') { |
67
|
|
|
return strcmp($tag1, $tag2); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
return strcmp($tag2, $tag1); |
71
|
|
|
}); |
72
|
|
|
|
73
|
|
|
return $results; |
74
|
|
|
} |
75
|
|
|
} |
76
|
|
|
|
Since your code implements the magic getter
_get
, this function will be called for any read access on an undefined variable. You can add the@property
annotation to your class or interface to document the existence of this variable.If the property has read access only, you can use the @property-read annotation instead.
Of course, you may also just have mistyped another name, in which case you should fix the error.
See also the PhpDoc documentation for @property.