Swift_Mime_Headers_AbstractHeader::createPhrase PHP Method

createPhrase() protected method

Produces a compliant, formatted RFC 2822 'phrase' based on the string given.
protected createPhrase ( Swift_Mime_Header $header, string $string, string $charset, Swift_Mime_HeaderEncoder $encoder = null, boolean $shorten = false ) : string
$header Swift_Mime_Header
$string string as displayed
$charset string of the text
$encoder Swift_Mime_HeaderEncoder
$shorten boolean the first line to make remove for header name
return string
    protected function createPhrase(Swift_Mime_Header $header, $string, $charset, Swift_Mime_HeaderEncoder $encoder = null, $shorten = false)
    {
        // Treat token as exactly what was given
        $phraseStr = $string;
        // If it's not valid
        if (!preg_match('/^' . $this->getGrammar()->getDefinition('phrase') . '$/D', $phraseStr)) {
            // .. but it is just ascii text, try escaping some characters
            // and make it a quoted-string
            if (preg_match('/^' . $this->getGrammar()->getDefinition('text') . '*$/D', $phraseStr)) {
                $phraseStr = $this->getGrammar()->escapeSpecials($phraseStr, array('"'), $this->getGrammar()->getSpecials());
                $phraseStr = '"' . $phraseStr . '"';
            } else {
                // ... otherwise it needs encoding
                // Determine space remaining on line if first line
                if ($shorten) {
                    $usedLength = strlen($header->getFieldName() . ': ');
                } else {
                    $usedLength = 0;
                }
                $phraseStr = $this->encodeWords($header, $string, $usedLength);
            }
        }
        return $phraseStr;
    }