MailMimeParser 1.1

CommentConsumer extends GenericConsumer

Consumes all tokens within parentheses as comments.

Parenthetical comments in mime-headers can be nested within one another. The outer-level continues after an inner-comment ends. Additionally, quoted-literals may exist with comments as well meaning a parenthesis inside a quoted string would not begin or end a comment section.

In order to satisfy these specifications, CommentConsumer inherits from GenericConsumer which defines CommentConsumer and QuotedStringConsumer as sub-consumers.

Examples: X-Mime-Header: Some value (comment) X-Mime-Header: Some value (comment (nested comment) still in comment) X-Mime-Header: Some value (comment "and part of original ) comment" - still a comment)

Tags
author

Zaahid Bateson

Table of Contents

$consumerService  : ConsumerService
$partFactory  : HeaderPartFactory
__construct()  : mixed
Initializes the instance.
__invoke()  : array<string|int, HeaderPart>
Invokes parsing of a header's value into header parts.
getInstance()  : mixed
Returns the singleton instance for the class.
advanceToNextToken()  : mixed
Calls $tokens->next() and returns.
filterIgnoredSpaces()  : array<string|int, HeaderPart>
Filters out ignorable spaces between parts in the passed array.
getAllConsumers()  : array<string|int, AbstractConsumer>
Returns this consumer and all unique sub consumers.
getAllTokenSeparators()  : array<string|int, string>
Returns a list of regular expression markers for this consumer and all sub-consumers by calling 'getTokenSeparators'.
getConsumerTokenParts()  : array<string|int, HeaderPart>|array<string|int, mixed>
Iterates through this consumer's sub-consumers checking if the current token triggers a sub-consumer's start token and passes control onto that sub-consumer's parseTokenIntoParts. If no sub-consumer is responsible for the current token, calls getPartForToken and returns it in an array.
getPartForToken()  : HeaderPart
Instantiates and returns Part\Token objects. Tokens from this and sub-consumers are combined into a Part\CommentPart in combineParts.
getSubConsumers()  : array<string|int, AbstractConsumer>
Called during construction to set up the list of sub-consumers that will take control from this consumer should a token match a sub-consumer's start token.
getTokenParts()  : array<string|int, HeaderPart>|array<string|int, mixed>
Returns an array of \ZBateson\MailMimeParser\Header\Part\HeaderPart for the current token on the iterator.
getTokenSeparators()  : array<string|int, string>
Returns patterns matching open and close parenthesis characters as separators.
getTokenSplitPattern()  : string
Returns a regex pattern used to split the input header string. The default implementation calls getAllTokenSeparators and implodes the returned array with the regex OR '|' character as its glue.
isEndToken()  : bool
Returns true if the token is a close parenthesis character, ')'.
isStartToken()  : bool
Returns true if the token is an open parenthesis character, '('.
parseTokensIntoParts()  : array<string|int, HeaderPart>
Iterates over the passed token Iterator and returns an array of parsed \ZBateson\MailMimeParser\Header\Part\HeaderPart objects.
processParts()  : array<string|int, HeaderPart>|array<string|int, mixed>
Post processing involves creating a single Part\CommentPart out of generated parts from tokens. The Part\CommentPart is returned in an array.
splitRawValue()  : array<string|int, mixed>
Returns an array of split tokens from the input string.
addSpaces()  : mixed
Checks if the passed space part should be added to the returned parts and adds it.
addSpaceToRetParts()  : mixed
Loops over the $parts array from the current position, checks if the space should be added, then adds it to $retParts and returns.
isSpaceToken()  : bool
Returns true if the passed HeaderPart is a Token instance and a space.
parseRawValue()  : array<string|int, HeaderPart>
Called by __invoke to parse the raw header value into header parts.
shouldAddSpace()  : bool
Returns true if a space should be added based on the passed last and next parts.

Properties

Methods

__invoke()

Invokes parsing of a header's value into header parts.

public __invoke(string $value) : array<string|int, HeaderPart>
Parameters
$value : string

the raw header value

Return values
array<string|int, HeaderPart>

the array of parsed parts

getInstance()

Returns the singleton instance for the class.

public static getInstance(ConsumerService $consumerService, HeaderPartFactory $partFactory) : mixed
Parameters
$consumerService : ConsumerService
$partFactory : HeaderPartFactory
Return values
mixed

advanceToNextToken()

Calls $tokens->next() and returns.

protected advanceToNextToken(Iterator $tokens, bool $isStartToken) : mixed

The default implementation checks if the current token is an end token, and will not advance past it. Because a comment part of a header can be nested, its implementation must advance past its own 'end' token.

Parameters
$tokens : Iterator
$isStartToken : bool
Return values
mixed

filterIgnoredSpaces()

Filters out ignorable spaces between parts in the passed array.

protected filterIgnoredSpaces(array<string|int, HeaderPart$parts) : array<string|int, HeaderPart>

Spaces with parts on either side of it that specify they can be ignored are filtered out. filterIgnoredSpaces is called from within processParts, and if needed by an implementing class that overrides processParts, must be specifically called.

Parameters
$parts : array<string|int, HeaderPart>
Return values
array<string|int, HeaderPart>

getAllConsumers()

Returns this consumer and all unique sub consumers.

protected getAllConsumers() : array<string|int, AbstractConsumer>

Loops into the sub-consumers (and their sub-consumers, etc...) finding all unique consumers, and returns them in an array.

Return values
array<string|int, AbstractConsumer>

getAllTokenSeparators()

Returns a list of regular expression markers for this consumer and all sub-consumers by calling 'getTokenSeparators'.

protected getAllTokenSeparators() : array<string|int, string>

.

Return values
array<string|int, string>

an array of regular expression markers

getConsumerTokenParts()

Iterates through this consumer's sub-consumers checking if the current token triggers a sub-consumer's start token and passes control onto that sub-consumer's parseTokenIntoParts. If no sub-consumer is responsible for the current token, calls getPartForToken and returns it in an array.

protected getConsumerTokenParts(Iterator $tokens) : array<string|int, HeaderPart>|array<string|int, mixed>
Parameters
$tokens : Iterator
Return values
array<string|int, HeaderPart>|array<string|int, mixed>

getPartForToken()

Instantiates and returns Part\Token objects. Tokens from this and sub-consumers are combined into a Part\CommentPart in combineParts.

protected getPartForToken(string $token, bool $isLiteral) : HeaderPart
Parameters
$token : string
$isLiteral : bool
Return values
HeaderPart

getSubConsumers()

Called during construction to set up the list of sub-consumers that will take control from this consumer should a token match a sub-consumer's start token.

protected abstract getSubConsumers() : array<string|int, AbstractConsumer>
Return values
array<string|int, AbstractConsumer>

the array of consumers

getTokenParts()

Returns an array of \ZBateson\MailMimeParser\Header\Part\HeaderPart for the current token on the iterator.

protected getTokenParts(Iterator $tokens) : array<string|int, HeaderPart>|array<string|int, mixed>

If the current token is a start token from a sub-consumer, the sub- consumer's parseTokensIntoParts method is called.

Parameters
$tokens : Iterator
Return values
array<string|int, HeaderPart>|array<string|int, mixed>

getTokenSeparators()

Returns patterns matching open and close parenthesis characters as separators.

protected getTokenSeparators() : array<string|int, string>
Return values
array<string|int, string>

the patterns

getTokenSplitPattern()

Returns a regex pattern used to split the input header string. The default implementation calls getAllTokenSeparators and implodes the returned array with the regex OR '|' character as its glue.

protected getTokenSplitPattern() : string
Return values
string

the regex pattern

isEndToken()

Returns true if the token is a close parenthesis character, ')'.

protected isEndToken(string $token) : bool
Parameters
$token : string
Return values
bool

isStartToken()

Returns true if the token is an open parenthesis character, '('.

protected isStartToken(string $token) : bool
Parameters
$token : string
Return values
bool

parseTokensIntoParts()

Iterates over the passed token Iterator and returns an array of parsed \ZBateson\MailMimeParser\Header\Part\HeaderPart objects.

protected parseTokensIntoParts(Iterator $tokens) : array<string|int, HeaderPart>

The method checks each token to see if the token matches a sub-consumer's start token, or if it matches the current consumer's end token to stop processing.

If a sub-consumer's start token is matched, the sub-consumer is invoked and its returned parts are merged to the current consumer's header parts.

After all tokens are read and an array of Header\Parts are constructed, the array is passed to AbstractConsumer::processParts for any final processing.

Parameters
$tokens : Iterator

an iterator over a string of tokens

Return values
array<string|int, HeaderPart>

an array of parsed parts

processParts()

Post processing involves creating a single Part\CommentPart out of generated parts from tokens. The Part\CommentPart is returned in an array.

protected processParts(array<string|int, HeaderPart$parts) : array<string|int, HeaderPart>|array<string|int, mixed>
Parameters
$parts : array<string|int, HeaderPart>
Return values
array<string|int, HeaderPart>|array<string|int, mixed>

splitRawValue()

Returns an array of split tokens from the input string.

protected splitRawValue(string $rawValue) : array<string|int, mixed>

The method calls preg_split using getTokenSplitPattern. The split array will not contain any empty parts and will contain the markers.

Parameters
$rawValue : string

the raw string

Return values
array<string|int, mixed>

the array of tokens

addSpaces()

Checks if the passed space part should be added to the returned parts and adds it.

private addSpaces(array<string|int, HeaderPart$parts, array<string|int, HeaderPart&$retParts, int $curIndex[, HeaderPart &$spacePart = null ]) : mixed

Never adds a space if it's the first part, otherwise only add it if either part isn't set to ignore the space

Parameters
$parts : array<string|int, HeaderPart>
$retParts : array<string|int, HeaderPart>
$curIndex : int
$spacePart : HeaderPart = null
Return values
mixed

addSpaceToRetParts()

Loops over the $parts array from the current position, checks if the space should be added, then adds it to $retParts and returns.

private addSpaceToRetParts(array<string|int, HeaderPart$parts, array<string|int, HeaderPart&$retParts, int $curIndex, HeaderPart &$spacePart, HeaderPart $lastPart) : mixed
Parameters
$parts : array<string|int, HeaderPart>
$retParts : array<string|int, HeaderPart>
$curIndex : int
$spacePart : HeaderPart
$lastPart : HeaderPart
Return values
mixed

isSpaceToken()

Returns true if the passed HeaderPart is a Token instance and a space.

private isSpaceToken(HeaderPart $part) : bool
Parameters
$part : HeaderPart
Return values
bool

parseRawValue()

Called by __invoke to parse the raw header value into header parts.

private parseRawValue(string $value) : array<string|int, HeaderPart>

Calls splitTokens to split the value into token part strings, then calls parseParts to parse the returned array.

Parameters
$value : string
Return values
array<string|int, HeaderPart>

the array of parsed parts

Search results