Parses a so-called "extended-domain" (from and by) part of a Received header.

Looks for and extracts the following fields from an extended-domain part:
Name, Hostname and Address.

The Name part is always the portion of the extended-domain part existing on
its own, outside of the parenthesized hostname and address part. This is
true regardless of whether an address is used as the name, as its assumed to
be the string used to identify the server, whatever it may be.

The parenthesized part normally (but not necessarily) following a name must
"look like" a tcp-info section of an extended domain as defined by RFC5321.
The validation is very purposefully very loose to be accommodating to many
erroneous implementations. The only restriction is the host part must
contain two characters, the first being alphanumeric, followed by any number
of more alphanumeric, '.', and '-' characters. The address part must be
within square brackets, '[]'... although an address outside of square
brackets could be matched by the domain matcher if it exists alone within the
parentheses. The address is any number of '.', numbers, ':' and letters a-f.
This allows it to match ipv6 addresses as well. In addition, the address may
start with the string "ipv6", and may be followed by a port number as some
implementations seem to do.

Strings in parentheses not matching the aforementioned 'domain/address'
pattern will be considered comments, and will be returned as a separate

public ZBateson\MailMimeParser\Header\Consumer\Received\GenericReceivedConsumer::__construct(ZBateson\MailMimeParser\Header\Consumer\ConsumerService $consumerService, ZBateson\MailMimeParser\Header\Part\HeaderPartFactory $partFactory, string $partName)

Constructor overridden to include $partName parameter.

public ZBateson\MailMimeParser\Header\Consumer\AbstractConsumer::__invoke(string $value) : array

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

  • param string $value the raw header value
  • return \ZBateson\MailMimeParser\Header\IHeaderPart[] the array of parsed
public static ZBateson\MailMimeParser\Header\Consumer\AbstractConsumer::getInstance(ZBateson\MailMimeParser\Header\Consumer\ConsumerService $consumerService, ZBateson\MailMimeParser\Header\Part\HeaderPartFactory $partFactory)

Returns the singleton instance for the class.

protected ZBateson\MailMimeParser\Header\Consumer\AbstractConsumer::$consumerService = NULL
  • var \ConsumerService used to get consumer instances for sub-consumers.
protected ZBateson\MailMimeParser\Header\Consumer\AbstractConsumer::$partFactory = NULL
  • var \HeaderPartFactory used to construct IHeaderPart objects
protected ZBateson\MailMimeParser\Header\Consumer\Received\GenericReceivedConsumer::$partName = NULL
  • var string the current part name being parsed.
protected ZBateson\MailMimeParser\Header\Consumer\AbstractConsumer::advanceToNextToken(Iterator $tokens, bool $isStartToken)

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

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.

  • param \Iterator $tokens The token iterator.
  • param bool $isStartToken true for the start token.
  • return static
protected ZBateson\MailMimeParser\Header\Consumer\GenericConsumer::filterIgnoredSpaces(array $parts)

Filters out ignorable spaces between parts in the passed array.

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.

  • param \HeaderPart[] $parts
  • return \HeaderPart[]
protected ZBateson\MailMimeParser\Header\Consumer\AbstractConsumer::getAllConsumers() : array

Returns this consumer and all unique sub consumers.

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

  • return \AbstractConsumer[] Array of unique consumers.
protected ZBateson\MailMimeParser\Header\Consumer\AbstractConsumer::getAllTokenSeparators() : array

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

  • return string[] Array of regular expression markers.
protected ZBateson\MailMimeParser\Header\Consumer\AbstractConsumer::getConsumerTokenParts(Iterator $tokens) : array

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
{@see \AbstractConsumer::getPartForToken()} and returns it in an array.

  • return \ZBateson\MailMimeParser\Header\IHeaderPart[]
protected ZBateson\MailMimeParser\Header\Consumer\AbstractConsumer::getPartForToken(string $token, bool $isLiteral)

Constructs and returns an IHeaderPart for the passed string token.

If the token should be ignored, the function must return null.

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

  • param string $token the token
  • param bool $isLiteral set to true if the token represents a literal -
    e.g. an escaped token
  • return \ZBateson\MailMimeParser\Header\IHeaderPart|null The constructed
    header part or null if the token should be ignored.
protected ZBateson\MailMimeParser\Header\Consumer\Received\GenericReceivedConsumer::getPartName() : string

Returns the name of the part being parsed.

This is always the lower-case name provided to the constructor, not the
actual string that started the consumer, which could be in any case.

protected ZBateson\MailMimeParser\Header\Consumer\Received\GenericReceivedConsumer::getSubConsumers() : array

Overridden to return a CommentConsumer.

  • return \ZBateson\MailMimeParser\Header\Consumer\AbstractConsumer[] the sub-consumers
protected ZBateson\MailMimeParser\Header\Consumer\AbstractConsumer::getTokenParts(Iterator $tokens) : array

Returns an array of IHeaderPart for the current token on the iterator.

If the current token is a start token from a sub-consumer, the sub-
consumer's {@see \AbstractConsumer::parseTokensIntoParts()} method is

  • param \Iterator $tokens The token iterator.
  • return \ZBateson\MailMimeParser\Header\IHeaderPart[]
protected ZBateson\MailMimeParser\Header\Consumer\Received\GenericReceivedConsumer::getTokenSeparators() : array

Returns a whitespace separator (for filtering ignorable whitespace
between parts), and a separator matching the current part name as
returned by $this->getPartName().

  • return string[] an array of regex pattern matchers
protected ZBateson\MailMimeParser\Header\Consumer\AbstractConsumer::getTokenSplitPattern() : string

Returns a regex pattern used to split the input header string.

The default implementation calls
{@see \AbstractConsumer::getAllTokenSeparators()} and implodes the
returned array with the regex OR '|' character as its glue.

  • return string the regex pattern
protected isEndToken(string $token) : bool

Overridden to return true if the passed token is a closing parenthesis.

protected ZBateson\MailMimeParser\Header\Consumer\Received\GenericReceivedConsumer::isStartToken(string $token) : bool

Returns true if the passed token matches (case-insensitively)
$this->getPartName() with optional whitespace surrounding it.

protected ZBateson\MailMimeParser\Header\Consumer\AbstractConsumer::parseTokensIntoParts(Iterator $tokens) : array

Iterates over the passed token Iterator and returns an array of parsed
IHeaderPart objects.

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

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

  • param \Iterator $tokens An iterator over a string of tokens
  • return \ZBateson\MailMimeParser\Header\IHeaderPart[] An array of
    parsed parts
protected processParts(array $parts) : array

Creates a single ReceivedDomainPart out of matched parts. If an
unmatched parenthesized expression was found, it's returned as a

  • param \ZBateson\MailMimeParser\Header\Part\HeaderPart[] $parts
  • return \ZBateson\MailMimeParser\Header\Part\ReceivedDomainPart[]|\ZBateson\MailMimeParser\Header\Part\CommentPart[]|\ZBateson\MailMimeParser\Header\Part\HeaderPart[]
protected ZBateson\MailMimeParser\Header\Consumer\AbstractConsumer::splitRawValue( $rawValue) : array

Returns an array of split tokens from the input string.

The method calls preg_split using
{@see \AbstractConsumer::getTokenSplitPattern()}. The split array will
not contain any empty parts and will contain the markers.

  • param string $rawValue the raw string
  • return array the array of tokens
private matchHostPart(string $value, ?string $hostname, ?string $address) : bool

Attempts to match a parenthesized expression to find a hostname and an
address. Returns true if the expression matched, and either hostname or
address were found.

  • param string $hostname
  • param string $address
public static ZBateson\MailMimeParser\Header\Consumer\AbstractConsumer::getInstance(ZBateson\MailMimeParser\Header\Consumer\ConsumerService $consumerService, ZBateson\MailMimeParser\Header\Part\HeaderPartFactory $partFactory)

Returns the singleton instance for the class.

© 2023 Bruce Wells
Search Namespaces \ Classes