1
|
|
|
<?php namespace Sofa\Revisionable\Laravel; |
2
|
|
|
|
3
|
|
|
use Illuminate\Database\Eloquent\Model; |
4
|
|
|
use Illuminate\Database\Eloquent\Collection; |
5
|
|
|
|
6
|
|
|
class Revision extends Model |
7
|
|
|
{ |
8
|
|
|
/** |
9
|
|
|
* The database table used by the model. |
10
|
|
|
* |
11
|
|
|
* @var string |
12
|
|
|
*/ |
13
|
|
|
protected static $customTable; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* Allow mass assignement. |
17
|
|
|
* |
18
|
|
|
* @var array |
19
|
|
|
*/ |
20
|
|
|
protected $fillable = ['*']; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* {@inheritdoc} |
24
|
|
|
*/ |
25
|
|
|
public static function boot() |
26
|
|
|
{ |
27
|
|
|
parent::boot(); |
28
|
|
|
|
29
|
|
|
// Make it read-only |
30
|
|
|
static::saving(function () { |
31
|
|
|
return false; |
32
|
|
|
}); |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Get array of updated fields. |
37
|
|
|
* |
38
|
|
|
* @return array |
39
|
|
|
*/ |
40
|
|
|
public function getUpdated() |
41
|
|
|
{ |
42
|
|
|
return array_keys(array_diff_assoc($this->new, $this->old)); |
|
|
|
|
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Get diff of the old/new arrays. |
47
|
|
|
* |
48
|
|
|
* @return array |
49
|
|
|
*/ |
50
|
|
|
public function getDiff() |
51
|
|
|
{ |
52
|
|
|
$diff = []; |
53
|
|
|
|
54
|
|
|
foreach ($this->getUpdated() as $key) { |
55
|
|
|
$diff[$key]['old'] = $this->old($key); |
|
|
|
|
56
|
|
|
|
57
|
|
|
$diff[$key]['new'] = $this->new($key); |
|
|
|
|
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
return $diff; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Determine whether field was updated during current action. |
65
|
|
|
* |
66
|
|
|
* @param string $key |
67
|
|
|
* @return boolean |
68
|
|
|
*/ |
69
|
|
|
public function isUpdated($key) |
70
|
|
|
{ |
71
|
|
|
return in_array($key, $this->getUpdated()); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Accessor for old property |
76
|
|
|
* |
77
|
|
|
* @return array |
78
|
|
|
*/ |
79
|
|
|
public function getOldAttribute($old) |
80
|
|
|
{ |
81
|
|
|
return (array) json_decode($old); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* Accessor for new property |
86
|
|
|
* |
87
|
|
|
* @return array |
88
|
|
|
*/ |
89
|
|
|
public function getNewAttribute($new) |
90
|
|
|
{ |
91
|
|
|
return (array) json_decode($new); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* Get single value from the new/old array. |
96
|
|
|
* |
97
|
|
|
* @param string $version |
98
|
|
|
* @param string $key |
99
|
|
|
* @return string |
100
|
|
|
*/ |
101
|
|
|
protected function getFromArray($version, $key) |
102
|
|
|
{ |
103
|
|
|
return array_get($this->{$version}, $key); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* {@inheritdoc} |
108
|
|
|
*/ |
109
|
|
|
public function getTable() |
110
|
|
|
{ |
111
|
|
|
$table = $this->table ?: static::$customTable; |
112
|
|
|
|
113
|
|
|
return ($table) ?: parent::getTable(); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* Set custom table name for the model. |
118
|
|
|
* |
119
|
|
|
* @param string $table |
120
|
|
|
* @return void |
121
|
|
|
*/ |
122
|
|
|
public static function setCustomTable($table) |
123
|
|
|
{ |
124
|
|
|
if (!isset(static::$customTable)) { |
125
|
|
|
static::$customTable = $table; |
126
|
|
|
} |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* Handle dynamic method calls. |
131
|
|
|
* |
132
|
|
|
* @param string $method |
133
|
|
|
* @param array $parameters |
134
|
|
|
* @return mixed |
135
|
|
|
*/ |
136
|
|
|
public function __call($method, $parameters) |
137
|
|
|
{ |
138
|
|
View Code Duplication |
if (in_array($method, ['new', 'old'])) { |
|
|
|
|
139
|
|
|
array_unshift($parameters, $method); |
140
|
|
|
|
141
|
|
|
return call_user_func_array([$this, 'getFromArray'], $parameters); |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
if ($method == 'label') { |
145
|
|
|
return reset($parameters); |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
return parent::__call($method, $parameters); |
149
|
|
|
} |
150
|
|
|
} |
151
|
|
|
|
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.