Index

Filter

MMO\core\helpers · 1 symbol · generated 30 Sep 2026

class

Filter

Filter.php:48

A single filter: a set of path => condition pairs, ALL of which must match (an implicit AND) for test to pass. Each condition is either a plain value (equality) or a ['$operator' => value] pair (FilterOperator).

class MMO\core\helpers\Filter implements MMO\core\interfaces\iFilter use MMO\core\classes\tArray
Namespace
MMO\core\helpers
Implements
iFilter
Uses traits
tArray
$f = Filter::From(['name' => 'Marc', 'age' => ['$gte' => 18]]); $f->test(['name' => 'Marc', 'age' => 30]); // true $f->test(['name' => 'Marc', 'age' => 10]); // false

Index

NATIVE_OPERATORSMongoDB's own top-level operators, besides the groups ($and, $or, $nor): kept as they are, for a repository that understands them -- MongoRepository::searchText() filters with `['$text' => ['$searc…
content
clear()Empty the underlying content, keeping the object itself.
count()\Countable::count
From($data, $vars) staticCreate a Filter or FilterGroup from an array or string.
getContent()
getIterator()\IteratorAggregate::getIterator
has($path)
IsCondition($value) staticWhether $value is a condition (['$gte' => 18], ['>=' => 18, '<' => 65]) rather than a value to compare with: each of its keys is an operator, or starts with $ like one.
isEmpty()
jsonSerialize()\JsonSerializable::jsonSerialize -- the raw content is returned as-is (not recursively converted), so json_encode() relies on any nested iArray values being JsonSerializable themselves.
keys()
loadArray($data)Load every entry of a plain array into the content, through offsetSet (so any offsetSet() override defined by the class using this trait, e.g.
offsetExists($key)\ArrayAccess::offsetExists
offsetGet($key)\ArrayAccess::offsetGet
offsetSet($offset, $value)\ArrayAccess::offsetSet -- a ['$operator' => value] array is recognized and converted into a FilterValue automatically (so $filter['age'] = ['$gte' => 18] behaves the same as building it via @s…
offsetUnset($key)\ArrayAccess::offsetUnset -- on a list (sequential integer keys, as produced by $bag[] = ...), the element is spliced out (closing the gap and reindexing), matching \unset() semantics on a plain …
test($data, $schema)
toArray()
toClosure($data, $schema)
__construct($data)
__toString()

Constants1

NATIVE_OPERATORS

MongoDB's own top-level operators, besides the groups ($and, $or, $nor): kept as they are, for a repository that understands them -- MongoRepository::searchText() filters with ['$text' => ['$search' => $text]].

const NATIVE_OPERATORS = ['$text', '$where', '$expr', '$jsonSchema', '$comment']

Properties1

content

protected $content = []

Methods20

clear()

tArray.php:25

Empty the underlying content, keeping the object itself.

function clear()
returns
$bag->clear(); // count($bag) === 0 afterwards

From($data, $vars)

staticFilter.php:96

Create a Filter or FilterGroup from an array or string.

static function From(array|string $data, ArrayAccess|array $vars = []): MMO\core\interfaces\iFilterSupported string format: `({path} {operator} {value}) {groupOperator} (...)` - {path}: valid json identifier on which the operator is applied. @ex `man/name` - {operator}: valid FilterOperator (starting with `$` or `<=`,`>=`,`!`,`=`) - {value}: valid json value. - if {value} is inside curly brackets and starts with #, it is converted to a reference to a path in the data. (example: {#xxx/yyy} represents the value at data[xxx][yyy]) - if {value} is inside curly brackets and starts with $, it is a value given in the vars (example {$xxx} is replaced by $vars['xxx']) - {groupOperator}: `!` (not), `&` (and), `|` (or)
$dataarray | string
$vars = []ArrayAccess | arrayvariables to inject in the string data (does not work if $data is an array) to use a variable, write it {$xxx} where xxx is a key in $vars
returnsiFilter
look for all firstname = lastname or firstname = $vars['name'] - (firstname = {#lastname}) | (firstname = {$name})Filter::From(['name' => 'Marc', 'age' => ['$gte' => 18]]); // a FilterFilter::From(['$or' => [['name' => 'Marc'], ['name' => 'Bob']]]); // a FilterGroupFilter::From(['age' => ['$gte' => 18, '$lt' => 65]]); // an AND FilterGroup: one condition per operatorFilter::From(['name' => 'Marc', '$or' => [['age' => 18], ['age' => 30]]]); // name AND (age OR age)Filter::From('(name = "Marc") & (age >= 18)');Filter::From('name = {$who}', ['who' => 'Marc']);

getContent()

Filter.php:63
function getContent(): array
returnsarrayraw path => (scalar|FilterValue) content

has($path)

Filter.php:308
function has(string $path): bool
$pathstring
returnsbooltrue if this filter has a condition on $path

IsCondition($value)

staticFilter.php:229

Whether $value is a condition (['$gte' => 18], ['>=' => 18, '<' => 65]) rather than a value to compare with: each of its keys is an operator, or starts with $ like one.

static function IsCondition(array $value): bool
$valuearray
returnsbool

isEmpty()

tArray.php:33
function isEmpty()
returnstrue if the underlying content has no elements
$bag->isEmpty(); // true right after clear(), false as soon as an item is set

jsonSerialize()

tArray.php:43

\JsonSerializable::jsonSerialize -- the raw content is returned as-is (not recursively converted), so json_encode() relies on any nested iArray values being JsonSerializable themselves.

function jsonSerialize(): mixed
returnsmixed
json_encode($bag); // e.g. '{"x":1,"0":2}'

keys()

tArray.php:82
function keys(): array
returnsarraythe list of keys currently set
$bag->keys(); // e.g. ['x', 0]

loadArray($data)

tArray.php:71

Load every entry of a plain array into the content, through offsetSet (so any offsetSet() override defined by the class using this trait, e.g. for validation or type coercion, is honored for every entry).

function loadArray(array $data): static
$dataarray
returnsstatic$this, for chaining
$bag->loadArray(['x' => 1, 'y' => 2]); // equivalent to $bag['x'] = 1; $bag['y'] = 2;

offsetExists($key)

tArray.php:130

\ArrayAccess::offsetExists

function offsetExists(mixed $key): bool
$keymixed
returnsbool

offsetSet($offset, $value)

Filter.php:242

\ArrayAccess::offsetSet -- a ['$operator' => value] array is recognized and converted into a FilterValue automatically (so $filter['age'] = ['$gte' => 18] behaves the same as building it via From).

function offsetSet(mixed $offset, mixed $value): void
$offsetmixed
$valuemixed
returnsvoid
throwsfor an unknown operator (['$gtee' => 18]), or several of them: a Filter holds one condition per path (From makes one Filter of each). The value of a NATIVE_OPERATORS is kept as it is.

offsetUnset($key)

tArray.php:107

\ArrayAccess::offsetUnset -- on a list (sequential integer keys, as produced by $bag[] = ...), the element is spliced out (closing the gap and reindexing), matching \unset() semantics on a plain PHP list; on an associative array, the key is simply removed.

function offsetUnset(mixed $key): void
$keymixed
returnsvoid
unset($bag[0]); // on a list: every following index shifts down by one, like array_splice()

test($data, $schema)

Filter.php:272
function test(ArrayAccess|array $data, ?MMO\core\schemas\Schema $schema = null): bool
$dataArrayAccess | arraythe record to test
$schema = nullnull | Schemaif given, each condition's value is coerced through the matching property's schema before comparing (e.g. so a string '18' compares correctly against an integer age); a value whose schema is not known (owner/username, when owner is typed with an interface: Schema::walkSchemaFor), or a bool one, is compared as it is
returnsbooltrue if EVERY path/condition in this filter matches (implicit AND)
(new Filter(['age' => ['$gte' => 18]]))->test(['age' => 30]); // true

toArray()

Filter.php:289
function toArray(): array
returnsarrayMongoDB-style filter array, e.g. ['name' => 'Marc', 'age' => ['$gte' => 18]]

toClosure($data, $schema)

Filter.php:259
function toClosure(ArrayAccess|array $data, ?MMO\core\schemas\Schema $schema = null): Closure
$dataArrayAccess | array
$schema = nullnull | Schema
returnsClosurea zero-arg closure that calls test($data, $schema) when invoked

__toString()

Filter.php:298
function __toString(): string
returnsstringe.g. '(name = "Marc" & age $gte 18)'