DiscordPHP-MTG Documentation

CardQuery extends Query
in package

A card search against the AllPrintings build's `cards` table, joined to `sets` (for `setName`), `cardIdentifiers` and `cardLegalities` as filters need them. One row per card face per printing, as in MTGJSON.

Every cards column is a filter, matched by its kind: text columns (name, type, text, flavorText, artist, …) partially, list columns (colors, colorIdentity, types, subtypes, keywords, …) by membership, numeric columns (manaValue, edhrecRank, …) with comparisons, is*/has* columns as booleans, and the rest exactly. Every cardIdentifiers column (multiverseId, scryfallId, mtgArenaId, …) matches exactly. Besides those:

  • setName, block — partial match on the printing's set.
  • gameFormat — legal in a format (commander, Pauper Commander, …); legality (Legal, Banned, Restricted, Not Legal) changes the status, and on its own matches any format.
  • language — the printing's language; with name, a non-English language searches that language's printed names instead.
  • contains — fields that must hold a value (flavorText,power), plus rulings and foreignData.
  • orderBy (a column, -name or name desc), random, page, pageSize (1-100, default 1).

The parameter names of the old api.magicthegathering.io (cmc, set, flavor, id, multiverseid, imageUrl, …) are accepted as aliases. Colors may be given as letters (W, U, B, R, G, C for colorless), runs of letters (UR) or names.

Without orderBy or random, exact name matches come first, then shorter names, then the most regular printing: English, from a regular set, not a promo, paper, newest first.

Tags
link

Card (Set) model

since
1.0.0

Table of Contents

Constants

ALIASES  : array<string, string> = ['cmc' => 'manaValue', 'convertedManaCost' => '...
Old api.magicthegathering.io parameter names, mapped to MTGJSON's.
COLOR_COLUMNS  : array<string|int, string> = ['colors', 'colorIdentity', 'colorIndicator', '...
List columns holding colors, which accept color names.
COLORS  : array<string, string> = ['w' => 'W', 'white' => 'W', 'u' => 'U', 'blue'...
Color names and letters, mapped to MTGJSON's letters; colorless maps to an empty list.
MAX_PAGE_SIZE  : int = 100
The largest page a query may return.
PARTIAL  : array<string|int, string> = ['artist', 'asciiName', 'faceFlavorName', 'face...
Text columns matched partially rather than exactly.
REGULAR_SET_TYPES  : array<string|int, string> = ['core', 'expansion', 'masters', 'draft_innovat...
Set types whose printings are preferred when results are not ordered explicitly.

Properties

$bindings  : array<string|int, mixed>
Values bound to the conditions' placeholders, in order.
$database  : Database
$joins  : array<string, string>
Joined tables, by table name, as SQL fragments.
$limit  : int|null
Page size, or null for no limit.
$names  : array<string|int, string>
The name alternatives searched for, used to rank exact matches first.
$offset  : int
Rows to skip.
$order  : array<string|int, string>
`ORDER BY` terms.
$orderBindings  : array<string|int, mixed>
Values bound to the ordering's placeholders, in order.
$table  : string
The primary table.
$where  : array<string|int, string>
Conditions, combined with `AND`.

Methods

__construct()  : mixed
count()  : int
How many rows match, ignoring the page.
filter()  : static
Applies a set of filters and query modifiers.
get()  : array<string|int, array<string|int, mixed>>
Runs the compiled statement and decodes each row.
getBindings()  : array<string|int, mixed>
The values bound to the compiled statement's placeholders.
toSql()  : string
The compiled statement.
alternatives()  : array<string|int, array<string|int, string>>
Splits a filter value into alternatives (`|`), each a list of terms that must all hold (`,`) when `$and` is set.
colorRuns()  : mixed
Splits runs of color letters (`UR`, `wubrg`) into separate required terms (`U,R`), leaving color names alone.
column()  : array{0: ?string, 1: array}
The condition for one `cards` column, by its kind.
combine()  : array{0: ?string, 1: array}
Combines conditions with `OR` (or `AND`).
compare()  : array{0: ?string, 1: array}
Numeric match, with an optional comparison prefix per alternative.
contains()  : array{0: ?string, 1: array}
Matches a `", "`-joined list column: every term of an alternative must be in the list.
direction()  : array{0: string, 1: string}
Parses an `orderBy` value: a field, optionally prefixed with `-` or suffixed with ` desc`/` asc`.
equals()  : array{0: ?string, 1: array}
Exact, case-insensitive match.
exact()  : array{0: ?string, 1: array}
Exact match on a column stored in one case (set codes, UUIDs), so its index still applies.
flag()  : array{0: string, 1: array}
Matches a `BOOLEAN` column, where the build stores false as `0` or null.
has()  : void
Requires fields to hold a value.
identifier()  : string
Joins `cardIdentifiers` and qualifies one of its columns.
isEnglish()  : bool
Whether a language filter asks only for English.
legality()  : void
Filters on format legality.
like()  : array{0: ?string, 1: array}
Partial, case-insensitive text match against one or more columns; a value in double quotes matches exactly.
nameAndLanguage()  : void
Filters on name and the printing's language. With a non-English language, the name is searched among that language's printed names.
normalize()  : array<string|int, mixed>
Normalizes filter keys to camelCase (`color_identity` → `colorIdentity`) and drops empty values.
orderBy()  : void
Adds an `ORDER BY` term.
paginate()  : void
Applies `page` and `pageSize`.
placeholders()  : string
`?, ?, ?` for a list of values.
present()  : array{0: string, 1: array}
Requires a column to hold a value.
printingOrder()  : array<string|int, string>
The most regular printing first: English, from a regular set, not a promo, paper, not funny, newest, front face. No placeholders.
rank()  : void
The default ranking: exact name matches, shorter names, then the most regular printing, newest first.
scalar()  : string
Converts a scalar filter value to a string.
select()  : string
The select list.
sort()  : void
Orders by an explicit field.
terms()  : array<string|int, string>
The first term of each alternative, for single-valued fields.
truthy()  : bool
Reads a boolean-ish filter value.
unique()  : void
Keeps one row per card name — its most regular printing (see {@see printingOrder()}) among the rows the other filters match — so a search lists cards rather than every printing and face of them.
where()  : void
Adds a condition.

Constants

ALIASES

Old api.magicthegathering.io parameter names, mapped to MTGJSON's.

public array<string, string> ALIASES = ['cmc' => 'manaValue', 'convertedManaCost' => 'manaValue', 'set' => 'setCode', 'flavor' => 'flavorText', 'id' => 'uuid', 'multiverseid' => 'multiverseId', 'border' => 'borderColor', 'reserved' => 'isReserved', 'timeshifted' => 'isTimeshifted', 'imageUrl' => 'scryfallId', 'foreignNames' => 'foreignData']

COLOR_COLUMNS

List columns holding colors, which accept color names.

public array<string|int, string> COLOR_COLUMNS = ['colors', 'colorIdentity', 'colorIndicator', 'producedMana']

COLORS

Color names and letters, mapped to MTGJSON's letters; colorless maps to an empty list.

public array<string, string> COLORS = ['w' => 'W', 'white' => 'W', 'u' => 'U', 'blue' => 'U', 'b' => 'B', 'black' => 'B', 'r' => 'R', 'red' => 'R', 'g' => 'G', 'green' => 'G', 'c' => '', 'colorless' => '']

MAX_PAGE_SIZE

The largest page a query may return.

public int MAX_PAGE_SIZE = 100

PARTIAL

Text columns matched partially rather than exactly.

public array<string|int, string> PARTIAL = ['artist', 'asciiName', 'faceFlavorName', 'faceName', 'facePrintedName', 'flavorName', 'flavorText', 'originalText', 'originalType', 'printedName', 'printedText', 'printedType', 'text', 'type']

REGULAR_SET_TYPES

Set types whose printings are preferred when results are not ordered explicitly.

public array<string|int, string> REGULAR_SET_TYPES = ['core', 'expansion', 'masters', 'draft_innovation', 'commander', 'starter', 'duel_deck', 'planechase', 'archenemy', 'from_the_vault', 'spellbook', 'premium_deck', 'arsenal', 'box', 'eternal']

Properties

$bindings

Values bound to the conditions' placeholders, in order.

protected array<string|int, mixed> $bindings = []

$joins

Joined tables, by table name, as SQL fragments.

protected array<string, string> $joins = []

$limit

Page size, or null for no limit.

protected int|null $limit = null

$names

The name alternatives searched for, used to rank exact matches first.

protected array<string|int, string> $names = []

$offset

Rows to skip.

protected int $offset = 0

$order

`ORDER BY` terms.

protected array<string|int, string> $order = []

$orderBindings

Values bound to the ordering's placeholders, in order.

protected array<string|int, mixed> $orderBindings = []

$table

The primary table.

protected string $table = 'cards'
Tags
inheritDoc

$where

Conditions, combined with `AND`.

protected array<string|int, string> $where = []

Methods

__construct()

public __construct(Database $database) : mixed
Parameters
$database : Database

An open build; its schema decides which filters exist.

count()

How many rows match, ignoring the page.

public count() : int
Return values
int

filter()

Applies a set of filters and query modifiers.

public filter(array<string|int, mixed> $filters) : static
Parameters
$filters : array<string|int, mixed>

key => value; keys may be snake_case or camelCase.

Tags
inheritDoc
Return values
static

get()

Runs the compiled statement and decodes each row.

public get() : array<string|int, array<string|int, mixed>>
Return values
array<string|int, array<string|int, mixed>>

getBindings()

The values bound to the compiled statement's placeholders.

public getBindings() : array<string|int, mixed>
Return values
array<string|int, mixed>

toSql()

The compiled statement.

public toSql() : string
Return values
string

alternatives()

Splits a filter value into alternatives (`|`), each a list of terms that must all hold (`,`) when `$and` is set.

protected static alternatives(mixed $value[, bool $and = false ]) : array<string|int, array<string|int, string>>
Parameters
$value : mixed

The filter value; an array is a list of alternatives.

$and : bool = false

Whether , separates required terms.

Return values
array<string|int, array<string|int, string>>

colorRuns()

Splits runs of color letters (`UR`, `wubrg`) into separate required terms (`U,R`), leaving color names alone.

protected static colorRuns(mixed $value) : mixed
Parameters
$value : mixed

column()

The condition for one `cards` column, by its kind.

protected static column(string $column, string $type, mixed $value) : array{0: ?string, 1: array}
Parameters
$column : string

The column.

$type : string

Its declared type.

$value : mixed
Return values
array{0: ?string, 1: array}

combine()

Combines conditions with `OR` (or `AND`).

protected static combine(array<string|int, array{0: ?string, 1: array}> $conditions[, string $operator = 'OR' ]) : array{0: ?string, 1: array}
Parameters
$conditions : array<string|int, array{0: ?string, 1: array}>
$operator : string = 'OR'

OR or AND.

Return values
array{0: ?string, 1: array}

compare()

Numeric match, with an optional comparison prefix per alternative.

protected static compare(string $column, mixed $value) : array{0: ?string, 1: array}
Parameters
$column : string

Qualified column name.

$value : mixed

E.g. 3, gte3, >=3, 2|5.

Tags
throws
InvalidArgumentException

When a term is not a number.

Return values
array{0: ?string, 1: array}

contains()

Matches a `", "`-joined list column: every term of an alternative must be in the list.

protected static contains(string $column, mixed $value[, callable|null $map = null ]) : array{0: ?string, 1: array}
Parameters
$column : string

Qualified column name.

$value : mixed
$map : callable|null = null

Maps each term before matching; returning '' matches an empty list.

Return values
array{0: ?string, 1: array}

direction()

Parses an `orderBy` value: a field, optionally prefixed with `-` or suffixed with ` desc`/` asc`.

protected static direction(string $value) : array{0: string, 1: string}
Parameters
$value : string
Return values
array{0: string, 1: string} —

The field and ASC/DESC.

equals()

Exact, case-insensitive match.

protected static equals(string $column, mixed $value) : array{0: ?string, 1: array}
Parameters
$column : string

Qualified column name.

$value : mixed
Return values
array{0: ?string, 1: array}

exact()

Exact match on a column stored in one case (set codes, UUIDs), so its index still applies.

protected static exact(string $column, mixed $value, callable $normalize) : array{0: ?string, 1: array}
Parameters
$column : string

Qualified column name.

$value : mixed
$normalize : callable

Puts each term in the stored case, e.g. strtoupper.

Return values
array{0: ?string, 1: array}

flag()

Matches a `BOOLEAN` column, where the build stores false as `0` or null.

protected static flag(string $column, mixed $value) : array{0: string, 1: array}
Parameters
$column : string

Qualified column name.

$value : mixed

true/false, 1/0, yes/no.

Return values
array{0: string, 1: array}

has()

Requires fields to hold a value.

protected has(mixed $value, array<string|int, mixed> $cards, array<string|int, mixed> $identifiers) : void
Parameters
$value : mixed

Field names, ,-separated.

$cards : array<string|int, mixed>

The cards columns.

$identifiers : array<string|int, mixed>

The cardIdentifiers columns.

Tags
throws
InvalidArgumentException

On an unknown field.

identifier()

Joins `cardIdentifiers` and qualifies one of its columns.

protected identifier(string $column) : string
Parameters
$column : string

The column.

Return values
string —

The qualified column.

isEnglish()

Whether a language filter asks only for English.

protected static isEnglish(mixed $language) : bool
Parameters
$language : mixed
Return values
bool

legality()

Filters on format legality.

protected legality(mixed $format, mixed $legality) : void
Parameters
$format : mixed

A format name (commander, Pauper Commander, …), or |-separated names.

$legality : mixed

Legal (default), Banned, Restricted or Not Legal.

Tags
throws
InvalidArgumentException

On an unknown format.

like()

Partial, case-insensitive text match against one or more columns; a value in double quotes matches exactly.

protected static like(array<string|int, string> $columns, mixed $value) : array{0: ?string, 1: array}
Parameters
$columns : array<string|int, string>

Qualified column names; a match on any counts.

$value : mixed
Return values
array{0: ?string, 1: array}

nameAndLanguage()

Filters on name and the printing's language. With a non-English language, the name is searched among that language's printed names.

protected nameAndLanguage(mixed $name, mixed $language) : void
Parameters
$name : mixed
$language : mixed

normalize()

Normalizes filter keys to camelCase (`color_identity` → `colorIdentity`) and drops empty values.

protected static normalize(array<string|int, mixed> $filters) : array<string|int, mixed>
Parameters
$filters : array<string|int, mixed>
Return values
array<string|int, mixed>

orderBy()

Adds an `ORDER BY` term.

protected orderBy(string $term[, array<string|int, mixed> $bindings = [] ]) : void
Parameters
$term : string

SQL, with ? placeholders.

$bindings : array<string|int, mixed> = []

Values for the placeholders.

paginate()

Applies `page` and `pageSize`.

protected paginate(mixed $page, mixed $pageSize) : void
Parameters
$page : mixed

1-based page.

$pageSize : mixed

Rows per page, 1-100.

Tags
throws
InvalidArgumentException

When either is out of range.

placeholders()

`?, ?, ?` for a list of values.

protected static placeholders(array<string|int, mixed> $values) : string
Parameters
$values : array<string|int, mixed>
Return values
string

present()

Requires a column to hold a value.

protected static present(string $column) : array{0: string, 1: array}
Parameters
$column : string

Qualified column name.

Return values
array{0: string, 1: array}

printingOrder()

The most regular printing first: English, from a regular set, not a promo, paper, not funny, newest, front face. No placeholders.

protected static printingOrder() : array<string|int, string>
Return values
array<string|int, string> —

ORDER BY terms.

rank()

The default ranking: exact name matches, shorter names, then the most regular printing, newest first.

protected rank() : void

scalar()

Converts a scalar filter value to a string.

protected static scalar(mixed $value) : string
Parameters
$value : mixed
Tags
throws
InvalidArgumentException

When it is not scalar.

Return values
string

select()

The select list.

protected select() : string
Tags
inheritDoc
Return values
string

sort()

Orders by an explicit field.

protected sort(string $value, array<string|int, mixed> $cards) : void
Parameters
$value : string

A column, -column or column desc.

$cards : array<string|int, mixed>

The cards columns.

Tags
throws
InvalidArgumentException

On an unknown field.

terms()

The first term of each alternative, for single-valued fields.

protected static terms(mixed $value) : array<string|int, string>
Parameters
$value : mixed
Return values
array<string|int, string>

truthy()

Reads a boolean-ish filter value.

protected static truthy(mixed $value) : bool
Parameters
$value : mixed
Return values
bool

unique()

Keeps one row per card name — its most regular printing (see {@see printingOrder()}) among the rows the other filters match — so a search lists cards rather than every printing and face of them.

protected unique() : void

where()

Adds a condition.

protected where(array{0: ?string, 1: array} $condition) : void
Parameters
$condition : array{0: ?string, 1: array}

[sql, bindings]; a null sql adds nothing.

On this page

Search results