for testing and deploying your application
for finding and fixing issues
for empowering human code reviews
<?php
declare(strict_types=1);
namespace Genkgo\Mail\Dkim;
use Genkgo\Mail\StreamInterface;
final class CanonicalizeBodyRelaxed implements CanonicalizeBodyInterface
{
/**
* @param StreamInterface $body
* @return string
*/
public function canonicalize(StreamInterface $body): string
$string = (string)$body;
if ($string === '') {
return $string;
}
$length = strlen($string);
$canon = '';
$lastChar = null;
$space = false;
$line = '';
$emptyCounter = 0;
for ($i = 0; $i < $length; ++$i) {
switch ($string[$i]) {
case "\r":
$lastChar = "\r";
break;
case "\n":
if ($lastChar === "\r") {
if ($line === '') {
++$emptyCounter;
} else {
$canon .= "\r\n";
throw new \RuntimeException('This should never happen');
case ' ':
case "\t":
$space = true;
default:
if ($emptyCounter > 0) {
$canon .= str_repeat("\r\n", $emptyCounter);
if ($space) {
$line .= ' ';
$canon .= ' ';
$line .= $string[$i];
$canon .= $string[$i];
return rtrim($canon, "\r\n") . "\r\n";
public function name(): string
return 'relaxed';