class LikeBuilder (View source)

Constants

DEFAULT_ANYCHARACTER_WILDCARD

The default wildcard that matches any number of characters in a LIKE query.

DEFAULT_ONECHARACTER_WILDCARD

The default wildcard that matches exactly one character in a LIKE query.

DEFAULT_ESCAPE_CHARACTER

The default character used to escape wildcards and other special characters.

Properties

protected string $anyCharacterWildcard

The wildcard that matches any number of characters in a LIKE query.

protected string $oneCharacterWildcard

The wildcard that matches exactly one character in a LIKE query.

protected string[] $otherWildcards

Any other characters that may have a special meaning in a LIKE query.

protected string $escapeCharacter

The character used to escape wildcards and other special characters.

protected array|null $escapeMap

The string mapping used to escape special characters.

Methods

__construct($anyCharacterWildcard = self::DEFAULT_ANYCHARACTER_WILDCARD, $oneCharacterWildcard = self::DEFAULT_ONECHARACTER_WILDCARD, $escapeCharacter = self::DEFAULT_ESCAPE_CHARACTER, array $otherWildcards = [])

Initialize the instance.

string
getAnyCharacterWildcard()

Get the wildcard that matches any number of characters in a LIKE query.

string
getOneCharacterWildcard()

Get the wildcard that matches exactly one character in a LIKE query.

array
getEscapeMap()

Get the string mapping used to escape special characters.

string
escapeForLike(string $string, bool $wildcardAtStart = true, bool $wildcardAtEnd = true)

Escape a string to be safely used as a parameter for a LIKE query.

string[]|null
splitKeywordsForLike(string|mixed $string, string $wordSeparators = '\\s', bool $addWildcards = true)

Split a string into words and format them to be used in LIKE queries.

Details

__construct($anyCharacterWildcard = self::DEFAULT_ANYCHARACTER_WILDCARD, $oneCharacterWildcard = self::DEFAULT_ONECHARACTER_WILDCARD, $escapeCharacter = self::DEFAULT_ESCAPE_CHARACTER, array $otherWildcards = [])

Initialize the instance.

Parameters

$anyCharacterWildcard
$oneCharacterWildcard
$escapeCharacter
array $otherWildcards

string getAnyCharacterWildcard()

Get the wildcard that matches any number of characters in a LIKE query.

Return Value

string

string getOneCharacterWildcard()

Get the wildcard that matches exactly one character in a LIKE query.

Return Value

string

protected array getEscapeMap()

Get the string mapping used to escape special characters.

Return Value

array

string escapeForLike(string $string, bool $wildcardAtStart = true, bool $wildcardAtEnd = true)

Escape a string to be safely used as a parameter for a LIKE query.

Parameters

string $string

The string to be escaped

bool $wildcardAtStart

whether to add the any-character wildcard as a prefix

bool $wildcardAtEnd

whether to add the any-character wildcard as a suffix

Return Value

string

Examples

escapeForLike('Hi% there', false, true) will return 'Hi\% there%'

string[]|null splitKeywordsForLike(string|mixed $string, string $wordSeparators = '\\s', bool $addWildcards = true)

Split a string into words and format them to be used in LIKE queries.

Parameters

string|mixed $string

The string to be splitted

string $wordSeparators

The regular expression pattern that represents potential word delimiters (eg '\s' for any whitespace character)

bool $addWildcards

whether to add any-character wildcard at start and end of every resulting word

Return Value

string[]|null

Returns null if no word has been found, an array of escaped words otherwise