This commit is contained in:
amikhaylov
2026-05-28 12:24:30 +03:00
parent 8f3cc468b9
commit 222e8984fc
5 changed files with 73 additions and 27 deletions
+19 -7
View File
@@ -9,9 +9,10 @@ use Carbon\Carbon;
class VkPost
{
private int $id;
private string $source_name;
private bool $post = true;
private string $text;
private int $author_id;
private int $from_id;
private int $owner_id;
private array $attachments = [];
private Carbon $date;
@@ -20,11 +21,12 @@ class VkPost
{
return [
'id' => $this->id,
'post' => (int) $this->post,
'post' => $this->post ? "post" : "copy",
'text' => $this->text,
'author_id' => $this->author_id,
'from_id' => $this->from_id,
'owner_id' => $this->owner_id,
'date' => $this->getDate()->toIso8601String(),
'name' => $this->getSourceName(),
'attachments' => $this->attachments,
];
}
@@ -64,9 +66,9 @@ class VkPost
return $this->text;
}
public function getAuthorId(): int
public function getFromId(): int
{
return $this->author_id;
return $this->from_id;
}
public function getDate(): Carbon
@@ -82,6 +84,11 @@ class VkPost
return $this->owner_id;
}
public function getSourceName(): string
{
return $this->source_name;
}
public function setId(int $id): void
{
$this->id = $id;
@@ -92,9 +99,9 @@ class VkPost
$this->text = $text;
}
public function setAuthorId(int $author_id): void
public function setFromId(int $from_id): void
{
$this->author_id = $author_id;
$this->from_id = $from_id;
}
public function setDate(string|int $date): void
@@ -111,4 +118,9 @@ class VkPost
{
$this->attachments = $attachments;
}
public function setSourceName(string $source_name): void
{
$this->source_name = $source_name;
}
}
@@ -17,7 +17,7 @@ class RepostStrategy implements MappingStrategyInterface
$post = new VkPost();
$post->setId($item['id']);
$post->setOwnerId($item['owner_id']);
$post->setAuthorId($item['from_id']);
$post->setFromId($item['from_id']);
$post->setDate($item['date']);
$post->setIsRepost();
$post->setText($item['text'] ?? '');
@@ -17,7 +17,7 @@ class SimplePostStrategy implements MappingStrategyInterface
$post = new VkPost();
$post->setId($item['id']);
$post->setOwnerId($item['owner_id']);
$post->setAuthorId($item['from_id']);
$post->setFromId($item['from_id']);
$post->setDate($item['date']);
$post->setIsPost();
$post->setText($item['text'] ?? '');
@@ -0,0 +1,21 @@
<?php
declare(strict_types=1);
namespace App\Library\VK\Resources;
use App\Library\VK\VkApiClient;
class GroupResource
{
public function __construct(private VkApiClient $api) {}
public function getById(string $groupId): array
{
$result = $this->api->call('groups.getById', [
'group_id' => $groupId,
]);
return $result['response'] ?? [];
}
}
+29 -16
View File
@@ -4,14 +4,18 @@ declare(strict_types=1);
namespace App\Library\VK\Service;
use App\Library\VK\Entity\VkPost;
use App\Library\VK\Mapper\PostMapper;
use App\Library\VK\Resources\GroupResource;
use App\Library\VK\Resources\WallResource;
use Illuminate\Support\Facades\Log;
use Generator;
class VkPostImportService
{
public function __construct(
private WallResource $resource,
private WallResource $wall,
private GroupResource $group,
private PostMapper $mapper
) {
@@ -19,29 +23,38 @@ class VkPostImportService
public function run(): void
{
//$result = $this->resource->get('ledstarband', 1, 100);
$group = $this->getGroupInfo('ledstarband');
$posts = $this->getWallPosts('ledstarband', 10);
foreach($posts as $item) {
$post = $this->mapper->map($item);
$post->setSourceName($group['name']);
if(! $post->isEmpty()) {
print_r($post->toArray());
}
}
}
/** @return Generator<int, array> */
private function getWallPosts(string $id, int $count = 5): Generator
{
$result = $this->wall->get($id, $count);
if (empty ($result['items']) ) {
Log::info('Post import failed: no items found');
}
# echo "\nFound items: ".$result['count']."\n";
// foreach ($result['items'] as $item) {
// $post = $this->mapper->map($item);
// if(! $post->isEmpty()) {
// # print_r($post->toArray());
// }
// }
$res = $this->resource->getById('-93243530_2113');
foreach ($res as $item) {
$post = $this->mapper->map($item);
if(! $post->isEmpty()) {
print_r($post->toArray());
foreach ($result['items'] as $item) {
yield $item;
}
}
# print_r($res);
private function getGroupInfo(string $groupId): ?array
{
$res = $this->group->getById($groupId);
return $res[0] ?? null;
}
}