1 | <?php |
||
2 | |||
3 | namespace RichanFongdasen\EloquentBlameable; |
||
4 | |||
5 | use Illuminate\Database\Eloquent\Model; |
||
6 | |||
7 | class BlameableService |
||
8 | { |
||
9 | /** |
||
10 | * Get configurations for the given Model. |
||
11 | * |
||
12 | * @param \Illuminate\Database\Eloquent\Model $model |
||
13 | * |
||
14 | * @return string[] |
||
15 | */ |
||
16 | private function getConfigurations(Model $model): array |
||
17 | { |
||
18 | $modelConfigurations = method_exists($model, 'blameable') ? |
||
19 | $model->blameable() : []; |
||
20 | |||
21 | return array_merge((array) config('blameable'), $modelConfigurations); |
||
0 ignored issues
–
show
Bug
introduced
by
![]() |
|||
22 | } |
||
23 | |||
24 | /** |
||
25 | * Get current configuration value for the given attributes. |
||
26 | * |
||
27 | * @param \Illuminate\Database\Eloquent\Model $model |
||
28 | * @param string $key |
||
29 | * |
||
30 | * @return string|null |
||
31 | */ |
||
32 | public function getConfiguration(Model $model, string $key): ?string |
||
33 | { |
||
34 | $value = data_get($this->getConfigurations($model), $key); |
||
35 | |||
36 | return is_string($value) ? $value : null; |
||
37 | } |
||
38 | |||
39 | /** |
||
40 | * Set Model's attribute value for the given key. |
||
41 | * |
||
42 | * @param Model $model |
||
43 | * @param string $key |
||
44 | * @param int|string|null $userId |
||
45 | * |
||
46 | * @return bool |
||
47 | */ |
||
48 | public function setAttribute(Model $model, string $key, $userId): bool |
||
49 | { |
||
50 | $attribute = $this->getConfiguration($model, $key); |
||
51 | |||
52 | if ($attribute !== null) { |
||
53 | $model->setAttribute($attribute, $userId); |
||
54 | } |
||
55 | |||
56 | return $model->isDirty($attribute); |
||
57 | } |
||
58 | } |
||
59 |