uuid = Uuid::v4()->toRfc4122(); $this->author = $author; $this->title = $title; $this->slug = $this->generateSlug($title); $this->body = $body; $this->createdAt = time(); $this->updatedAt = time(); } public function getId(): ?int { return $this->id; } public function getUuid(): string { return $this->uuid; } public function getTitle(): string { return $this->title; } public function getSlug(): string { return $this->slug; } public function getBody(): string { return $this->body; } public function getSummary(): ?string { return $this->summary; } public function getImageUrl(): ?string { return $this->imageUrl; } public function getImagePath(): ?string { return $this->imagePath; } public function getAuthor(): User { return $this->author; } public function getTags(): array { return $this->tags; } public function getStatus(): string { return $this->status; } public function getCity(): ?City { return $this->city; } public function setTitle(string $v): self { $this->title = $v; $this->touch(); return $this; } public function setSlug(string $v): self { $this->slug = $v; $this->touch(); return $this; } public function setBody(string $v): self { $this->body = $v; $this->touch(); return $this; } public function setSummary(?string $v): self { $this->summary = $v; $this->touch(); return $this; } public function setImageUrl(?string $v): self { $this->imageUrl = $v; $this->touch(); return $this; } public function setImagePath(?string $v): self { $this->imagePath = $v; $this->touch(); return $this; } public function setTags(array $v): self { $this->tags = $v; $this->touch(); return $this; } public function setStatus(string $v): self { $this->status = $v; $this->touch(); return $this; } /** null = پست سراسری (روی همهٔ دامنه‌ها، canonical روی دامنهٔ اصلی) */ public function setCity(?City $v): self { $this->city = $v; $this->touch(); return $this; } private function touch(): void { $this->updatedAt = time(); } private function generateSlug(string $title): string { $slug = mb_strtolower(trim($title)); $slug = preg_replace('/\s+/', '-', $slug); $slug = preg_replace('/[^a-z0-9\-\p{Arabic}]/u', '', $slug); return $slug . '-' . substr(str_replace('-', '', $this->uuid), 0, 8); } public function toArray(): array { return [ 'uuid' => $this->uuid, 'title' => $this->title, 'slug' => $this->slug, 'summary' => $this->summary, 'body' => $this->body, 'image_url' => $this->imageUrl, 'tags' => $this->tags, 'status' => $this->status, 'author' => $this->authorToArray(), 'city' => $this->cityToArray(), 'created_at' => $this->createdAt, 'updated_at' => $this->updatedAt, ]; } /** null = پست سراسری. مصرف‌کننده روی همین null تصمیم canonical می‌گیرد. */ private function cityToArray(): ?array { if ($this->city === null) { return null; } return [ 'id' => (string) $this->city->getId(), 'name' => $this->city->getName(), ]; } private function authorToArray(): ?array { if ($this->author === null) { return null; } // The referenced user may have been deleted; a lazy proxy then throws // EntityNotFoundException on access. Treat a missing author as null. try { return [ 'uuid' => $this->author->getUuid(), 'name' => $this->author->getRealName(), ]; } catch (\Doctrine\ORM\EntityNotFoundException) { return null; } } public function toListArray(): array { return [ 'uuid' => $this->uuid, 'title' => $this->title, 'slug' => $this->slug, 'summary' => $this->summary, 'image_url' => $this->imageUrl, 'tags' => $this->tags, 'status' => $this->status, 'city' => $this->cityToArray(), 'created_at' => $this->createdAt, ]; } }