Reads headers separated into parameters consisting of a main value, and subsequent name/value pairs - for example text/html; charset=utf-8.

A ParameterConsumer's parts are separated by a semi-colon. Its name/value pairs are separated with an '=' character.

Parts may be mime-encoded entities. Additionally, a value can be quoted and comments may exist.

author Zaahid Bateson
package MailMimeParser
inherited_from \ZBateson\MailMimeParser\Header\Consumer\GenericConsumer

 Methods

Initializes the instance.

__construct(\ZBateson\MailMimeParser\Header\Consumer\ConsumerService $consumerService, \ZBateson\MailMimeParser\Header\Part\HeaderPartFactory $partFactory) 
Inherited

Parameters

$consumerService

\ZBateson\MailMimeParser\Header\Consumer\ConsumerService

$partFactory

\ZBateson\MailMimeParser\Header\Part\HeaderPartFactory

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

__invoke(string $value) : array<mixed,\ZBateson\MailMimeParser\Header\Part\HeaderPart>
Inherited

Parameters

$value

string

the raw header value

Returns

array<mixed,\ZBateson\MailMimeParser\Header\Part\HeaderPart>the array of parsed parts

Returns the singleton instance for the class.

getInstance(\ZBateson\MailMimeParser\Header\Consumer\ConsumerService $consumerService, \ZBateson\MailMimeParser\Header\Part\HeaderPartFactory $partFactory) 
InheritedStatic

Parameters

$consumerService

\ZBateson\MailMimeParser\Header\Consumer\ConsumerService

$partFactory

\ZBateson\MailMimeParser\Header\Part\HeaderPartFactory

Determines if the iterator should be advanced to the next token after reading tokens or finding a start token.

advanceToNextToken(\Iterator $tokens, boolean $isStartToken) 
Inherited

The default implementation will advance for a start token, but not advance on the end token of the current consumer, allowing the end token to be passed up to a higher-level consumer.

Parameters

$tokens

\Iterator

$isStartToken

boolean

Filters out ignorable spaces between parts in the passed array.

filterIgnoredSpaces(array<mixed,\ZBateson\MailMimeParser\Header\Part\HeaderPart> $parts) : array<mixed,\ZBateson\MailMimeParser\Header\Part\HeaderPart>
Inherited

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<mixed,\ZBateson\MailMimeParser\Header\Part\HeaderPart>

Returns

array<mixed,\ZBateson\MailMimeParser\Header\Part\HeaderPart>

Returns this consumer and all unique sub consumers.

getAllConsumers() : array<mixed,\ZBateson\MailMimeParser\Header\AbstractConsumer>
Inherited

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

Returns

array<mixed,\ZBateson\MailMimeParser\Header\AbstractConsumer>

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

getAllTokenSeparators() : array<mixed,string>
Inherited

.

Returns

array<mixed,string>an array of regular expression markers

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.

getConsumerTokenParts(\Iterator $tokens) : array<mixed,\ZBateson\MailMimeParser\Header\Part\HeaderPart> | array
Inherited

Parameters

$tokens

\Iterator

Returns

array<mixed,\ZBateson\MailMimeParser\Header\Part\HeaderPart>array

Creates and returns a \ZBateson\MailMimeParser\Header\Part\Token out of the passed string token and returns it, unless the token is an escaped literal, in which case a LiteralPart is returned.

getPartForToken(string $token, boolean $isLiteral) : \ZBateson\MailMimeParser\Header\Part\HeaderPart

Parameters

$token

string

$isLiteral

boolean

Returns

\ZBateson\MailMimeParser\Header\Part\HeaderPart

Constructs and returns a \ZBateson\MailMimeParser\Header\Part\HeaderPart for the passed string token. If the token should be ignored, the function must return null.

getPartForToken(string $token, boolean $isLiteral) : \ZBateson\MailMimeParser\Header\Part\HeaderPart | null
Inherited

The default created part uses the instance's partFactory->newInstance method.

Parameters

$token

string

the token

$isLiteral

boolean

set to true if the token represents a literal - e.g. an escaped token

Returns

\ZBateson\MailMimeParser\Header\Part\HeaderPartnullthe constructed header part or null if the token should be ignored

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.

getSubConsumers() : array<mixed,\ZBateson\MailMimeParser\Header\Consumer\AbstractConsumer>
Inherited

Returns

array<mixed,\ZBateson\MailMimeParser\Header\Consumer\AbstractConsumer>the array of consumers

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

getTokenParts(\Iterator $tokens) : array<mixed,\ZBateson\MailMimeParser\Header\Part\HeaderPart> | array
Inherited

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

Parameters

$tokens

\Iterator

Returns

array<mixed,\ZBateson\MailMimeParser\Header\Part\HeaderPart>array

Returns semi-colon and equals char as token separators.

getTokenSeparators() : array<mixed,string>

The whitespace character delimits mime-encoded parts for decoding.

inherited_from \ZBateson\MailMimeParser\Header\Consumer\GenericConsumer::getTokenSeparators()

Returns

array<mixed,string>

Returns an array of regular expression separators specific to this consumer. The returned patterns are used to split the header value into tokens for the consumer to parse into parts.

getTokenSeparators() : array<mixed,string>
Inherited

Each array element makes part of a generated regular expression that is used in a call to preg_split(). RegEx patterns can be used, and care should be taken to escape special characters.

Returns

array<mixed,string>the array of patterns

Overridden to use a specialized regex for finding mime-encoded parts (RFC 2047).

getTokenSplitPattern() : string

Some implementations seem to place mime-encoded parts within quoted parameters, and split the mime-encoded parts across multiple split parameters. The specialized regex doesn't allow double quotes inside a mime encoded part, so it can be "continued" in another parameter.

Returns

stringthe regex pattern

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.

getTokenSplitPattern() : string
Inherited

Returns

stringthe regex pattern

Returns true if the passed string token marks the end marker for the current consumer.

isEndToken(string $token) : boolean
Inherited

Parameters

$token

string

the current token

Returns

boolean

Returns true if the passed string token marks the beginning marker for the current consumer.

isStartToken(string $token) : boolean
Inherited

Parameters

$token

string

the current token

Returns

boolean

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

parseTokensIntoParts(\Iterator $tokens) : array<mixed,\ZBateson\MailMimeParser\Header\Part\HeaderPart>
Inherited

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

Returns

array<mixed,\ZBateson\MailMimeParser\Header\Part\HeaderPart>an array of parsed parts

Post processing involves creating Part\LiteralPart or Part\ParameterPart objects out of created Token and LiteralParts.

processParts(array<mixed,\ZBateson\MailMimeParser\Header\Consumer\HeaderPart> $parts) : array<mixed,\ZBateson\MailMimeParser\Header\Consumer\HeaderPart> | array

The default implementation simply returns the passed array after filtering out null/empty parts.

inherited_from \ZBateson\MailMimeParser\Header\Consumer\GenericConsumer::processParts()

Parameters

$parts

array<mixed,\ZBateson\MailMimeParser\Header\Consumer\HeaderPart>

Returns

array<mixed,\ZBateson\MailMimeParser\Header\Consumer\HeaderPart>array

Performs any final processing on the array of parsed parts before returning it to the consumer client.

processParts(array<mixed,\ZBateson\MailMimeParser\Header\Part\HeaderPart> $parts) : array<mixed,\ZBateson\MailMimeParser\Header\Part\HeaderPart>
Inherited

The default implementation simply returns the passed array after filtering out null/empty parts.

Parameters

$parts

array<mixed,\ZBateson\MailMimeParser\Header\Part\HeaderPart>

Returns

array<mixed,\ZBateson\MailMimeParser\Header\Part\HeaderPart>

Returns an array of split tokens from the input string.

splitRawValue(string $rawValue) : array
Inherited

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

Returns

arraythe array of tokens

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

addSpaceToRetParts(array<mixed,\ZBateson\MailMimeParser\Header\Part\HeaderPart> $parts, array<mixed,\ZBateson\MailMimeParser\Header\Part\HeaderPart> $retParts, integer $curIndex, \ZBateson\MailMimeParser\Header\Part\HeaderPart $spacePart, \ZBateson\MailMimeParser\Header\Part\HeaderPart $lastPart) 
Inherited

Parameters

$parts

array<mixed,\ZBateson\MailMimeParser\Header\Part\HeaderPart>

$retParts

array<mixed,\ZBateson\MailMimeParser\Header\Part\HeaderPart>

$curIndex

integer

$spacePart

\ZBateson\MailMimeParser\Header\Part\HeaderPart

$lastPart

\ZBateson\MailMimeParser\Header\Part\HeaderPart

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

addSpaces(array<mixed,\ZBateson\MailMimeParser\Header\Part\HeaderPart> $parts, array<mixed,\ZBateson\MailMimeParser\Header\Part\HeaderPart> $retParts, integer $curIndex, \ZBateson\MailMimeParser\Header\Part\HeaderPart $spacePart = null
Inherited

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<mixed,\ZBateson\MailMimeParser\Header\Part\HeaderPart>

$retParts

array<mixed,\ZBateson\MailMimeParser\Header\Part\HeaderPart>

$curIndex

integer

$spacePart

\ZBateson\MailMimeParser\Header\Part\HeaderPart

Adds the passed parameter with the given name and value to a SplitParameterToken, at the passed index. If one with the given name doesn't exist, it is created.

addToSplitPart(\ArrayObject $splitParts, string $name, string $value, integer $index, boolean $isEncoded) 

Parameters

$splitParts

\ArrayObject

$name

string

$value

string

$index

integer

$isEncoded

boolean

Loops over parts in the passed array, creating ParameterParts out of any parsed SplitParameterTokens, replacing them in the array.

finalizeParameterParts(\ArrayObject $combined) : array<mixed,\ZBateson\MailMimeParser\Header\Consumer\HeaderPart> | array

The method then calls filterIgnoreSpaces to filter out empty elements in the combined array and returns an array.

Parameters

$combined

\ArrayObject

Returns

array<mixed,\ZBateson\MailMimeParser\Header\Consumer\HeaderPart>array

Instantiates and returns either a MimeLiteralPart if $strName is empty, a SplitParameterToken if the parameter is a split parameter and is the first in a series, null if it's a split parameter but is not the first part in its series, or a ParameterPart is returned otherwise.

getPartFor(string $strName, string $strValue, \ArrayObject $splitParts) : \ZBateson\MailMimeParser\Header\Part\MimeLiteralPart

If the part is a SplitParameterToken, it's added to the passed $splitParts as well with its name as a key.

Parameters

$strName

string

$strValue

string

$splitParts

\ArrayObject

Returns

\ZBateson\MailMimeParser\Header\Part\MimeLiteralPart|SplitParameterToken|\ZBateson\MailMimeParser\Header\Part\ParameterPart

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

isSpaceToken(\ZBateson\MailMimeParser\Header\Part\HeaderPart $part) : boolean
Inherited

Parameters

$part

\ZBateson\MailMimeParser\Header\Part\HeaderPart

Returns

boolean

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

parseRawValue(string $value) : array<mixed,\ZBateson\MailMimeParser\Header\Part\HeaderPart>
Inherited

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

Parameters

$value

string

Returns

array<mixed,\ZBateson\MailMimeParser\Header\Part\HeaderPart>the array of parsed parts

Handles parameter separator tokens during final processing.

processTokenPart(string $tokenValue, \ArrayObject $combined, \ArrayObject $splitParts, string $strName, string $strCat) : boolean

If the end token is found, a new HeaderPart is assigned to the passed $combined array. If an '=' character is found, $strCat is assigned to $strName and emptied.

Returns true if the token was processed, and false otherwise.

Parameters

$tokenValue

string

$combined

\ArrayObject

$splitParts

\ArrayObject

$strName

string

$strCat

string

Returns

boolean

Returns true if a space should be added based on the passed last and next parts.

shouldAddSpace(\ZBateson\MailMimeParser\Header\Part\HeaderPart $nextPart, \ZBateson\MailMimeParser\Header\Part\HeaderPart $lastPart) : boolean
Inherited

Parameters

$nextPart

\ZBateson\MailMimeParser\Header\Part\HeaderPart

$lastPart

\ZBateson\MailMimeParser\Header\Part\HeaderPart

Returns

boolean

 Properties

 

used to get consumer instances for sub-consumers

$consumerService : \ZBateson\MailMimeParser\Header\Consumer\ConsumerService

Default

 

used to construct HeaderPart objects

$partFactory : \ZBateson\MailMimeParser\Header\Part\HeaderPartFactory

Default