1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace App\Blog; |
6
|
|
|
|
7
|
|
|
use App\User\User; |
8
|
|
|
use Cycle\Annotated\Annotation\Column; |
9
|
|
|
use Cycle\Annotated\Annotation\Entity; |
10
|
|
|
use Cycle\Annotated\Annotation\Relation\BelongsTo; |
11
|
|
|
use Cycle\Annotated\Annotation\Table; |
12
|
|
|
use Cycle\ORM\Promise\Reference; |
13
|
|
|
use DateTimeImmutable; |
14
|
|
|
use Yiisoft\Security\Random; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* @Entity( |
18
|
|
|
* repository="App\Blog\PostRepository", |
19
|
|
|
* ) |
20
|
|
|
* @Table() |
21
|
|
|
*/ |
22
|
|
|
class Post |
23
|
|
|
{ |
24
|
|
|
/** |
25
|
|
|
* @Column(type="primary") |
26
|
|
|
*/ |
27
|
|
|
private ?int $id = null; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @Column(type="string(128)") |
31
|
|
|
*/ |
32
|
|
|
private string $slug; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @Column(type="string(255)", default="") |
36
|
|
|
*/ |
37
|
|
|
private string $title = ''; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @Column(type="int(11)", default="0") |
41
|
|
|
*/ |
42
|
|
|
private int $status = 0; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @Column(type="text") |
46
|
|
|
*/ |
47
|
|
|
private string $content; |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @Column(type="datetime") |
51
|
|
|
*/ |
52
|
|
|
private DateTimeImmutable $created_at; |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @Column(type="datetime") |
56
|
|
|
*/ |
57
|
|
|
private DateTimeImmutable $updated_at; |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @BelongsTo(target="App\User\User", nullable=false) |
61
|
|
|
* |
62
|
|
|
* @var Reference|User |
63
|
|
|
*/ |
64
|
|
|
private $user = null; |
65
|
|
|
private ?int $user_id = null; |
|
|
|
|
66
|
|
|
|
67
|
|
|
public function __construct() |
68
|
|
|
{ |
69
|
|
|
$this->created_at = new DateTimeImmutable(); |
70
|
|
|
$this->updated_at = new DateTimeImmutable(); |
71
|
|
|
$this->resetSlug(); |
72
|
|
|
} |
73
|
|
|
|
74
|
2 |
|
public function getId(): ?int |
75
|
|
|
{ |
76
|
2 |
|
return $this->id; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
public function getSlug(): ?string |
80
|
|
|
{ |
81
|
|
|
return $this->slug; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
public function resetSlug(): void |
85
|
|
|
{ |
86
|
|
|
$this->slug = Random::string(128); |
87
|
|
|
} |
88
|
|
|
|
89
|
2 |
|
public function getTitle(): string |
90
|
|
|
{ |
91
|
2 |
|
return $this->title; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
public function setTitle(string $title): void |
95
|
|
|
{ |
96
|
|
|
$this->title = $title; |
97
|
|
|
} |
98
|
|
|
|
99
|
2 |
|
public function getContent(): string |
100
|
|
|
{ |
101
|
2 |
|
return $this->content; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
public function setContent(string $content): void |
105
|
|
|
{ |
106
|
|
|
$this->content = $content; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
public function setStatus(PostStatus $status): void |
110
|
|
|
{ |
111
|
|
|
$this->status = $status->getValue(); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
public function getCreatedAt(): DateTimeImmutable |
115
|
|
|
{ |
116
|
|
|
return $this->created_at; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
public function getUpdatedAt(): DateTimeImmutable |
120
|
|
|
{ |
121
|
|
|
return $this->updated_at; |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
public function setUser(User $user): void |
125
|
|
|
{ |
126
|
|
|
$this->user = $user; |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* @return Reference|User |
131
|
|
|
*/ |
132
|
|
|
public function getUser(): ?User |
133
|
|
|
{ |
134
|
|
|
return $this->user; |
|
|
|
|
135
|
|
|
} |
136
|
|
|
} |
137
|
|
|
|