|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace RichanFongdasen\EloquentBlameable; |
|
4
|
|
|
|
|
5
|
|
|
use Illuminate\Database\Eloquent\Model; |
|
6
|
|
|
|
|
7
|
|
|
class BlameableService |
|
8
|
|
|
{ |
|
9
|
|
|
/** |
|
10
|
|
|
* Global configurations from config/blameable.php. |
|
11
|
|
|
* |
|
12
|
|
|
* @var array |
|
13
|
|
|
*/ |
|
14
|
|
|
private $globalConfig; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* Blameable Service Constructor. |
|
18
|
|
|
*/ |
|
19
|
|
|
public function __construct() |
|
20
|
|
|
{ |
|
21
|
|
|
$this->loadConfig(); |
|
22
|
|
|
} |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* Get configurations for the given Model. |
|
26
|
|
|
* |
|
27
|
|
|
* @param \Illuminate\Database\Eloquent\Model $model |
|
28
|
|
|
* |
|
29
|
|
|
* @return array |
|
30
|
|
|
*/ |
|
31
|
|
|
private function getConfigurations(Model $model) |
|
32
|
|
|
{ |
|
33
|
|
|
$modelConfigurations = method_exists($model, 'blameable') ? |
|
34
|
|
|
$model->blameable() : []; |
|
35
|
|
|
|
|
36
|
|
|
return array_merge($this->globalConfig, $modelConfigurations); |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* Get current configuration value for the given attributes. |
|
41
|
|
|
* |
|
42
|
|
|
* @param \Illuminate\Database\Eloquent\Model $model |
|
43
|
|
|
* @param string $key |
|
44
|
|
|
* |
|
45
|
|
|
* @return string |
|
46
|
|
|
*/ |
|
47
|
|
|
public function getConfiguration(Model $model, $key) |
|
48
|
|
|
{ |
|
49
|
|
|
return data_get($this->getConfigurations($model), $key); |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* Load default blameable configurations. |
|
54
|
|
|
* |
|
55
|
|
|
* @return void |
|
56
|
|
|
*/ |
|
57
|
|
|
public function loadConfig() |
|
58
|
|
|
{ |
|
59
|
|
|
$this->globalConfig = app('config')->get('blameable'); |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* Set Model's attribute value for the given key. |
|
64
|
|
|
* |
|
65
|
|
|
* @param Model $model |
|
66
|
|
|
* @param string $key |
|
67
|
|
|
* @param bool $reset |
|
68
|
|
|
* |
|
69
|
|
|
* @return bool |
|
70
|
|
|
*/ |
|
71
|
|
|
public function setAttribute(Model $model, $key, $reset = false) |
|
72
|
|
|
{ |
|
73
|
|
|
$attribute = $this->getConfiguration($model, $key); |
|
74
|
|
|
|
|
75
|
|
|
if ($attribute) { |
|
76
|
|
|
$value = $reset ? null : blameable_user($model); |
|
77
|
|
|
$model->setAttribute($attribute, $value); |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
return $model->isDirty($attribute); |
|
81
|
|
|
} |
|
82
|
|
|
} |
|
83
|
|
|
|