Continued work on getter/setters: post sources
This commit is contained in:
parent
329f6a0259
commit
509bf44619
7 changed files with 29 additions and 25 deletions
|
@ -38,7 +38,7 @@ class AddPostJob extends AbstractJob
|
|||
'post' => TextHelper::reprPost($post),
|
||||
'tags' => TextHelper::reprTags($post->getTags()),
|
||||
'safety' => $post->getSafety()->toString(),
|
||||
'source' => $post->source]);
|
||||
'source' => $post->getSource()]);
|
||||
|
||||
Logger::flush();
|
||||
|
||||
|
|
|
@ -13,7 +13,7 @@ class EditPostSourceJob extends AbstractPostJob
|
|||
$post = $this->post;
|
||||
$newSource = $this->getArgument(self::SOURCE);
|
||||
|
||||
$oldSource = $post->source;
|
||||
$oldSource = $post->getSource();
|
||||
$post->setSource($newSource);
|
||||
|
||||
if ($this->getContext() == self::CONTEXT_NORMAL)
|
||||
|
@ -24,7 +24,7 @@ class EditPostSourceJob extends AbstractPostJob
|
|||
Logger::log('{user} changed source of {post} to {source}', [
|
||||
'user' => TextHelper::reprUser(Auth::getCurrentUser()),
|
||||
'post' => TextHelper::reprPost($post),
|
||||
'source' => $post->source]);
|
||||
'source' => $post->getSource()]);
|
||||
}
|
||||
|
||||
return $post;
|
||||
|
|
|
@ -16,16 +16,23 @@ class PostEntity extends AbstractEntity implements IValidatable
|
|||
public $imageWidth;
|
||||
public $imageHeight;
|
||||
public $uploaderId;
|
||||
public $source;
|
||||
protected $source;
|
||||
public $commentCount = 0;
|
||||
public $favCount = 0;
|
||||
public $score = 0;
|
||||
|
||||
public function validate()
|
||||
{
|
||||
//todo
|
||||
if (empty($this->getType()))
|
||||
throw new SimpleException('No post type detected');
|
||||
|
||||
$this->getType()->validate();
|
||||
|
||||
$this->getSafety()->validate();
|
||||
|
||||
$maxSourceLength = getConfig()->posts->maxSourceLength;
|
||||
if (strlen($this->getSource()) > $maxSourceLength)
|
||||
throw new SimpleException('Source must have at most %d characters', $maxSourceLength);
|
||||
}
|
||||
|
||||
public function getUploader()
|
||||
|
@ -197,7 +204,6 @@ class PostEntity extends AbstractEntity implements IValidatable
|
|||
|
||||
public function setType(PostType $type)
|
||||
{
|
||||
$type->validate();
|
||||
$this->type = $type;
|
||||
}
|
||||
|
||||
|
@ -208,13 +214,17 @@ class PostEntity extends AbstractEntity implements IValidatable
|
|||
|
||||
public function setSafety(PostSafety $safety)
|
||||
{
|
||||
$safety->validate();
|
||||
$this->safety = $safety;
|
||||
}
|
||||
|
||||
public function getSource()
|
||||
{
|
||||
return $this->source;
|
||||
}
|
||||
|
||||
public function setSource($source)
|
||||
{
|
||||
$this->source = PostModel::validateSource($source);
|
||||
$this->source = trim($source);
|
||||
}
|
||||
|
||||
public function getThumbCustomPath($width = null, $height = null)
|
||||
|
@ -390,14 +400,19 @@ class PostEntity extends AbstractEntity implements IValidatable
|
|||
public function getEditToken()
|
||||
{
|
||||
$x = [];
|
||||
|
||||
foreach ($this->getTags() as $tag)
|
||||
$x []= TextHelper::reprTag($tag->getName());
|
||||
|
||||
foreach ($this->getRelations() as $relatedPost)
|
||||
$x []= TextHelper::reprPost($relatedPost);
|
||||
|
||||
$x []= $this->getSafety()->toInteger();
|
||||
$x []= $this->source;
|
||||
$x []= $this->getSource();
|
||||
$x []= $this->fileHash;
|
||||
|
||||
natcasesort($x);
|
||||
|
||||
$x = join(' ', $x);
|
||||
return md5($x);
|
||||
}
|
||||
|
|
|
@ -57,7 +57,7 @@ class PostModel extends AbstractCrudModel
|
|||
'image_width' => $post->imageWidth,
|
||||
'image_height' => $post->imageHeight,
|
||||
'uploader_id' => $post->uploaderId,
|
||||
'source' => $post->source,
|
||||
'source' => $post->getSource(),
|
||||
];
|
||||
|
||||
$stmt = new Sql\UpdateStatement();
|
||||
|
@ -273,17 +273,6 @@ class PostModel extends AbstractCrudModel
|
|||
|
||||
|
||||
|
||||
public static function validateSource($source)
|
||||
{
|
||||
$source = trim($source);
|
||||
|
||||
$maxLength = getConfig()->posts->maxSourceLength;
|
||||
if (strlen($source) > $maxLength)
|
||||
throw new SimpleException('Source must have at most %d characters', $maxLength);
|
||||
|
||||
return $source;
|
||||
}
|
||||
|
||||
public static function validateThumbSize($width, $height)
|
||||
{
|
||||
$width = $width === null ? getConfig()->browsing->thumbWidth : $width;
|
||||
|
|
|
@ -62,7 +62,7 @@
|
|||
<input type="text"
|
||||
name="source"
|
||||
id="source"
|
||||
value="<?= htmlspecialchars($this->context->transport->post->source) ?>"/>
|
||||
value="<?= htmlspecialchars($this->context->transport->post->getSource()) ?>"/>
|
||||
</div>
|
||||
</div>
|
||||
<?php endif ?>
|
||||
|
|
|
@ -125,8 +125,8 @@ $canEditAnything = count(array_filter($editPostPrivileges)) > 0;
|
|||
<div class="key-value source">
|
||||
<span class="key">Source:</span>
|
||||
<span class="value"
|
||||
title="<?= $val = htmlspecialchars($this->context->transport->post->source ?: 'unknown') ?>">
|
||||
<?php if (preg_match('/^((https?|ftp):|)\/\//', $this->context->transport->post->source)): ?>
|
||||
title="<?= $val = htmlspecialchars($this->context->transport->post->getSource() ?: 'unknown') ?>">
|
||||
<?php if (preg_match('/^((https?|ftp):|)\/\//', $this->context->transport->post->getSource())): ?>
|
||||
<a href="<?= $val ?>"><?= $val ?></a>
|
||||
<?php else: ?>
|
||||
<?= $val ?>
|
||||
|
|
|
@ -10,7 +10,7 @@ class EditPostSourceJobTest extends AbstractTest
|
|||
return $this->runApi('a');
|
||||
});
|
||||
|
||||
$this->assert->areEqual('a', $post->source);
|
||||
$this->assert->areEqual('a', $post->getSource());
|
||||
$this->assert->doesNotThrow(function() use ($post)
|
||||
{
|
||||
PostModel::findById($post->getId());
|
||||
|
|
Loading…
Reference in a new issue