|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace miBadger\ActiveRecord\Traits; |
|
4
|
|
|
|
|
5
|
|
|
use miBadger\Query\Query; |
|
6
|
|
|
use miBadger\ActiveRecord\ColumnProperty; |
|
7
|
|
|
|
|
8
|
|
|
const TRAIT_SOFT_DELETE_FIELD_KEY = "soft_delete"; |
|
9
|
|
|
|
|
10
|
|
|
trait SoftDelete |
|
11
|
|
|
{ |
|
12
|
|
|
/** @var boolean the soft delete status for the entity this trait is embedded into. */ |
|
13
|
|
|
protected $softDelete; |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* this method is required to be called in the constructor for each class that uses this trait. |
|
17
|
|
|
* It adds the required fields to the table definition and registers hooks |
|
18
|
|
|
*/ |
|
19
|
10 |
|
protected function initSoftDelete() |
|
20
|
|
|
{ |
|
21
|
10 |
|
$this->softDelete = false; |
|
22
|
|
|
|
|
23
|
10 |
|
$this->extendTableDefinition(TRAIT_SOFT_DELETE_FIELD_KEY, [ |
|
|
|
|
|
|
24
|
10 |
|
'value' => &$this->softDelete, |
|
25
|
|
|
'validate' => null, |
|
26
|
10 |
|
'default' => 0, |
|
27
|
10 |
|
'type' => 'INT', |
|
28
|
10 |
|
'length' => 1, |
|
29
|
10 |
|
'properties' => ColumnProperty::NOT_NULL |
|
30
|
|
|
]); |
|
31
|
|
|
|
|
32
|
10 |
|
$this->registerSearchHook(TRAIT_SOFT_DELETE_FIELD_KEY, 'softDeleteSearchHook'); |
|
|
|
|
|
|
33
|
10 |
|
} |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* The hook that gets called whenever a query is made |
|
37
|
|
|
*/ |
|
38
|
|
|
protected function softDeleteSearchHook(Query $query) |
|
39
|
|
|
{ |
|
40
|
|
|
$query->where(TRAIT_SOFT_DELETE_FIELD_KEY, '=', 0); |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* returns the name for the soft delete field in the database |
|
45
|
|
|
* @return string |
|
46
|
|
|
*/ |
|
47
|
|
|
public function getSoftDeleteFieldName() |
|
48
|
|
|
{ |
|
49
|
|
|
return TRAIT_SOFT_DELETE_FIELD_KEY; |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* Mark the current record as soft deleted |
|
54
|
|
|
* @return $this |
|
55
|
|
|
*/ |
|
56
|
1 |
|
public function softDelete() |
|
57
|
|
|
{ |
|
58
|
1 |
|
$this->softDelete = true; |
|
59
|
1 |
|
return $this; |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* Undo the current soft deletion status (mark it as non-soft deleted) |
|
64
|
|
|
* @return $this |
|
65
|
|
|
*/ |
|
66
|
1 |
|
public function softRestore() |
|
67
|
|
|
{ |
|
68
|
1 |
|
$this->softDelete = false; |
|
69
|
1 |
|
return $this; |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* returns the current soft deletion status |
|
74
|
|
|
* @return $this |
|
75
|
|
|
*/ |
|
76
|
1 |
|
public function getDeletionStatus() |
|
77
|
|
|
{ |
|
78
|
1 |
|
return $this->softDelete; |
|
79
|
|
|
} |
|
80
|
|
|
} |
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.