DiscordPHP-MTG Documentation

CardRepository extends AbstractRepository
in package
uses DatabaseRepositoryTrait

Card printings from MTGJSON's AllPrintings build — search with any {@see CardQuery} filter, and fetch one by its MTGJSON `uuid` — hydrating {@see Card} parts with their identifiers, legalities, rulings, foreign data and purchase URLs.

MTGJSON serves cards only inside bulk files, so these are answered from the local Database copy rather than over HTTP.

Tags
link

Card (Set) model

since
0.3.0

Table of Contents

Constants

MANY_PER_CARD  : array<string, array{0: string, 1: string}> = ['rulings' => ['cardRulings', '"date", rowid'],...
Tables holding a list of extra card data per uuid, by Card attribute, with their ordering.
ONE_PER_CARD  : array<string, string> = ['identifiers' => 'cardIdentifiers', 'legalitie...
Tables holding one row of extra card data per uuid, by Card attribute.

Properties

$class  : mixed
$database  : Database
The local MTGJSON build.
$discord  : mixed
$discrim  : mixed
$endpoints  : mixed
MTGJSON has no per-card route; see {@see DatabaseRepositoryTrait}.
$mtg_http  : Http
The extended HTTP client.

Methods

__call()  : mixed
This method checks if a method with the name "__Collection__{$name}" exists within the class. If it does, it calls that method with the provided arguments.
__construct()  : mixed
AbstractRepository constructor.
__get()  : mixed
Exposes a small allow-list of otherwise-protected properties (`discrim`, `cache`) for read access; any other key returns null.
clear()  : void
Clears the repository.
countCards()  : PromiseInterface<string|int, int>
Counts the card printings (or, with `unique`, the cards) a search matches, ignoring `page` and `pageSize`.
create()  : Part
Builds a new, empty part.
delete()  : PromiseInterface<string|int, Part>
Attempts to delete a part on the Discord servers.
fetch()  : PromiseInterface<string|int, Part>
Gets a part from the repository or the MTGJSON build.
filter()  : ExCollectionInterface
Runs a filter callback over the repository and returns a new collection based on the response of the callback.
find()  : Part|null
Runs a filter callback over the repository and returns the first part where the callback returns `true` when given the part.
first()  : Part|null
Returns the first cached part.
fresh()  : PromiseInterface<string|int, Part>
Refills a part from the MTGJSON build.
freshen()  : PromiseInterface
Rejects: the repository is too large to load whole; search it instead.
get()  : Part|null
Gets a part from the repository.
getCards()  : PromiseInterface<string|int, ExCollectionInterface<string|int, Card>>
Searches card printings. See {@see CardQuery} for every filter; the common ones are `name`, `manaValue` (or `cmc`), `colors`, `colorIdentity`, `type`, `types`, `subtypes`, `rarity`, `setCode` (or `set`), `text`, `gameFormat` + `legality`, `multiverseId`, `contains`, `orderBy`, `random`, `page` and `pageSize` (1-100, default 1).
getCardsByUuid()  : PromiseInterface<string|int, ExCollectionInterface<string|int, Card>>
Gets card printings by their MTGJSON uuids. Unknown uuids are skipped.
getIterator()  : Traversable
Returns an iterator for the cache.
getPrintings()  : PromiseInterface<string|int, array<string|int, array<string|int, mixed>>>
Every printing of a card face, newest first: {uuid, setCode, setName, number, releaseDate}. For the printing picker.
has()  : bool
Checks if the array has an object.
jsonSerialize()  : array<string|int, mixed>
keys()  : array<string|int, int>|array<string|int, string>
Get the keys of the items.
last()  : Part|null
Returns the last cached part.
offsetExists()  : bool
If the repository has an offset.
offsetGet()  : Part|null
Gets a part from the repository.
offsetSet()  : void
Sets a part into the repository.
offsetUnset()  : void
Unsets an index from the repository.
pull()  : Part|mixed
Pulls a part from the repository.
pushItem()  : self
Pushes a single item to the repository.
save()  : PromiseInterface<string|int, Part>
Attempts to save a part to the Discord servers.
set()  : mixed
Sets a part in the repository.
toArray()  : array<string|int, mixed>
Converts the weak caches to array.
values()  : array<string|int, mixed>
Get the values of the items.
cacheFreshen()  : PromiseInterface<string|int, static>
filtersFrom()  : array<string|int, mixed>
Turns a Card's attributes into filters: scalars as they are, lists as `,`-joined terms that must all match. Nested data is skipped.
hydrate()  : ExCollectionInterface<string|int, Card>
Builds Card parts from decoded `cards` rows, attaching their extra data.
lookup()  : ?Card
related()  : array<string, array<string|int, mixed>>
Reads the identifiers, legalities, purchase URLs, rulings and foreign data of a set of cards.
rows()  : array<string|int, array<string|int, mixed>>
Reads `cards` rows by uuid, with the set name.

Constants

MANY_PER_CARD

Tables holding a list of extra card data per uuid, by Card attribute, with their ordering.

public array<string, array{0: string, 1: string}> MANY_PER_CARD = ['rulings' => ['cardRulings', '"date", rowid'], 'foreignData' => ['cardForeignData', '"language", rowid']]

ONE_PER_CARD

Tables holding one row of extra card data per uuid, by Card attribute.

public array<string, string> ONE_PER_CARD = ['identifiers' => 'cardIdentifiers', 'legalities' => 'cardLegalities', 'purchaseUrls' => 'cardPurchaseUrls']

Properties

$class

protected mixed $class = \MTG\Parts\Card::class
Tags
inheritDoc

$endpoints

MTGJSON has no per-card route; see {@see DatabaseRepositoryTrait}.

protected mixed $endpoints = []
Tags
inheritDoc

Methods

__call()

This method checks if a method with the name "__Collection__{$name}" exists within the class. If it does, it calls that method with the provided arguments.

public __call(string $name, array<string|int, mixed> $arguments) : mixed

If the method does not exist, it throws a BadMethodCallException.

Previously, this class utilized parent::method to call methods from the parent class. This was changed to use the __Collection__method naming convention to avoid conflicts

Parameters
$name : string

The name of the method being called.

$arguments : array<string|int, mixed>

The arguments passed to the method.

Tags
throws
BadMethodCallException

If the method does not exist.

Return values
mixed —

The result of the called method.

__construct()

AbstractRepository constructor.

public __construct(MTG|Discord $discord[, array<string|int, mixed> $vars = [] ]) : mixed
Parameters
$discord : MTG|Discord
$vars : array<string|int, mixed> = []

An array of variables used for the endpoint.

__get()

Exposes a small allow-list of otherwise-protected properties (`discrim`, `cache`) for read access; any other key returns null.

public __get(string $key) : mixed
Parameters
$key : string

Property name.

countCards()

Counts the card printings (or, with `unique`, the cards) a search matches, ignoring `page` and `pageSize`.

public countCards([Card|array<string|int, mixed> $params = [] ]) : PromiseInterface<string|int, int>
Parameters
$params : Card|array<string|int, mixed> = []

The same filters as getCards().

Tags
since
1.1.0
Return values
PromiseInterface<string|int, int>

create()

Builds a new, empty part.

public create([array<string|int, mixed>|object $attributes = [] ][, bool $created = false ]) : Part
Parameters
$attributes : array<string|int, mixed>|object = []

The attributes for the new part.

$created : bool = false
Tags
throws
Exception
Return values
Part —

The new part.

delete()

Attempts to delete a part on the Discord servers.

public delete(Part|string $part[, string|null $reason = null ]) : PromiseInterface<string|int, Part>
Parameters
$part : Part|string

The part to delete.

$reason : string|null = null

Reason for Audit Log (if supported).

Tags
throws
Exception
Return values
PromiseInterface<string|int, Part>

fetch()

Gets a part from the repository or the MTGJSON build.

public fetch(string $id[, bool $fresh = false ]) : PromiseInterface<string|int, Part>
Parameters
$id : string

The discriminator value to search for.

$fresh : bool = false

Whether to skip the cache.

Return values
PromiseInterface<string|int, Part> —

Rejects with \OutOfBoundsException when the build has no such item.

filter()

Runs a filter callback over the repository and returns a new collection based on the response of the callback.

public filter(callable $callback) : ExCollectionInterface
Parameters
$callback : callable
Tags
todo

This method will be typed to return an ExCollectionInterface in v11

Return values
ExCollectionInterface

find()

Runs a filter callback over the repository and returns the first part where the callback returns `true` when given the part.

public find(callable $callback) : Part|null
Parameters
$callback : callable
Return values
Part|null —

null if no items returns true when called in the callback.

fresh()

Refills a part from the MTGJSON build.

public fresh(Part $part[, array<string|int, mixed> $queryparams = [] ]) : PromiseInterface<string|int, Part>
Parameters
$part : Part

The part to refresh.

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

Unused; kept for the upstream signature.

Return values
PromiseInterface<string|int, Part>

freshen()

Rejects: the repository is too large to load whole; search it instead.

public freshen([array<string|int, mixed> $queryparams = [] ]) : PromiseInterface
Parameters
$queryparams : array<string|int, mixed> = []

Unused.

Return values
PromiseInterface —

Rejects with \BadMethodCallException.

get()

Gets a part from the repository.

public get(string $discrim, mixed $key) : Part|null
Parameters
$discrim : string
$key : mixed
Return values
Part|null

getCards()

Searches card printings. See {@see CardQuery} for every filter; the common ones are `name`, `manaValue` (or `cmc`), `colors`, `colorIdentity`, `type`, `types`, `subtypes`, `rarity`, `setCode` (or `set`), `text`, `gameFormat` + `legality`, `multiverseId`, `contains`, `orderBy`, `random`, `page` and `pageSize` (1-100, default 1).

public getCards([Card|array<string|int, mixed> $params = [] ]) : PromiseInterface<string|int, ExCollectionInterface<string|int, Card>>
Parameters
$params : Card|array<string|int, mixed> = []

Filters, or a Card whose scalar and list attributes are used as filters.

Tags
since
0.3.0
Return values
PromiseInterface<string|int, ExCollectionInterface<string|int, Card>> —

Keyed by uuid. Rejects with \InvalidArgumentException on a bad filter.

getCardsByUuid()

Gets card printings by their MTGJSON uuids. Unknown uuids are skipped.

public getCardsByUuid(array<string|int, string> $uuids) : PromiseInterface<string|int, ExCollectionInterface<string|int, Card>>
Parameters
$uuids : array<string|int, string>
Tags
since
1.0.0
Return values
PromiseInterface<string|int, ExCollectionInterface<string|int, Card>> —

One card per distinct uuid, keyed by uuid.

getIterator()

Returns an iterator for the cache.

public & getIterator() : Traversable
Return values
Traversable

getPrintings()

Every printing of a card face, newest first: {uuid, setCode, setName, number, releaseDate}. For the printing picker.

public getPrintings(Card $card) : PromiseInterface<string|int, array<string|int, array<string|int, mixed>>>
Parameters
$card : Card
Tags
since
1.1.0
Return values
PromiseInterface<string|int, array<string|int, array<string|int, mixed>>>

has()

Checks if the array has an object.

public has(string|int ...$keys) : bool

Use async $repository->cache->has()

Parameters
$keys : string|int
Return values
bool

jsonSerialize()

public jsonSerialize([bool $assoc = true ]) : array<string|int, mixed>
Parameters
$assoc : bool = true
Tags
inheritDoc
Return values
array<string|int, mixed>

keys()

Get the keys of the items.

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

offsetExists()

If the repository has an offset.

public offsetExists(string|int $offset) : bool

Use async $repository->cache->has() This method is deprecated for userland code but can still be used internally within the library.

Parameters
$offset : string|int
Return values
bool

offsetGet()

Gets a part from the repository.

public offsetGet(string|int $offset) : Part|null

Use async $repository->cacheGet() or sync $repository->get() This method is deprecated for userland code but can still be used internally within the library.

Parameters
$offset : string|int
Return values
Part|null

offsetSet()

Sets a part into the repository.

public offsetSet(string|int $offset, ?Part $value) : void

Use async $repository->cache->set()

Parameters
$offset : string|int
$value : ?Part

offsetUnset()

Unsets an index from the repository.

public offsetUnset(string|int $offset) : void

Use async $repository->cache->delete()

Parameters
$offset : string|int

pull()

Pulls a part from the repository.

public pull(string|int $key[, mixed $default = null ]) : Part|mixed

Use async $repository->cachePull()

Parameters
$key : string|int
$default : mixed = null
Return values
Part|mixed

pushItem()

Pushes a single item to the repository.

public pushItem(Part $item) : self

Use async $repository->cache->set() This method is deprecated for userland code but can still be used internally within the library.

Parameters
$item : Part
Return values
self

save()

Attempts to save a part to the Discord servers.

public save(Part $part[, string|null $reason = null ]) : PromiseInterface<string|int, Part>

Use Part->save($reason) to ensure permissions are checked.

Parameters
$part : Part

The part to save.

$reason : string|null = null

Reason for Audit Log (if supported).

Tags
throws
Exception
Return values
PromiseInterface<string|int, Part>

set()

Sets a part in the repository.

public set(string|int $offset, Part $value) : mixed
Parameters
$offset : string|int
$value : Part

toArray()

Converts the weak caches to array.

public toArray([bool $assoc = true ]) : array<string|int, mixed>

Use jsonSerialize

Parameters
$assoc : bool = true
Return values
array<string|int, mixed>

values()

Get the values of the items.

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

cacheFreshen()

protected cacheFreshen(object $response) : PromiseInterface<string|int, static>
Parameters
$response : object
Return values
PromiseInterface<string|int, static>

filtersFrom()

Turns a Card's attributes into filters: scalars as they are, lists as `,`-joined terms that must all match. Nested data is skipped.

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

hydrate()

Builds Card parts from decoded `cards` rows, attaching their extra data.

protected hydrate(array<string|int, array<string|int, mixed>> $rows[, bool $cache = true ]) : ExCollectionInterface<string|int, Card>
Parameters
$rows : array<string|int, array<string|int, mixed>>

Decoded rows.

$cache : bool = true

Whether to cache the parts.

Return values
ExCollectionInterface<string|int, Card> —

Keyed by uuid.

lookup()

protected lookup(string $id) : ?Card
Parameters
$id : string
Tags
inheritDoc
Return values
?Card

Reads the identifiers, legalities, purchase URLs, rulings and foreign data of a set of cards.

protected related(array<string|int, string> $uuids) : array<string, array<string|int, mixed>>
Parameters
$uuids : array<string|int, string>
Return values
array<string, array<string|int, mixed>> —

Card attributes, by uuid.

rows()

Reads `cards` rows by uuid, with the set name.

protected rows(array<string|int, string> $uuids) : array<string|int, array<string|int, mixed>>
Parameters
$uuids : array<string|int, string>
Return values
array<string|int, array<string|int, mixed>> —

Decoded rows.

On this page

Search results