Added VKBot

This commit is contained in:
amikhaylov
2026-05-28 02:16:26 +03:00
parent 20450c4ede
commit e90314f18b
16 changed files with 464 additions and 5 deletions
@@ -0,0 +1,20 @@
<?php
declare(strict_types=1);
namespace App\Library\VK\Mapper;
class AttachmentMapper
{
public function map(array $attachments = []): array
{
$photos = [];
foreach ($attachments as $attachment) {
if( ($attachment['type'] === 'photo') && $attachment['photo']['sizes'] ) {
$photos[] = end($attachment['photo']['sizes'])['url'];
}
}
return $photos;
}
}
+28
View File
@@ -0,0 +1,28 @@
<?php
declare(strict_types=1);
namespace App\Library\VK\Mapper;
use App\Library\VK\Entity\VkPost;
use App\Library\VK\Mapper\Strategy\Interfaces\MappingStrategyInterface;
use App\Library\VK\Mapper\Strategy\MappingStrategyFactory;
class PostMapper
{
public function __construct(
private MappingStrategyFactory $mappingStrategyFactory,
) {
}
public function map(array $item): VkPost
{
$isRepost = isset($item['copy_history']);
/** @var MappingStrategyInterface $strategy */
$strategy = $this->mappingStrategyFactory->getStrategy($isRepost);
return $strategy->map($item);
}
}
@@ -0,0 +1,12 @@
<?php
declare(strict_types=1);
namespace App\Library\VK\Mapper\Strategy\Interfaces;
use App\Library\VK\Entity\VkPost;
interface MappingStrategyInterface
{
public function map(array $item): VkPost;
}
@@ -0,0 +1,20 @@
<?php
declare(strict_types=1);
namespace App\Library\VK\Mapper\Strategy;
use App\Library\VK\Mapper\AttachmentMapper;
use App\Library\VK\Mapper\Strategy\Interfaces\MappingStrategyInterface;
class MappingStrategyFactory
{
public function __construct(private AttachmentMapper $attachmentMapper) {}
public function getStrategy(bool $isRepost): MappingStrategyInterface
{
return $isRepost
? new RepostStrategy($this->attachmentMapper)
: new SimplePostStrategy($this->attachmentMapper);
}
}
@@ -0,0 +1,49 @@
<?php
declare(strict_types=1);
namespace App\Library\VK\Mapper\Strategy;
use App\Library\VK\Entity\VkPost;
use App\Library\VK\Mapper\AttachmentMapper;
use App\Library\VK\Mapper\Strategy\Interfaces\MappingStrategyInterface;
class RepostStrategy implements MappingStrategyInterface
{
public function __construct(private AttachmentMapper $attachmentMapper) {}
public function map(array $item): VkPost
{
$post = new VkPost();
$post->setId($item['id']);
$post->setOwnerId($item['owner_id']);
$post->setAuthorId($item['from_id']);
$post->setDate($item['date']);
$post->setIsRepost();
$post->setText($item['text'] ?? '');
// Переключаемся на оригинал для контента
$original = $this->getOriginalPost($item);
$post->setText($original['text'] ?? '');
// Вложения берем именно из оригинала
if (! empty($original['attachments'])) {
$attachments = $this->attachmentMapper->map($original['attachments']);
$post->setAttachments($attachments);
}
return $post;
}
private function getOriginalPost(array $item): array
{
$copy_history_length = count($item['copy_history']);
if($copy_history_length > 0) {
return end($item['copy_history']);
}
return $item;
}
}
@@ -0,0 +1,32 @@
<?php
declare(strict_types=1);
namespace App\Library\VK\Mapper\Strategy;
use App\Library\VK\Entity\VkPost;
use App\Library\VK\Mapper\AttachmentMapper;
use App\Library\VK\Mapper\Strategy\Interfaces\MappingStrategyInterface;
class SimplePostStrategy implements MappingStrategyInterface
{
public function __construct(private AttachmentMapper $attachmentMapper) {}
public function map(array $item): VkPost
{
$post = new VkPost();
$post->setId($item['id']);
$post->setOwnerId($item['owner_id']);
$post->setAuthorId($item['from_id']);
$post->setDate($item['date']);
$post->setIsPost();
$post->setText($item['text'] ?? '');
if (! empty($item['attachments'])) {
$attachments = $this->attachmentMapper->map($item['attachments']);
$post->setAttachments($attachments);
}
return $post;
}
}