vk/hood/vk/system/robots/robot.php

282 lines
8.8 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
declare(strict_types=1);
namespace hood\vk\robots;
use Exception;
use GuzzleHttp\Client as browser;
use hood\vk\core;
use hood\vk\proxies\proxy;
use hood\vk\captcha\captcha;
use hood\vk\api\settings as api;
use hood\vk\api\methods\method;
use hood\accounts\vk as account;
/**
* Робот
*
* @var int $id Идентификатор
* @var int $session Сессия
* @var string $key Ключ
* @var api $api API ВКонтакте
* @var account $account Аккаунт
* @var browser $browser Браузер
* @var proxy $proxy Прокси
* @var captcha $captcha Обработчик капчи
*
* @var int $messages_mode Режим отправки сообщений
*
* @method public function __construct(int $id = null) Конструктор
* @method public function key(string $key) Инициализация ключа
* @method public function account(account $account) Инициализация аккаунта
* @method public function __set($name, $value) Запись свойства
* @method public function __get($name) Чтение свойства
* @method public function __isset($name) Проверка на инициализированность свойства
* @method public function __call(string $method, array $params) Вызов метода
* @method public static function __callStatic(string $method, array $params) Вызов статического метода
* @method public function __toString() Конвертация в строку
*
* @package hood\vk\robots
* @author Arsen Mirzaev Tatyano-Muradovich <red@hood.su>
*/
abstract class robot
{
/**
* @var int Сессия
*/
protected int $session;
/**
* @var string Аккаунт
*/
private account $account;
/**
* @var proxy Прокси
*/
protected proxy $proxy;
/**
* @var captcha Обработчик капчи
*/
protected captcha $captcha;
/**
* @var int Режим отправки сообщений
*/
protected int $messages_mode = 1;
/**
* @var api API ВКонтакте
*/
protected api $api;
/**
* Конструктор
*
* @param int|null $id Идентификатор
* @param string|null $key Ключ
*/
public function __construct(
protected int|null $id = null,
protected string|null $key = null
) {
// Инициализация ядра
$core = core::init();
// Идентификация робота
$this->id = $id ?? $core->robots + 1;
// Регистрация робота в ядре
$core->write($this->id, $this);
// Идентификация сессии робота
$this->session = count($core->read($this->id));
}
/**
* Инициализация ключа
*
* @param string $key Ключ
*
* @return self
*/
public function key(string $key): self
{
$this->__set('key', $key);
return $this;
}
/**
* Инициализация аккаунта
*
* @param account $account Аккаунт
*
* @return self
*/
public function account(account $account): self
{
$this->__set('account', $account);
return $this;
}
/**
* Инициализация прокси
*
* @param proxy $proxy Прокси
*
* @return self
*/
public function proxy(proxy $proxy): self
{
$this->__set('proxy', $proxy);
return $this;
}
/**
* Инициализация обработчика капчи
*
* @param captcha $captcha Обработчик капчи
*
* @return self
*/
public function captcha(captcha $captcha): self
{
$this->__set('captcha', $captcha);
return $this;
}
/**
* Записать свойство
*
* @param string $name Название
* @param mixed $value Значение
*
* @return void
*/
public function __set(string $name, mixed $value): void
{
match ($name) {
'id' => isset($this->id) ? throw new Exception('Запрещено перезаписывать идентификатор', 500) : $this->id = (int) $value,
'session' => isset($this->session) ? throw new Exception('Запрещено перезаписывать сессию', 500) : $this->session = (int) $value,
'key' => isset($this->key) ? throw new Exception('Запрещено перезаписывать ключ', 500) : $this->key = (string) $value,
'api' => isset($this->api) ? throw new Exception('Запрещено перезаписывать API', 500) : $this->api = $value,
'account' => isset($this->account) ? throw new Exception('Запрещено перезаписывать аккаунт', 500) : $this->account = $value,
'browser' => isset($this->browser) ? throw new Exception('Запрещено перезаписывать браузер', 500) : $this->browser = $value,
'proxy' => $this->proxy = $value,
'captcha' => $this->captcha = $value,
'messages_new' => $this->messages_new = (int) $value,
default => throw new Exception("Свойство $name не найдено", 404)
};
}
/**
* Прочитать свойство
*
* @param string $name Название
*
* @return mixed
*/
public function __get(string $name): mixed
{
return match ($name) {
'id' => $this->id ?? throw new Exception('Идентификатор не инициализирован', 500),
'session' => $this->session ?? throw new Exception('Сессия не инициализирована', 500),
'key' => $this->key ?? throw new Exception('Ключ не инициализирован', 500),
'api' => $this->api ?? $this->api = new api($this),
'account' => $this->account ?? throw new Exception('Аккаунт не инициализирован', 500),
'browser' => $this->browser ?? $this->browser = new browser([
'base_uri' => 'https://api.vk.com/method/',
'cookies' => true
]),
'proxy' => $this->proxy,
'captcha' => $this->captcha,
'messages_new' => $this->messages_new,
default => throw new Exception("Свойство $name не найдено", 404)
};
}
/**
* Проверить свойство на инициализированность
*
* @param string $name Название
*
* @return mixed
*/
public function __isset(string $name): bool
{
return match ($name) {
'id' => isset($this->id),
'session' => isset($this->session),
'key' => isset($this->key),
'account' => isset($this->account),
'api' => isset($this->api),
'browser' => isset($this->browser),
'proxy' => isset($this->proxy),
'captcha' => isset($this->captcha),
'messages_new' => isset($this->messages_new),
default => throw new Exception("Свойство $name не найдено", 404)
};
}
/**
* Вызвать метод
*
* Ищет класс описывающий метод API ВКонтакте,
* создаёт и возвращает его объект
*
* @param string $method Метод
* @param array $params Параметры
*
* @return method
*/
public function __call(string $method, array $params): method
{
if (class_exists($class = '\\hood\\vk\\api\\methods\\' . $method . 's')) {
// Если найден класс реализующий запрошенный метод
return new $class($this, ...$params);
}
throw new Exception("Метод $method не найден", 404);
}
/**
* Вызвать статический метод
*
* Ищет класс описывающий метод API ВКонтакте,
* создаёт и возвращает его объект
*
* @param string $method Метод
* @param array $params Параметры
*
* @return method
*/
public static function __callStatic(string $method, array $params): method
{
if (class_exists($class = '\\hood\\vk\\api\\methods\\' . $method . 's')) {
return $class(self, ...$params);
}
throw new Exception("Метод $method не найден", 404);
}
/**
* Конвертировать в строку
*
* @return string
*/
public function __toString(): string
{
return (string) $this->id;
}
}