abschluss
This commit is contained in:
72
app/Entity/Post.php
Normal file
72
app/Entity/Post.php
Normal file
@ -0,0 +1,72 @@
|
||||
<?php
|
||||
namespace Blog\Entity;
|
||||
|
||||
/**
|
||||
* Repräsentiert einen Blogpost mit ID, Titel, Inhalt, Autor und Zeitstempel.
|
||||
*/
|
||||
class Post {
|
||||
/**
|
||||
* Erstellt eine neue Post-Instanz.
|
||||
*
|
||||
* @param int $id Die eindeutige ID des Blogposts.
|
||||
* @param string $title Der Titel des Blogposts.
|
||||
* @param string $content Der Inhalt des Blogposts.
|
||||
* @param string $author Der Name des Autors.
|
||||
* @param int $stamp Der Zeitstempel der Veröffentlichung.
|
||||
*/
|
||||
public function __construct(
|
||||
private int $id,
|
||||
private string $title,
|
||||
private string $content,
|
||||
private string $author,
|
||||
private int $stamp
|
||||
) {}
|
||||
|
||||
/**
|
||||
* Gibt die ID des Blogposts zurück.
|
||||
*
|
||||
* @return int Die eindeutige ID.
|
||||
*/
|
||||
public function getId(): int {
|
||||
return $this->id;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gibt den Titel des Blogposts zurück.
|
||||
*
|
||||
* @return string Der Titel des Blogposts.
|
||||
*/
|
||||
public function getTitle(): string {
|
||||
return $this->title;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gibt den Inhalt des Blogposts zurück. Optional kann eine maximale Länge angegeben werden.
|
||||
*
|
||||
* @param int|null $maxlength Die maximale Zeichenanzahl, falls angegeben.
|
||||
* @return string Der gekürzte oder vollständige Inhalt des Blogposts.
|
||||
*/
|
||||
public function getContent($maxlength = null): string {
|
||||
return $maxlength
|
||||
? mb_strimwidth($this->content, 0, $maxlength, "...")
|
||||
: $this->content;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gibt den Namen des Autors zurück.
|
||||
*
|
||||
* @return string Der Name des Autors.
|
||||
*/
|
||||
public function getAuthor(): string {
|
||||
return $this->author;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gibt das Veröffentlichungsdatum und die Uhrzeit formatiert zurück.
|
||||
*
|
||||
* @return string Das Datum und die Uhrzeit im Format `d.m.Y H:i:s`.
|
||||
*/
|
||||
public function getDateTime(): string {
|
||||
return date('d.m.Y H:i:s', $this->stamp);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user