Files
2026-05-28 03:05:06 +03:00

46 lines
1.2 KiB
PHP

<?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 $this->fixLinks($strategy->map($item));
}
private function fixLinks(VkPost $vkPost): VkPost
{
$text = $vkPost->getText();
if (empty($text)) return $vkPost;
$newText = preg_replace_callback("#\[([^|]+)\|([^\]]+)\]#u", function ($matches) {
$alias = $matches[1]; // то, что после vk.com/
$label = $matches[2]; // текст ссылки
return "<a href=\"https://vk.com/{$alias}\" target=\"_blank\">{$label}</a>";
}, $text);
$vkPost->setText($newText);
return $vkPost;
}
}