php-node-client
7 лет назадphp-graphene-node-client v2.0.0 [оптимизация кода]
Начал работу над возможностью апвота но отвлекся на две важные вещи:
- Data Transfer Object CommandQueryData для комманд DataBase;
- DataBase команды вынесены в отдельный неймспейс;
Какой профит? Теперь для всех команд к стиму\голосу используется один DTO CommandQueryData, который сам разрулит какие параметры нужно передать в апи голоса а какие в апи стима. Для меня это удобно - избавило от лишних вилок в коде)
И так, встречаем!
Базовое использование
<?php
use GrapheneNodeClient\Commands\CommandQueryData;
use GrapheneNodeClient\Commands\DataBase\GetDiscussionsByCreatedCommand;
use GrapheneNodeClient\Connectors\WebSocket\GolosWSConnector;
use GrapheneNodeClient\Connectors\WebSocket\SteemitWSConnector;
//Set params for query
$commandQuery = new CommandQueryData();
$data = [
[
'limit' => $limit,
'select_tags' => ['golos'], // for GOLOS
'tag' => 'steemit', // for STEEMIT
]
];
$commandQuery->setParams($data);
//OR
$commandQuery = new CommandQueryData();
$commandQuery->setParamByKey('0:limit', $limit);
$commandQuery->setParamByKey('0:select_tags', [$tag]);
$commandQuery->setParamByKey('0:tag', $tag);
$command = new GetDiscussionsByCreatedCommand(new GolosWSConnector());
$golosPosts = $command->execute(
$commandQuery
);
// вернет
// [
// "id" => 1,
// "result" => [
// [
// "id": 466628,
// "author": "piranya",
// "permlink": "devyatyi-krug",
// ...
// ],
// ...
// ]
// ]
$command = new GetDiscussionsByCreatedCommand(new SteemitWSConnector());
$steemitPosts = $command->execute(
$commandQuery,
'result',
SteemitWSConnector::ANSWER_FORMAT_ARRAY // или SteemitWSConnector::ANSWER_FORMAT_OBJECT
);
// вернет
// [
// [
// "id": 466628,
// "author": "piranya",
// "permlink": "devyatyi-krug",
// ...
// ],
// ...
// ]
Реализованный список команд
namespace: GrapheneNodeClient\Commands\DataBase;
- GetContentCommand
- GetDiscussionsByAuthorBeforeDateCommand
- GetDiscussionsByBlogCommand
- GetDiscussionsByCreatedCommand
- GetTrendingCategoriesCommand
Реализованный список коннекторов
namespace: GrapheneNodeClient\Connectors\WebSocket;
- GolosWSConnector (wss://ws.golos.io)
- SteemitWSConnector (wss://ws.steemit.com)
переключение между коннекторами
<?php
use GrapheneNodeClient\Commands\CommandQueryData;
use GrapheneNodeClient\Commands\DataBase\GetContentCommand;
use GrapheneNodeClient\Connectors\InitConnector;
$command = new GetContentCommand(InitConnector::getConnector(InitConnector::PLATFORM_STEEMIT));
$commandQuery = new CommandQueryData();
$commandQuery->setParamByKey('0', 'author');
$commandQuery->setParamByKey('1', 'permlink');
//OR
$commandQuery = new CommandQueryData();
$commandQuery->setParams(
[
0 => "author",
1 => "permlink"
]
);
$content = $command->execute(
$commandQuery
);
// вернет
// [
// "id" => 1,
// "result" => [
// ...
// ]
// ]
Создание своего коннектора
<?php
namespace My\App\Connectors;
use GrapheneNodeClient\Connectors\ConnectorInterface;
class MyConnector implements ConnectorInterface
{
public function getPlatform() {
// TODO: Implement getPlatform() method.
}
public function doRequest(array $data, $answerFormat = self::ANSWER_FORMAT_ARRAY) {
// TODO: Implement doRequest() method.
}
}
Создание своей команды
<?php
namespace My\App\Commands;
use GrapheneNodeClient\Commands\DataBase\CommandAbstract;
use GrapheneNodeClient\Connectors\ConnectorInterface;
class MyCommand extends CommandAbstract
{
protected $method = 'method_name';
//If different for platforms
protected $queryDataMap = [
ConnectorInterface::PLATFORM_GOLOS => [
//слева - ключи массива, справа - валидаторы
//валидаторы для списка елементов задаются через '*'
'*:limit' => ['integer'], //the discussions return amount top limit
'*:select_tags:*' => ['nullOrString'], //list of tags to include, posts without these tags are filtered
'*:select_authors:*' => ['nullOrString'], //list of authors to select
'*:truncate_body' => ['nullOrInteger'], //the amount of bytes of the post body to return, 0 for all
'*:start_author' => ['nullOrString'], //the author of discussion to start searching from
'*:start_permlink' => ['nullOrString'], //the permlink of discussion to start searching from
'*:parent_author' => ['nullOrString'], //the author of parent discussion
'*:parent_permlink' => ['nullOrString'] //the permlink of parent discussion
],
ConnectorInterface::PLATFORM_STEEMIT => [
//for list params
'*:tag' => ['nullOrString'], //'author',
'*:limit' => ['integer'], //'limit'
'*:start_author' => ['nullOrString'], //'start_author' for pagination,
'*:start_permlink' => ['nullOrString'] //'start_permlink' for pagination,
]
];
//если карта запроса одинаковая для двух платформ можно сделать так
//protected $queryDataMap = [
// route example: 'key:123:array' => $_SESSION['key'][123]['array']
// 'some_array_key:some_other_key' => ['integer'], // доступные валидаторы 'required', 'array', 'string',
// 'integer', 'nullOrArray', 'nullOrString', 'nullOrInteger'.
//];
}
Инструменты
Транслитератор
<?php
use GrapheneNodeClient\Tools\Transliterator;
//Кодировать теги
$tag = Transliterator::encode('пол', Transliterator::LANG_RU); // return 'pol';
//Декодировка тегов
$tag = Transliterator::encode('ru--pol', Transliterator::LANG_RU); // return 'пол';
Код доступен на Github или packagist под MIT лицензией. Автор @t3ran13