Properties

$defaultEncoding

$defaultEncoding : string

The default encoding to use while reading.

Type

string

$badCharacters

$badCharacters : string

Bad characters that are automatically removed when using `normalize()`.

Type

string

$indentCharacters

$indentCharacters : string

The characters that are seen as indentation.

Type

string

$quoteCharacters

$quoteCharacters : string

Characters that are seen as quote characters.

Type

string

$expressionBrackets

$expressionBrackets : array

An array of brackets that are seen as valid brackets for bracket counting.

Key is always the open-bracket, value is the close-bracket.

Type

array

$pregErrors

$pregErrors : array

An array of PREG errors with a good error message.

Type

array

$input

$input : string

The current input string that is read.

Type

string

$encoding

$encoding : string

The encoding currently used in this reading process.

Type

string

$position

$position : integer

The position in the input we are currently at.

Type

integer

$line

$line : integer

Contains the line the reader is currently on.

Type

integer

$offset

$offset : integer

Contains the offset of the line the reader is currently on.

Type

integer

$lastPeekResult

$lastPeekResult : string|null

Contains the last result of `peek()`, if any.

Type

string|null

$lastMatchResult

$lastMatchResult : array|null

Contains the last result-array of `match()`, if any.

This is a normal array created by preg_match-matching.

Type

array|null

$nextConsumeLength

$nextConsumeLength : integer|null

The length that is to be consumed by `consume()`, if any.

Type

integer|null

$inputLength

$inputLength : 

Type

Methods

setPath()

setPath(string  $path) : $this

Parameters

string $path

Returns

$this

__construct()

__construct(string  $input, string|null  $encoding = null) 

Creates a new reader instance.

Parameters

string $input

the input string to read from.

string|null $encoding

the encoding used in the reading process.

getInput()

getInput() : string

Returns the current input string.

This doesn't equal the initial input string, as it's consumed byte by byte.

Returns

string

getEncoding()

getEncoding() : string

Returns the currently used encoding.

Returns

string

getLastPeekResult()

getLastPeekResult() : string

Returns the last result of a `peek()`-call.

Returns

string

getLastMatchResult()

getLastMatchResult() : array

Returns the last result of a `match()`-call.

Returns

array

getNextConsumeLength()

getNextConsumeLength() : integer

Returns the length that `consume()` should consume next.

Returns

integer

getPosition()

getPosition() : integer

Returns the current position in our input string.

Returns

integer

getLine()

getLine() : integer

Returns the line the reader is currently on.

Returns

integer

getOffset()

getOffset() : integer

Returns the offset of the line the reader is currently on.

Returns

integer

normalize()

normalize() : $this

Removes useless characters from the whole input string.

Returns

$this

getLength()

getLength() : integer

Returns the total length of the remaining input string.

Returns

integer

hasLength()

hasLength() : boolean

Returns wether the input string still has characters remaining.

Returns

boolean

peek()

peek(integer  $length = null, integer  $start = null) : string|null

Peeks one or multiple characters without moving the pointer forward.

The peeked length will be stored and can be consumed with consume() later on.

Parameters

integer $length

the length to consume (default: 1).

integer $start

the offset to start on based on the current offset (default: 0).

Returns

string|null —

the peeked string or null if reading is finished.

match()

match(string  $pattern, string  $modifiers = null, string  $ignoredSuffixes = null) : boolean

Matches current input string against a regular expression.

The result length will be stored and can be consumed with consume() later on.

Notice that ^ is automatically prepended to the pattern.

Parameters

string $pattern

the regular expression without slashes or modifiers.

string $modifiers

the modifiers for the regular expression.

string $ignoredSuffixes

characters that are scanned, but don't end up in the consume length.

Throws

\Phug\ReaderException

Returns

boolean —

wether the expression matched or not.

getMatch()

getMatch(string|integer  $key) : string|null

Returns a single capture group matched with `match()` based on its index or name.

Parameters

string|integer $key

the index or name of the capturing group.

Returns

string|null —

the matched string part.

getMatchData()

getMatchData() : array

Returns all named capturing groups matched with `match()` in an array.

Returns

array —

the matched string parts indexed by capturing group name.

consume()

consume(integer  $length = null) : string

Consumes part of the input string and advances internal counters.

When no length is given, it will use the last peek() or match() length automatically. Use this after successful peek() or match()-operations.

Parameters

integer $length

the length to consume (default: null)

Returns

string

readWhile()

readWhile(callable  $callback, integer  $peekLength = null) : string|null

Reads part of a string until it doesn't match the given callback anymore.

The string part is consumed directly, no consume() is required after read()-operations.

Parameters

callable $callback

the callback to check string parts against.

integer $peekLength

the length to peek for each iteration. (default: 1)

Returns

string|null —

the result string or null if finished reading.

readUntil()

readUntil(callable  $callback, integer  $peekLength = null) : string|null

The opposite of `readWhile()`. Reads a string until the callback matches the string part.

Parameters

callable $callback

the callback to check string parts against.

integer $peekLength

the length to peek for each iteration. (default: 1)

Returns

string|null —

the result string or null if finished reading.

peekChar()

peekChar(string  $char) : boolean

Peeks one byte and checks if it equals the given character.

Parameters

string $char

the character to check against.

Returns

boolean —

whether it matches or not.

peekChars()

peekChars(string|array  $chars) : boolean

Peeks one byte and checks if it equals the given characters.

You can pass the characters as a string containing them all or as an array.

Parameters

string|array $chars

the characters to check against.

Returns

boolean —

whether one of them match or not.

peekString()

peekString(string  $string) : boolean

Peeks and checks if it equals the given string.

Parameters

string $string

the string to check against.

Returns

boolean —

whether it matches or not.

peekNewLine()

peekNewLine() : boolean

Peeks one byte and checks if it is a newline character.

Returns

boolean —

whether it matches or not.

peekIndentation()

peekIndentation() : boolean

Peeks one byte and checks if it is an indentation character.

The indentation characters are defined in Reader->indentCharacters

Returns

boolean —

whether it is one or not.

peekQuote()

peekQuote() : boolean

Peeks one byte and checks if it is a quote character.

The quote characters are defined in Reader->quoteCharacters

Returns

boolean —

whether it is one or not.

peekSpace()

peekSpace() : boolean

Peeks one byte and checks if it is a whitespace character.

Uses ctype_space() internally.

Returns

boolean —

whether it is one or not.

peekDigit()

peekDigit() : boolean

Peeks one byte and checks if it is a digit character.

Uses ctype_digit() internally.

Returns

boolean —

whether it is one or not.

peekAlpha()

peekAlpha() : boolean

Peeks one byte and checks if it is a alphabetical character.

Uses ctype_alpha() internally.

Returns

boolean —

whether it is one or not.

peekAlphaNumeric()

peekAlphaNumeric() : boolean

Peeks one byte and checks if it is a alpha-numeric character.

Uses ctype_alnum() internally.

Returns

boolean —

whether it is one or not.

peekAlphaIdentifier()

peekAlphaIdentifier(array  $allowedChars = null) : boolean

Peeks one byte and checks if it could be a valid alphabetical identifier.

Parameters

array $allowedChars

additional chars to allow in the identifier (default: ['_'])

Returns

boolean —

whether it could be or not.

peekIdentifier()

peekIdentifier(array  $allowedChars = null) : boolean

Peeks one byte and checks if it could be a valid alpha-numeric identifier.

Parameters

array $allowedChars

additional chars to allow in the identifier (default: ['_'])

Returns

boolean —

whether it could be or not.

readIndentation()

readIndentation() : string|null

Reads all upcoming indentation characters in a string using `peekIndentation()`.

Returns

string|null —

the indentation string part or null if no indentation encountered.

readUntilNewLine()

readUntilNewLine() : string

Reads a whole line and returns it.

Returns

string —

the line until the new-line character.

readSpaces()

readSpaces() : string|null

Reads all upcoming whitespace characters in a string using `ctype_space()`.

Returns

string|null —

the whitespace string part or null if no whitespace encountered.

readDigits()

readDigits() : string|null

Reads all upcoming digit characters in a string using `ctype_digit()`.

Returns

string|null —

the digit string part or null if no digits encountered.

readAlpha()

readAlpha() : string|null

Reads all upcoming alphabetical characters in a string using `ctype_alpha()`.

Returns

string|null —

the alphabetical string part or null if no alphabetical encountered.

readAlphaNumeric()

readAlphaNumeric() : string|null

Reads all upcoming alpha-numeric characters in a string using `ctype_alnum()`.

Returns

string|null —

the alpha-numeric string part or null if no alpha-numeric encountered.

readIdentifier()

readIdentifier(string  $prefix = null, array  $allowedChars = null) : string|null

Reads an upcoming alpha-numeric identifier in a string.

Identifiers start with an alphabetical character and then follow with alpha-numeric characters.

Parameters

string $prefix

the prefix for an identifier (e.g. $, @, % etc., default: none)

array $allowedChars

additional chars to allow in the identifier (default: ['_'])

Returns

string|null —

the resulting identifier or null of none encountered.

readString()

readString(array  $escapeSequences = null, boolean  $raw = false) : string|null

Reads an enclosed string correctly.

Strings start with a quote and end with that same quote while other quotes inside it are ignored, including other kinds of expressions.

The quote itself is automatically passed as an escape sequence, so a '-enclosed string always knows \' as an escape expression.

Parameters

array $escapeSequences
boolean $raw

whether to return the string raw, with quotes and keep escape sequences intact.

Returns

string|null —

the resulting string or null if none encountered.

readExpression()

readExpression(array  $breaks = null, array  $brackets = null) : string|null

Reads a code-expression that applies bracket counting correctly.

The expression reading stops on any string specified in the $breaks-argument. Breaks will be ignored if we are still in an open bracket.

Notice that this also validates brackets, if any bracket set doesn't match, this will throw an exception (e.g. "callMe(['demacia')]" would throw an exception)

Parameters

array $breaks

the break characters to use (Breaks on string end by default).

array $brackets

the brackets to allow (Defaulting to whatever is defined in Reader->expressionBrackets)

Returns

string|null —

the resulting expression or null, if none encountered.

getPregErrorText()

getPregErrorText(null  $code = null) : string

Returns a describing text for the last PREG error that happened.

Parameters

null $code

Returns

string

throwException()

throwException(string  $message) 

Throws an exception that contains useful debugging information.

Parameters

string $message

the message to pass to the exception.

Throws

\Phug\ReaderException