Skip to content

Commit

Permalink
Merge pull request #77 from botuniverse/feature/ob12-action-update
Browse files Browse the repository at this point in the history
根据最新的 OneBot 12 标准文档更正 Action 对象
  • Loading branch information
crazywhalecc authored Dec 20, 2022
2 parents cedcd0b + 444061e commit f3d2905
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 15 deletions.
34 changes: 20 additions & 14 deletions src/OneBot/V12/Object/Action.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,10 @@

namespace OneBot\V12\Object;

use ReturnTypeWillChange;

/**
* OneBot 12 标准的 Action 请求对象
*/
class Action implements \JsonSerializable, \IteratorAggregate
class Action implements \JsonSerializable, \IteratorAggregate, \Stringable
{
/** @var string 动作名称 */
public string $action = '';
Expand All @@ -20,41 +18,49 @@ class Action implements \JsonSerializable, \IteratorAggregate
/** @var mixed 回包消息 */
public $echo;

public ?array $self;

/**
* 创建新的动作实例
*
* @param string $action 动作名称
* @param array $params 动作参数
* @param mixed $echo
*/
public function __construct(string $action, array $params = [], $echo = null)
public function __construct(string $action, array $params = [], $echo = null, ?array $self = null)
{
$this->action = $action;
$this->params = $params;
$this->echo = $echo;
$this->self = $self;
}

public function __toString()
{
return json_encode($this->jsonSerialize(), JSON_UNESCAPED_SLASHES);
}

/**
* 从数组创建动作实例
*/
public static function fromArray(array $arr): Action
{
return new self($arr['action'], $arr['params'] ?? [], $arr['echo'] ?? null);
return new self($arr['action'], $arr['params'] ?? [], $arr['echo'] ?? null, $arr['self'] ?? null);
}

public function jsonSerialize(): array
{
if ($this->echo === null) {
return [
'action' => $this->action,
'params' => $this->params,
];
}
return [
$d = [
'action' => $this->action,
'params' => $this->params,
'echo' => $this->echo,
];
if ($this->echo !== null) {
$d['echo'] = $this->echo;
}
if ($this->self !== null) {
$d['self'] = $this->self;
}
return $d;
}

/**
Expand All @@ -63,6 +69,6 @@ public function jsonSerialize(): array
#[\ReturnTypeWillChange]
public function getIterator(): \ArrayIterator
{
return new \ArrayIterator($this);
return new \ArrayIterator($this->jsonSerialize());
}
}
22 changes: 22 additions & 0 deletions src/OneBot/V12/Object/OneBotEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,20 @@
* @method getChannelId()
* @method getOperatorId()
* @method getMessageId()
* @method setId(string $id)
* @method setType(string $type)
* @method setSelf(array $self)
* @method setDetailType(string $detail_type)
* @method setSubType(string $sub_type)
* @method setTime(float|int $time)
* @method setAltMessage(string $alt_message)
* @method setGroupId(string $group_id)
* @method setUserId(string $user_id)
* @method setGuildId(string $guild_id)
* @method setChannelId(string $channel_id)
* @method setOperatorId(string $operator_id)
* @method setMessageId(string $message_id)
* @method setMessage(array|MessageSegment|string|\Stringable $message)
* @property string $id
* @property string $type
* @property array $self
Expand Down Expand Up @@ -53,6 +67,14 @@ public function __call(string $name, array $args = [])
}
return null;
}
if (str_starts_with($name, 'set')) {
$key = Utils::camelToSeparator(substr($name, 3));
if (isset($this->data[$key])) {
$this->data[$key] = $args[0];
return true;
}
return false;
}
throw new \BadMethodCallException('Call to undefined method ' . __CLASS__ . '::' . $name . '()');
}

Expand Down
2 changes: 1 addition & 1 deletion src/OneBot/global_defines.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
use ZM\Logger\ConsoleLogger;

const ONEBOT_VERSION = '12';
const ONEBOT_LIBOB_VERSION = '0.4.3';
const ONEBOT_LIBOB_VERSION = '0.4.4';

const ONEBOT_JSON = 1;
const ONEBOT_MSGPACK = 2;
Expand Down

0 comments on commit f3d2905

Please sign in to comment.