|
1
|
|
|
<?php |
|
2
|
|
|
/* |
|
3
|
|
|
* This file is part of the Laravel Platfourm package. |
|
4
|
|
|
* |
|
5
|
|
|
* (c) Avtandil Kikabidze aka LONGMAN <[email protected]> |
|
6
|
|
|
* |
|
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
8
|
|
|
* file that was distributed with this source code. |
|
9
|
|
|
*/ |
|
10
|
|
|
|
|
11
|
|
|
namespace Longman\Platfourm\Database\Eloquent\Traits; |
|
12
|
|
|
|
|
13
|
|
|
use Exception; |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* Sorter model trait. |
|
17
|
|
|
* |
|
18
|
|
|
* Usage: |
|
19
|
|
|
* |
|
20
|
|
|
* Model table must have sort_order table column. |
|
21
|
|
|
* |
|
22
|
|
|
* In the model class definition: |
|
23
|
|
|
* |
|
24
|
|
|
* use Longman\Platfourm\Database\Eloquent\Traits\Sorter; |
|
25
|
|
|
* |
|
26
|
|
|
* To set orders: |
|
27
|
|
|
* |
|
28
|
|
|
* $model->setSorterOrder($recordIds, $recordOrders); |
|
29
|
|
|
* |
|
30
|
|
|
* You can change the sort field used by declaring: |
|
31
|
|
|
* |
|
32
|
|
|
* const SORT_ORDER = 'my_sort_order'; |
|
33
|
|
|
*/ |
|
34
|
|
|
trait Sorter |
|
35
|
|
|
{ |
|
36
|
|
|
/** |
|
37
|
|
|
* Boot the Sorter trait for this model. |
|
38
|
|
|
* |
|
39
|
|
|
* @return void |
|
40
|
|
|
*/ |
|
41
|
|
|
public static function bootSorter() |
|
42
|
|
|
{ |
|
43
|
|
|
static::created(function ($model) { |
|
44
|
|
|
$model->setSorterOrder($model->id); |
|
45
|
|
|
}); |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* Sets the sort order of records to the specified orders. If the orders is |
|
50
|
|
|
* undefined, the record identifier is used. |
|
51
|
|
|
*/ |
|
52
|
|
|
public function setSorterOrder($itemIds, $itemOrders = null) |
|
53
|
|
|
{ |
|
54
|
|
|
if (!is_array($itemIds)) { |
|
55
|
|
|
$itemIds = [$itemIds]; |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
if ($itemOrders === null) { |
|
59
|
|
|
$itemOrders = $itemIds; |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
if (count($itemIds) != count($itemOrders)) { |
|
63
|
|
|
throw new Exception('Invalid setSorterOrder call - count of itemIds do not match count of itemOrders'); |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
foreach ($itemIds as $index => $id) { |
|
67
|
|
|
$order = $itemOrders[$index]; |
|
68
|
|
|
$this->newQuery()->where('id', $id)->update([$this->getSortOrderColumn() => $order]); |
|
|
|
|
|
|
69
|
|
|
} |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* Get the name of the "sort order" column. |
|
74
|
|
|
* |
|
75
|
|
|
* @return string |
|
76
|
|
|
*/ |
|
77
|
|
|
public function getSortOrderColumn() |
|
78
|
|
|
{ |
|
79
|
|
|
return defined('static::SORT_ORDER') ? static::SORT_ORDER : 'sort_order'; |
|
80
|
|
|
} |
|
81
|
|
|
} |
|
82
|
|
|
|
This check looks for methods that are used by a trait but not required by it.
To illustrate, let’s look at the following code example
The trait
Idableprovides a methodequalsIdthat in turn relies on the methodgetId(). If this method does not exist on a class mixing in this trait, the method will fail.Adding the
getId()as an abstract method to the trait will make sure it is available.