Added post note information to post snapshots

This commit is contained in:
Marcin Kurczewski 2014-10-26 01:16:14 +02:00
parent e4424d5191
commit 31c4584e94
4 changed files with 53 additions and 11 deletions

3
TODO
View file

@ -10,9 +10,6 @@ first major release.
- tags: add tag descriptions - tags: add tag descriptions
- tags: add tag edit snapshots (backed-only) - tags: add tag edit snapshots (backed-only)
- post notes
- post notes history
refactors: refactors:
- add enum validation in IValidatables (needs refactors of enums and - add enum validation in IValidatables (needs refactors of enums and
possible disposal of EnumHelper in favor of something more subtle) possible disposal of EnumHelper in favor of something more subtle)

View file

@ -23,10 +23,10 @@ class PostNoteEntityConverter extends AbstractEntityConverter implements IEntity
{ {
$entity = new PostNote($array['id']); $entity = new PostNote($array['id']);
$entity->setPostId($array['postId']); $entity->setPostId($array['postId']);
$entity->setLeft($array['x']); $entity->setLeft(intval($array['x']));
$entity->setTop($array['y']); $entity->setTop(intval($array['y']));
$entity->setWidth($array['width']); $entity->setWidth(intval($array['width']));
$entity->setHeight($array['height']); $entity->setHeight(intval($array['height']));
$entity->setText($array['text']); $entity->setText($array['text']);
return $entity; return $entity;
} }

View file

@ -1,18 +1,25 @@
<?php <?php
namespace Szurubooru\Services; namespace Szurubooru\Services;
use Szurubooru\Dao\GlobalParamDao; use Szurubooru\Dao\GlobalParamDao;
use Szurubooru\Dao\PostNoteDao;
use Szurubooru\Entities\GlobalParam; use Szurubooru\Entities\GlobalParam;
use Szurubooru\Entities\Post; use Szurubooru\Entities\Post;
use Szurubooru\Entities\PostNote;
use Szurubooru\Entities\Snapshot; use Szurubooru\Entities\Snapshot;
use Szurubooru\Entities\Tag;
use Szurubooru\Helpers\EnumHelper; use Szurubooru\Helpers\EnumHelper;
class PostSnapshotProvider class PostSnapshotProvider
{ {
private $globalParamDao; private $globalParamDao;
private $postNoteDao;
public function __construct(GlobalParamDao $globalParamDao) public function __construct(
GlobalParamDao $globalParamDao,
PostNoteDao $postNoteDao)
{ {
$this->globalParamDao = $globalParamDao; $this->globalParamDao = $globalParamDao;
$this->postNoteDao = $postNoteDao;
} }
public function getPostChangeSnapshot(Post $post) public function getPostChangeSnapshot(Post $post)
@ -33,9 +40,21 @@ class PostSnapshotProvider
'contentChecksum' => $post->getContentChecksum(), 'contentChecksum' => $post->getContentChecksum(),
'featured' => $isFeatured, 'featured' => $isFeatured,
'notes' => array_values(array_map(function (PostNote $note)
{
return [
'x' => intval($note->getLeft()),
'y' => intval($note->getTop()),
'w' => intval($note->getWidth()),
'h' => intval($note->getHeight()),
'text' => trim($note->getText()),
];
},
$this->postNoteDao->findByPostId($post->getId()))),
'tags' => 'tags' =>
array_values(array_map( array_values(array_map(
function ($tag) function (Tag $tag)
{ {
return $tag->getName(); return $tag->getName();
}, },
@ -43,7 +62,7 @@ class PostSnapshotProvider
'relations' => 'relations' =>
array_values(array_map( array_values(array_map(
function ($post) function (Post $post)
{ {
return $post->getId(); return $post->getId();
}, },
@ -54,6 +73,7 @@ class PostSnapshotProvider
sort($data['tags']); sort($data['tags']);
sort($data['relations']); sort($data['relations']);
usort($data['notes'], function ($note1, $note2) { return $note1['x'] - $note2['x']; });
$snapshot = $this->getPostSnapshot($post); $snapshot = $this->getPostSnapshot($post);
$snapshot->setOperation(Snapshot::OPERATION_CHANGE); $snapshot->setOperation(Snapshot::OPERATION_CHANGE);

View file

@ -1,8 +1,10 @@
<?php <?php
namespace Szurubooru\Tests\Services; namespace Szurubooru\Tests\Services;
use Szurubooru\Dao\GlobalParamDao; use Szurubooru\Dao\GlobalParamDao;
use Szurubooru\Dao\PostNoteDao;
use Szurubooru\Entities\GlobalParam; use Szurubooru\Entities\GlobalParam;
use Szurubooru\Entities\Post; use Szurubooru\Entities\Post;
use Szurubooru\Entities\PostNote;
use Szurubooru\Entities\Snapshot; use Szurubooru\Entities\Snapshot;
use Szurubooru\Entities\Tag; use Szurubooru\Entities\Tag;
use Szurubooru\Services\PostSnapshotProvider; use Szurubooru\Services\PostSnapshotProvider;
@ -16,6 +18,7 @@ class PostSnapshotProviderTest extends AbstractTestCase
{ {
parent::setUp(); parent::setUp();
$this->globalParamDaoMock = $this->mock(GlobalParamDao::class); $this->globalParamDaoMock = $this->mock(GlobalParamDao::class);
$this->postNoteDaoMock = $this->mock(PostNoteDao::class);
} }
public function testPostChangeSnapshot() public function testPostChangeSnapshot()
@ -35,6 +38,11 @@ class PostSnapshotProviderTest extends AbstractTestCase
$post->setSource('amazing source'); $post->setSource('amazing source');
$post->setFlags(Post::FLAG_LOOP); $post->setFlags(Post::FLAG_LOOP);
$this->postNoteDaoMock
->method('findByPostId')
->with($post->getId())
->willReturn([$this->getTestPostNote()]);
$postSnapshotProvider = $this->getPostSnapshotProvider(); $postSnapshotProvider = $this->getPostSnapshotProvider();
$snapshot = $postSnapshotProvider->getPostChangeSnapshot($post); $snapshot = $postSnapshotProvider->getPostChangeSnapshot($post);
@ -46,6 +54,7 @@ class PostSnapshotProviderTest extends AbstractTestCase
'tags' => ['tag1', 'tag2'], 'tags' => ['tag1', 'tag2'],
'relations' => [1, 2], 'relations' => [1, 2],
'flags' => ['loop'], 'flags' => ['loop'],
'notes' => [['x' => 5, 'y' => 6, 'w' => 7, 'h' => 8, 'text' => 'text']],
], $snapshot->getData()); ], $snapshot->getData());
$this->assertEquals(Snapshot::TYPE_POST, $snapshot->getType()); $this->assertEquals(Snapshot::TYPE_POST, $snapshot->getType());
@ -67,6 +76,11 @@ class PostSnapshotProviderTest extends AbstractTestCase
->with(GlobalParam::KEY_FEATURED_POST) ->with(GlobalParam::KEY_FEATURED_POST)
->willReturn($param); ->willReturn($param);
$this->postNoteDaoMock
->method('findByPostId')
->with($post->getId())
->willReturn([]);
$postSnapshotProvider = $this->getPostSnapshotProvider(); $postSnapshotProvider = $this->getPostSnapshotProvider();
$snapshot = $postSnapshotProvider->getPostChangeSnapshot($post); $snapshot = $postSnapshotProvider->getPostChangeSnapshot($post);
@ -75,6 +89,17 @@ class PostSnapshotProviderTest extends AbstractTestCase
private function getPostSnapshotProvider() private function getPostSnapshotProvider()
{ {
return new PostSnapshotProvider($this->globalParamDaoMock); return new PostSnapshotProvider($this->globalParamDaoMock, $this->postNoteDaoMock);
}
private function getTestPostNote()
{
$postNote = new PostNote();
$postNote->setLeft(5);
$postNote->setTop(6);
$postNote->setWidth(7);
$postNote->setHeight(8);
$postNote->setText('text');
return $postNote;
} }
} }