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 edit snapshots (backed-only)
- post notes
- post notes history
refactors:
- add enum validation in IValidatables (needs refactors of enums and
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->setPostId($array['postId']);
$entity->setLeft($array['x']);
$entity->setTop($array['y']);
$entity->setWidth($array['width']);
$entity->setHeight($array['height']);
$entity->setLeft(intval($array['x']));
$entity->setTop(intval($array['y']));
$entity->setWidth(intval($array['width']));
$entity->setHeight(intval($array['height']));
$entity->setText($array['text']);
return $entity;
}

View file

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

View file

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