|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* @link https://github.com/paulzi/yii2-sortable |
|
4
|
|
|
* @copyright Copyright (c) 2015 PaulZi <[email protected]> |
|
5
|
|
|
* @license MIT (https://github.com/paulzi/yii2-sortable/blob/master/LICENSE) |
|
6
|
|
|
*/ |
|
7
|
|
|
|
|
8
|
|
|
namespace paulzi\sortable; |
|
9
|
|
|
|
|
10
|
|
|
use yii\base\InvalidConfigException; |
|
11
|
|
|
use yii\db\ActiveRecord; |
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* Sortable Trait |
|
15
|
|
|
* @author PaulZi <[email protected]> |
|
16
|
|
|
*/ |
|
17
|
|
|
trait SortableTrait |
|
18
|
|
|
{ |
|
19
|
|
|
/** |
|
20
|
|
|
* @var SortableBehavior |
|
21
|
|
|
*/ |
|
22
|
|
|
private $_sortableBehavior; |
|
23
|
|
|
|
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* @throws InvalidConfigException |
|
27
|
|
|
*/ |
|
28
|
|
|
private function getSortableBehavior() |
|
29
|
|
|
{ |
|
30
|
|
|
if ($this->_sortableBehavior === null) { |
|
31
|
|
|
/** @var \yii\base\Component|self $this */ |
|
32
|
|
|
foreach ($this->getBehaviors() as $behavior) { |
|
|
|
|
|
|
33
|
|
|
if ($behavior instanceof SortableBehavior) { |
|
34
|
|
|
$this->_sortableBehavior = $behavior; |
|
35
|
|
|
} |
|
36
|
|
|
} |
|
37
|
|
|
if ($this->_sortableBehavior === null) { |
|
38
|
|
|
throw new InvalidConfigException('SortableBehavior is not attached to model'); |
|
39
|
|
|
} |
|
40
|
|
|
} |
|
41
|
|
|
return $this->_sortableBehavior; |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* @return integer |
|
46
|
|
|
*/ |
|
47
|
|
|
public function getSortablePosition() |
|
48
|
|
|
{ |
|
49
|
|
|
return $this->getSortableBehavior()->getSortablePosition(); |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* @return $this |
|
54
|
|
|
*/ |
|
55
|
|
|
public function moveFirst() |
|
56
|
|
|
{ |
|
57
|
|
|
return $this->getSortableBehavior()->moveFirst(); |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* @return $this |
|
62
|
|
|
*/ |
|
63
|
|
|
public function moveLast() |
|
64
|
|
|
{ |
|
65
|
|
|
return $this->getSortableBehavior()->moveLast(); |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* @param integer $position |
|
70
|
|
|
* @param bool $forward Move existing items to forward or backward |
|
71
|
|
|
* @return $this |
|
72
|
|
|
*/ |
|
73
|
|
|
public function moveTo($position, $forward = true) |
|
74
|
|
|
{ |
|
75
|
|
|
return $this->getSortableBehavior()->moveTo($position, $forward); |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
/** |
|
79
|
|
|
* @param ActiveRecord $model |
|
80
|
|
|
* @return $this |
|
81
|
|
|
*/ |
|
82
|
|
|
public function moveBefore($model) |
|
83
|
|
|
{ |
|
84
|
|
|
return $this->getSortableBehavior()->moveBefore($model); |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
/** |
|
88
|
|
|
* @param ActiveRecord $model |
|
89
|
|
|
* @return $this |
|
90
|
|
|
*/ |
|
91
|
|
|
public function moveAfter($model) |
|
92
|
|
|
{ |
|
93
|
|
|
return $this->getSortableBehavior()->moveAfter($model); |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
/** |
|
97
|
|
|
* Reorders items with values of sortAttribute begin from zero. |
|
98
|
|
|
* @param bool $middle |
|
99
|
|
|
* @return integer |
|
100
|
|
|
* @throws \Exception |
|
101
|
|
|
*/ |
|
102
|
|
|
public function reorder($middle = true) |
|
103
|
|
|
{ |
|
104
|
|
|
return $this->getSortableBehavior()->reorder($middle); |
|
105
|
|
|
} |
|
106
|
|
|
} |
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.