forked from statamic/cms
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRevision.php
144 lines (118 loc) · 3.29 KB
/
Revision.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
<?php
namespace Statamic\Revisions;
use Illuminate\Contracts\Support\Arrayable;
use Statamic\Contracts\Auth\User;
use Statamic\Contracts\Revisions\Revision as Contract;
use Statamic\Data\ExistsAsFile;
use Statamic\Events\RevisionDeleted;
use Statamic\Events\RevisionSaved;
use Statamic\Facades;
use Statamic\Facades\Revision as Revisions;
use Statamic\Support\Traits\FluentlyGetsAndSets;
class Revision implements Contract, Arrayable
{
use FluentlyGetsAndSets, ExistsAsFile;
protected $id;
protected $key;
protected $date;
protected $user;
protected $userId;
protected $message;
protected $action = 'revision';
protected $attributes = [];
public function id($id = null)
{
return $this->fluentlyGetOrSet('id')->value($id);
}
public function user($user = null)
{
if (is_null($user)) {
if ($this->user) {
return $this->user;
}
return $this->user = Facades\User::find($this->userId ?: null);
}
if ($user instanceof User) {
$this->user = $user;
$user = $user->id();
}
$this->userId = $user;
return $this;
}
public function action($action = null)
{
return $this->fluentlyGetOrSet('action')->value($action);
}
public function message($message = null)
{
return $this->fluentlyGetOrSet('message')->value($message);
}
public function attributes($attributes = null)
{
return $this->fluentlyGetOrSet('attributes')->value($attributes);
}
public function attribute(string $key, $value = null)
{
if (func_num_args() == 1) {
return $this->attributes[$key];
}
$this->attributes[$key] = $value;
return $this;
}
public function key($key = null)
{
return $this->fluentlyGetOrSet('key')->value($key);
}
public function date($date = null)
{
return $this->fluentlyGetOrSet('date')->value($date);
}
public function path()
{
return vsprintf('%s/%s/%s.yaml', [
Revisions::directory(),
$this->key(),
$this->date()->timestamp,
]);
}
public function fileData()
{
return [
'action' => $this->action,
'date' => $this->date->timestamp,
'user' => $this->userId ?: null,
'message' => $this->message ?: null,
'attributes' => $this->attributes,
];
}
public function toArray()
{
if ($user = $this->user()) {
$user = [
'id' => $user->id(),
'email' => $user->email(),
'name' => $user->name(),
'avatar' => $user->avatar(),
'initials' => $user->initials(),
];
}
return [
'id' => $this->id,
'action' => $this->action,
'date' => $this->date()->timestamp,
'user' => $user,
'message' => $this->message,
'attributes' => $this->attributes,
];
}
public function save()
{
Revisions::save($this);
RevisionSaved::dispatch($this);
}
public function delete()
{
Revisions::delete($this);
RevisionDeleted::dispatch($this);
}
}