yiisoft /
demo-api
| 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 DateTimeImmutable; |
||
| 12 | use Yiisoft\Security\Random; |
||
| 13 | |||
| 14 | #[Entity(repository: PostRepository::class)] |
||
| 15 | class Post |
||
| 16 | { |
||
| 17 | #[Column(type: 'primary')] |
||
| 18 | private ?int $id = null; |
||
| 19 | |||
| 20 | #[Column(type: 'string(128)')] |
||
| 21 | private string $slug; |
||
| 22 | |||
| 23 | #[Column(type: 'string(255)', default: '')] |
||
| 24 | private string $title = ''; |
||
| 25 | |||
| 26 | #[Column(type: 'int(11)', default: 0)] |
||
| 27 | private int $status = 0; |
||
| 28 | |||
| 29 | #[Column(type: 'string')] |
||
| 30 | private string $content; |
||
| 31 | |||
| 32 | #[Column(type: 'datetime')] |
||
| 33 | private DateTimeImmutable $created_at; |
||
| 34 | |||
| 35 | #[Column(type: 'datetime')] |
||
| 36 | private DateTimeImmutable $updated_at; |
||
| 37 | |||
| 38 | #[BelongsTo(target: User::class, nullable: false)] |
||
| 39 | private ?User $user = null; |
||
| 40 | private ?int $user_id = null; |
||
|
0 ignored issues
–
show
introduced
by
Loading history...
|
|||
| 41 | |||
| 42 | public function __construct() |
||
| 43 | { |
||
| 44 | $this->created_at = new DateTimeImmutable(); |
||
| 45 | $this->updated_at = new DateTimeImmutable(); |
||
| 46 | $this->resetSlug(); |
||
| 47 | } |
||
| 48 | |||
| 49 | public function getId(): ?int |
||
| 50 | { |
||
| 51 | return $this->id; |
||
| 52 | } |
||
| 53 | |||
| 54 | public function getSlug(): ?string |
||
| 55 | { |
||
| 56 | return $this->slug; |
||
| 57 | } |
||
| 58 | |||
| 59 | public function resetSlug(): void |
||
| 60 | { |
||
| 61 | $this->slug = Random::string(128); |
||
| 62 | } |
||
| 63 | |||
| 64 | public function getTitle(): string |
||
| 65 | { |
||
| 66 | return $this->title; |
||
| 67 | 1 | } |
|
| 68 | |||
| 69 | 1 | public function setTitle(string $title): void |
|
| 70 | 1 | { |
|
| 71 | 1 | $this->title = $title; |
|
| 72 | } |
||
| 73 | |||
| 74 | 2 | public function getContent(): string |
|
| 75 | { |
||
| 76 | 2 | return $this->content; |
|
| 77 | } |
||
| 78 | |||
| 79 | public function setContent(string $content): void |
||
| 80 | { |
||
| 81 | $this->content = $content; |
||
| 82 | } |
||
| 83 | |||
| 84 | 1 | public function setStatus(PostStatus $status): void |
|
| 85 | { |
||
| 86 | 1 | $this->status = $status->getValue(); |
|
| 87 | } |
||
| 88 | |||
| 89 | 2 | public function getCreatedAt(): DateTimeImmutable |
|
| 90 | { |
||
| 91 | 2 | return $this->created_at; |
|
| 92 | } |
||
| 93 | |||
| 94 | 2 | public function getUpdatedAt(): DateTimeImmutable |
|
| 95 | { |
||
| 96 | 2 | return $this->updated_at; |
|
| 97 | } |
||
| 98 | |||
| 99 | 2 | public function setUser(User $user): void |
|
| 100 | { |
||
| 101 | 2 | $this->user = $user; |
|
| 102 | } |
||
| 103 | |||
| 104 | 2 | public function getUser(): ?User |
|
| 105 | { |
||
| 106 | 2 | return $this->user; |
|
| 107 | } |
||
| 108 | } |
||
| 109 |