2013-12-18 15:10:53 +01:00
|
|
|
<?php
|
2014-05-06 00:54:15 +02:00
|
|
|
final class CommentEntity extends AbstractEntity implements IValidatable
|
2013-12-18 15:10:53 +01:00
|
|
|
{
|
2014-05-09 17:54:52 +02:00
|
|
|
private $text;
|
|
|
|
private $postId;
|
|
|
|
private $commentDate;
|
|
|
|
private $commenterId;
|
|
|
|
|
|
|
|
public function fillNew()
|
|
|
|
{
|
|
|
|
$this->commentDate = time();
|
|
|
|
}
|
|
|
|
|
|
|
|
public function fillFromDatabase($row)
|
|
|
|
{
|
|
|
|
$this->id = (int) $row['id'];
|
|
|
|
$this->text = $row['text'];
|
|
|
|
$this->postId = (int) $row['post_id'];
|
|
|
|
$this->commentDate = $row['comment_date'];
|
|
|
|
$this->commenterId = (int) $row['commenter_id'];
|
|
|
|
}
|
2013-12-18 15:10:53 +01:00
|
|
|
|
2014-05-05 09:36:08 +02:00
|
|
|
public function validate()
|
|
|
|
{
|
2014-05-06 00:54:15 +02:00
|
|
|
$config = getConfig();
|
|
|
|
|
2014-05-09 21:23:54 +02:00
|
|
|
if (strlen($this->getText()) < $config->comments->minLength)
|
2014-05-06 00:54:15 +02:00
|
|
|
throw new SimpleException('Comment must have at least %d characters', $config->comments->minLength);
|
|
|
|
|
2014-05-09 21:23:54 +02:00
|
|
|
if (strlen($this->getText()) > $config->comments->maxLength)
|
2014-05-06 00:54:15 +02:00
|
|
|
throw new SimpleException('Comment must have at most %d characters', $config->comments->maxLength);
|
|
|
|
|
|
|
|
if (!$this->getPostId())
|
|
|
|
throw new SimpleException('Trying to save comment that doesn\'t refer to any post');
|
|
|
|
|
2014-05-07 20:33:52 +02:00
|
|
|
if (!$this->getCreationTime())
|
2014-05-06 00:54:15 +02:00
|
|
|
throw new SimpleException('Trying to save comment that has no creation date specified');
|
2014-05-05 09:36:08 +02:00
|
|
|
}
|
|
|
|
|
2013-12-18 15:10:53 +01:00
|
|
|
public function getText()
|
|
|
|
{
|
2014-05-06 00:54:15 +02:00
|
|
|
return $this->text;
|
2013-12-18 15:10:53 +01:00
|
|
|
}
|
|
|
|
|
2014-05-06 00:54:15 +02:00
|
|
|
public function getTextMarkdown()
|
2013-12-18 15:10:53 +01:00
|
|
|
{
|
2014-05-06 00:54:15 +02:00
|
|
|
return TextHelper::parseMarkdown($this->getText());
|
2013-12-18 15:10:53 +01:00
|
|
|
}
|
|
|
|
|
2014-05-06 00:54:15 +02:00
|
|
|
public function setText($text)
|
2013-12-18 15:10:53 +01:00
|
|
|
{
|
2014-05-09 21:23:54 +02:00
|
|
|
$this->text = $text === null ? null : trim($text);
|
2013-12-18 15:10:53 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public function getPost()
|
|
|
|
{
|
|
|
|
if ($this->hasCache('post'))
|
|
|
|
return $this->getCache('post');
|
2014-05-08 08:54:08 +02:00
|
|
|
$post = PostModel::getById($this->getPostId());
|
2013-12-18 15:10:53 +01:00
|
|
|
$this->setCache('post', $post);
|
|
|
|
return $post;
|
|
|
|
}
|
|
|
|
|
2014-05-06 00:54:15 +02:00
|
|
|
public function getPostId()
|
|
|
|
{
|
|
|
|
return $this->postId;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function setPost($post)
|
|
|
|
{
|
|
|
|
$this->setCache('post', $post);
|
|
|
|
$this->postId = $post->getId();
|
|
|
|
}
|
|
|
|
|
2014-05-07 20:33:52 +02:00
|
|
|
public function getCreationTime()
|
2014-05-06 00:54:15 +02:00
|
|
|
{
|
|
|
|
return $this->commentDate;
|
|
|
|
}
|
|
|
|
|
2014-05-07 20:33:52 +02:00
|
|
|
public function setCreationTime($unixTime)
|
2014-05-06 00:54:15 +02:00
|
|
|
{
|
2014-05-07 20:33:52 +02:00
|
|
|
$this->commentDate = $unixTime;
|
2014-05-06 00:54:15 +02:00
|
|
|
}
|
|
|
|
|
2013-12-18 15:10:53 +01:00
|
|
|
public function getCommenter()
|
|
|
|
{
|
|
|
|
if ($this->hasCache('commenter'))
|
|
|
|
return $this->getCache('commenter');
|
2014-05-07 18:39:14 +02:00
|
|
|
if (!$this->commenterId)
|
|
|
|
return null;
|
2014-05-08 08:54:08 +02:00
|
|
|
$user = UserModel::tryGetById($this->commenterId);
|
2013-12-18 15:10:53 +01:00
|
|
|
$this->setCache('commenter', $user);
|
|
|
|
return $user;
|
|
|
|
}
|
2014-05-06 00:54:15 +02:00
|
|
|
|
|
|
|
public function getCommenterId()
|
|
|
|
{
|
|
|
|
return $this->commenterId;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function setCommenter($user)
|
|
|
|
{
|
|
|
|
$this->setCache('commenter', $user);
|
|
|
|
$this->commenterId = $user ? $user->getId() : null;
|
|
|
|
}
|
2013-12-18 15:10:53 +01:00
|
|
|
}
|