Skip to content

Commit 113d077

Browse files
committed
Добавлены форматтеры для входящих команд, исправлено деление команды и полезной нагрузки для входящего сообщения
1 parent b79975f commit 113d077

File tree

7 files changed

+102
-6
lines changed

7 files changed

+102
-6
lines changed

src/.root/lang/ru/vk-sdk.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,4 +178,5 @@
178178
'Invalid SourceQuery packet' => 'Недопустимый пакет SourceQuery',
179179
'Unknown server' => 'Неизвестный сервер',
180180
'Undefined server response' => 'Неопределенный ответ сервера',
181+
'Invalid input data type' => 'Недопустимый тип входных данных',
181182
];

src/.root/routes/api.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
use Hopex\VkSdk\Services\ServerEventsService;
66
use Illuminate\Support\Facades\Route;
77

8-
Route::prefix('api')->name('vk-sdk.')->group(function () {
8+
Route::prefix('api')->name('vk.')->group(function () {
99

1010
Route::post(SdkConfig::routes('group'), function (CallbackEventsService $callback) {
1111
return response($callback->divide(), 200, [
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
3+
namespace Hopex\VkSdk\Exceptions\Formatters;
4+
5+
use Hopex\VkSdk\Exceptions\SdkException;
6+
7+
class InvalidInputDataTypeException extends SdkException
8+
{
9+
public $message = 'Invalid input data type';
10+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
3+
namespace Hopex\VkSdk\Formatters;
4+
5+
use Hopex\VkSdk\Contracts\CanFormatContract;
6+
use Hopex\VkSdk\Exceptions\Formatters\InvalidInputDataTypeException;
7+
8+
/**
9+
* Class ClearMentionsFormatter
10+
* @package Hopex\VkSdk\Formatters
11+
*/
12+
class ClearMentionsFormatter implements CanFormatContract
13+
{
14+
/**
15+
* @param $data
16+
* @return string
17+
* @throws InvalidInputDataTypeException
18+
*/
19+
public function format($data): string
20+
{
21+
if (!is_string($data)) {
22+
throw new InvalidInputDataTypeException();
23+
}
24+
25+
return preg_replace("~^(\[.*]\s)|(!)|(.*)~", '$3', $data);
26+
}
27+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
3+
namespace Hopex\VkSdk\Formatters;
4+
5+
use Hopex\VkSdk\Contracts\CanFormatContract;
6+
use Hopex\VkSdk\Exceptions\Formatters\InvalidInputDataTypeException;
7+
8+
/**
9+
* Class ClearSpacesFormatter
10+
* @package Hopex\VkSdk\Formatters
11+
*/
12+
class ClearSpacesFormatter implements CanFormatContract
13+
{
14+
/**
15+
* @param $data
16+
* @return string
17+
* @throws InvalidInputDataTypeException
18+
*/
19+
public function format($data): string
20+
{
21+
if (!is_string($data)) {
22+
throw new InvalidInputDataTypeException();
23+
}
24+
25+
return trim(preg_replace('~\s{2,}~', ' ', $data));
26+
}
27+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
3+
namespace Hopex\VkSdk\Formatters;
4+
5+
use Hopex\VkSdk\Contracts\CanFormatContract;
6+
use Hopex\VkSdk\Exceptions\Formatters\InvalidInputDataTypeException;
7+
8+
/**
9+
* Class SliceInputMessageFormatter
10+
* @package Hopex\VkSdk\Formatters
11+
*/
12+
class SliceInputMessageFormatter implements CanFormatContract
13+
{
14+
/**
15+
* @param $data
16+
* @return string
17+
* @throws InvalidInputDataTypeException
18+
*/
19+
public function format($data): array
20+
{
21+
if (!is_string($data)) {
22+
throw new InvalidInputDataTypeException();
23+
}
24+
25+
$exploded = explode(' ', $data);
26+
return [
27+
'command' => $exploded[0],
28+
'payload' => implode(' ', array_slice($exploded, 1, count($exploded)))
29+
];
30+
}
31+
}

src/Foundation/Core/Entities/Messages/MessageFields.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
use Carbon\Carbon;
66
use Hopex\VkSdk\Exceptions\Api\ApiException;
7-
use Hopex\VkSdk\Facades\VkApi;
87
use Hopex\VkSdk\Foundation\Core\Entities\Users\UserProfileFields;
98
use Hopex\VkSdk\Foundation\Core\Entities\Users\UserRequestFields;
109
use Illuminate\Support\Collection;
@@ -53,19 +52,20 @@ public function getDate(): Carbon
5352

5453
/**
5554
* @param string $token
56-
* @param UserRequestFields $userRequestFields
55+
* @param array $userProfileFields
5756
* @return UserProfileFields
57+
* @throws ApiException
58+
* @throws Throwable
5859
*/
5960
public function getSender(string $token, UserRequestFields $userRequestFields): UserProfileFields
6061
{
61-
// return new UserProfileFields([]);
62-
return new UserProfileFields(collect(VkApi::user($token)->get($userRequestFields)));
62+
return new UserProfileFields([]);
6363
// return new UserProfileFields(collect(VkApi::user($token)
6464
// ->get((new UserRequestFields())
6565
// ->setUserIds([
6666
// $this->getSenderId()
6767
// ])
68-
// ->setProfileFields($userRequestFields->fields
68+
// ->setProfileFields($userRequestFields->)
6969
// )->first()));
7070
}
7171

0 commit comments

Comments
 (0)