Continued work on getter/setters: post sources

This commit is contained in:
Marcin Kurczewski 2014-05-07 17:52:57 +02:00
parent 329f6a0259
commit 509bf44619
7 changed files with 29 additions and 25 deletions

View file

@ -38,7 +38,7 @@ class AddPostJob extends AbstractJob
'post' => TextHelper::reprPost($post), 'post' => TextHelper::reprPost($post),
'tags' => TextHelper::reprTags($post->getTags()), 'tags' => TextHelper::reprTags($post->getTags()),
'safety' => $post->getSafety()->toString(), 'safety' => $post->getSafety()->toString(),
'source' => $post->source]); 'source' => $post->getSource()]);
Logger::flush(); Logger::flush();

View file

@ -13,7 +13,7 @@ class EditPostSourceJob extends AbstractPostJob
$post = $this->post; $post = $this->post;
$newSource = $this->getArgument(self::SOURCE); $newSource = $this->getArgument(self::SOURCE);
$oldSource = $post->source; $oldSource = $post->getSource();
$post->setSource($newSource); $post->setSource($newSource);
if ($this->getContext() == self::CONTEXT_NORMAL) if ($this->getContext() == self::CONTEXT_NORMAL)
@ -24,7 +24,7 @@ class EditPostSourceJob extends AbstractPostJob
Logger::log('{user} changed source of {post} to {source}', [ Logger::log('{user} changed source of {post} to {source}', [
'user' => TextHelper::reprUser(Auth::getCurrentUser()), 'user' => TextHelper::reprUser(Auth::getCurrentUser()),
'post' => TextHelper::reprPost($post), 'post' => TextHelper::reprPost($post),
'source' => $post->source]); 'source' => $post->getSource()]);
} }
return $post; return $post;

View file

@ -16,16 +16,23 @@ class PostEntity extends AbstractEntity implements IValidatable
public $imageWidth; public $imageWidth;
public $imageHeight; public $imageHeight;
public $uploaderId; public $uploaderId;
public $source; protected $source;
public $commentCount = 0; public $commentCount = 0;
public $favCount = 0; public $favCount = 0;
public $score = 0; public $score = 0;
public function validate() public function validate()
{ {
//todo
if (empty($this->getType())) if (empty($this->getType()))
throw new SimpleException('No post type detected'); 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() public function getUploader()
@ -197,7 +204,6 @@ class PostEntity extends AbstractEntity implements IValidatable
public function setType(PostType $type) public function setType(PostType $type)
{ {
$type->validate();
$this->type = $type; $this->type = $type;
} }
@ -208,13 +214,17 @@ class PostEntity extends AbstractEntity implements IValidatable
public function setSafety(PostSafety $safety) public function setSafety(PostSafety $safety)
{ {
$safety->validate();
$this->safety = $safety; $this->safety = $safety;
} }
public function getSource()
{
return $this->source;
}
public function setSource($source) public function setSource($source)
{ {
$this->source = PostModel::validateSource($source); $this->source = trim($source);
} }
public function getThumbCustomPath($width = null, $height = null) public function getThumbCustomPath($width = null, $height = null)
@ -390,14 +400,19 @@ class PostEntity extends AbstractEntity implements IValidatable
public function getEditToken() public function getEditToken()
{ {
$x = []; $x = [];
foreach ($this->getTags() as $tag) foreach ($this->getTags() as $tag)
$x []= TextHelper::reprTag($tag->getName()); $x []= TextHelper::reprTag($tag->getName());
foreach ($this->getRelations() as $relatedPost) foreach ($this->getRelations() as $relatedPost)
$x []= TextHelper::reprPost($relatedPost); $x []= TextHelper::reprPost($relatedPost);
$x []= $this->getSafety()->toInteger(); $x []= $this->getSafety()->toInteger();
$x []= $this->source; $x []= $this->getSource();
$x []= $this->fileHash; $x []= $this->fileHash;
natcasesort($x); natcasesort($x);
$x = join(' ', $x); $x = join(' ', $x);
return md5($x); return md5($x);
} }

View file

@ -57,7 +57,7 @@ class PostModel extends AbstractCrudModel
'image_width' => $post->imageWidth, 'image_width' => $post->imageWidth,
'image_height' => $post->imageHeight, 'image_height' => $post->imageHeight,
'uploader_id' => $post->uploaderId, 'uploader_id' => $post->uploaderId,
'source' => $post->source, 'source' => $post->getSource(),
]; ];
$stmt = new Sql\UpdateStatement(); $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) public static function validateThumbSize($width, $height)
{ {
$width = $width === null ? getConfig()->browsing->thumbWidth : $width; $width = $width === null ? getConfig()->browsing->thumbWidth : $width;

View file

@ -62,7 +62,7 @@
<input type="text" <input type="text"
name="source" name="source"
id="source" id="source"
value="<?= htmlspecialchars($this->context->transport->post->source) ?>"/> value="<?= htmlspecialchars($this->context->transport->post->getSource()) ?>"/>
</div> </div>
</div> </div>
<?php endif ?> <?php endif ?>

View file

@ -125,8 +125,8 @@ $canEditAnything = count(array_filter($editPostPrivileges)) > 0;
<div class="key-value source"> <div class="key-value source">
<span class="key">Source:</span> <span class="key">Source:</span>
<span class="value" <span class="value"
title="<?= $val = htmlspecialchars($this->context->transport->post->source ?: 'unknown') ?>"> title="<?= $val = htmlspecialchars($this->context->transport->post->getSource() ?: 'unknown') ?>">
<?php if (preg_match('/^((https?|ftp):|)\/\//', $this->context->transport->post->source)): ?> <?php if (preg_match('/^((https?|ftp):|)\/\//', $this->context->transport->post->getSource())): ?>
<a href="<?= $val ?>"><?= $val ?></a> <a href="<?= $val ?>"><?= $val ?></a>
<?php else: ?> <?php else: ?>
<?= $val ?> <?= $val ?>

View file

@ -10,7 +10,7 @@ class EditPostSourceJobTest extends AbstractTest
return $this->runApi('a'); return $this->runApi('a');
}); });
$this->assert->areEqual('a', $post->source); $this->assert->areEqual('a', $post->getSource());
$this->assert->doesNotThrow(function() use ($post) $this->assert->doesNotThrow(function() use ($post)
{ {
PostModel::findById($post->getId()); PostModel::findById($post->getId());