DiscordPHP-BridgeBot Documentation

Arguments
in package

FinalYes

The arguments to one command invocation, parsed the same way on both surfaces.

The two clients hand over tokens that were split by different rules — DiscordPHP runs the line through str_getcsv, TwitchPHP splits on runs of whitespace — so tokens are re-joined and re-parsed here. Without that, !title "on the road" would mean two different things depending on where it was typed, which is exactly the kind of difference this project exists to avoid.

Supports positional arguments and key=value flags, either of which may be quoted:

!api channels.modify title="Back soon" game_id=509658
     └─ positional ─┘ └────── named ──────────────────┘
Tags
author

Valithor Obsidion valithor@discordphp.org

Table of Contents

Properties

$named  : array<string|int, mixed>
$positional  : array<string|int, mixed>
$raw  : string|null

Methods

all()  : array<int, string>
allNamed()  : array<string, string>
count()  : int
fromParts()  : self
Builds from values that were already separated — a slash command's options, which Discord has parsed and typed for us.
fromString()  : self
fromTokens()  : self
Builds from whatever tokens a client handed over.
get()  : string|null
The nth positional argument, or `$default`.
has()  : bool
isEmpty()  : bool
named()  : string|null
A `key=value` flag, or `$default`.
rest()  : string
Everything from `$index` onwards as one string, quotes and all.
__construct()  : mixed
tokenize()  : array<int, string>
Splits on whitespace, keeping quoted runs together.
unquote()  : string

Properties

$named read-only

private array<string|int, mixed> $named

$positional read-only

private array<string|int, mixed> $positional

Methods

all()

public all() : array<int, string>
Return values
array<int, string>

allNamed()

public allNamed() : array<string, string>
Return values
array<string, string>

count()

public count() : int
Return values
int

fromParts()

Builds from values that were already separated — a slash command's options, which Discord has parsed and typed for us.

public static fromParts(array<int, string> $positional[, array<string, string> $named = [] ]) : self

Deliberately not routed through fromString(): a value containing a space would be re-split by the tokenizer, and one containing a quote would be mangled. The values are taken as given.

Each option is recorded both positionally, in declaration order, and by name — so a handler written for !relay link twitchdev reading get(1) and one reading named('twitch') both work against the same invocation.

Parameters
$positional : array<int, string>
$named : array<string, string> = []
Return values
self

fromString()

public static fromString(string $line) : self
Parameters
$line : string
Return values
self

fromTokens()

Builds from whatever tokens a client handed over.

public static fromTokens(array<int, string> $tokens) : self
Parameters
$tokens : array<int, string>
Return values
self

get()

The nth positional argument, or `$default`.

public get(int $index[, string|null $default = null ]) : string|null
Parameters
$index : int
$default : string|null = null
Return values
string|null

has()

public has(string $key) : bool
Parameters
$key : string
Return values
bool

isEmpty()

public isEmpty() : bool
Return values
bool

named()

A `key=value` flag, or `$default`.

public named(string $key[, string|null $default = null ]) : string|null
Parameters
$key : string
$default : string|null = null
Return values
string|null

rest()

Everything from `$index` onwards as one string, quotes and all.

public rest([int $index = 0 ]) : string

This is what free-text actions want: !title takes a sentence, not a list of words, and re-joining the tokens would drop the user's own spacing and quotes.

When the arguments came from a slash command there is no line to slice — the values arrived already separated and typed — so the positional values are joined instead. Splitting and re-joining those would corrupt any value containing a space, which for /title text:Back in ten is the whole point of it.

Parameters
$index : int = 0
Return values
string

__construct()

private __construct(array<int, string> $positional, array<string, string> $named, string|null $raw) : mixed
Parameters
$positional : array<int, string>
$named : array<string, string>
$raw : string|null

The line as typed, or null when the values did not come from one.

tokenize()

Splits on whitespace, keeping quoted runs together.

private static tokenize(string $line) : array<int, string>

A token is any run of unquoted non-space characters and quoted segments, in any order — which is what makes title="on the road" one token rather than title="on and road". A simpler "..."|\S+ alternation gets that wrong, because \S+ matches from the start of the word and stops at the first space, having already swallowed the opening quote.

Quotes are kept in the token at this stage so rest() can hand back what was actually typed; unquote() strips them when a single value is read out.

Parameters
$line : string
Return values
array<int, string>

unquote()

private static unquote(string $value) : string
Parameters
$value : string
Return values
string
On this page

Search results