Allow serializer to have a configurable line-ending

This commit is contained in:
Jaapio
2021-08-26 22:31:30 +02:00
parent bca4974b0b
commit c5e702d299
3 changed files with 11 additions and 4 deletions
+1 -1
View File
@@ -20,7 +20,7 @@ $factory = DocBlockFactory::createInstance();
$docblock = $factory->create($docComment); $docblock = $factory->create($docComment);
// Create the serializer that will reconstitute the DocBlock back to its original form. // Create the serializer that will reconstitute the DocBlock back to its original form.
$serializer = new Serializer(); $serializer = new Serializer(0, '', true, null, null, PHP_EOL);
// Reconstitution is performed by the `getDocComment()` method. // Reconstitution is performed by the `getDocComment()` method.
$reconstitutedDocComment = $serializer->getDocComment($docblock); $reconstitutedDocComment = $serializer->getDocComment($docblock);
+1 -1
View File
@@ -126,5 +126,5 @@ $customTagObjects = $docblock->getTagsByName('my-tag');
// As an experiment: let's reconstitute the DocBlock and observe that because we added a __toString() method // As an experiment: let's reconstitute the DocBlock and observe that because we added a __toString() method
// to the tag class that we can now also see it. // to the tag class that we can now also see it.
$serializer = new Serializer(); $serializer = new Serializer(0, '',true, null, null, PHP_EOL);
$reconstitutedDocComment = $serializer->getDocComment($docblock); $reconstitutedDocComment = $serializer->getDocComment($docblock);
+9 -2
View File
@@ -42,6 +42,10 @@ class Serializer
/** @var Formatter A custom tag formatter. */ /** @var Formatter A custom tag formatter. */
protected $tagFormatter; protected $tagFormatter;
/**
* @var string
*/
private $lineEnding;
/** /**
* Create a Serializer instance. * Create a Serializer instance.
@@ -51,19 +55,22 @@ class Serializer
* @param bool $indentFirstLine Whether to indent the first line. * @param bool $indentFirstLine Whether to indent the first line.
* @param int|null $lineLength The max length of a line or NULL to disable line wrapping. * @param int|null $lineLength The max length of a line or NULL to disable line wrapping.
* @param Formatter $tagFormatter A custom tag formatter, defaults to PassthroughFormatter. * @param Formatter $tagFormatter A custom tag formatter, defaults to PassthroughFormatter.
* @param string $lineEnding Line ending used in the output, by default \n is used.
*/ */
public function __construct( public function __construct(
int $indent = 0, int $indent = 0,
string $indentString = ' ', string $indentString = ' ',
bool $indentFirstLine = true, bool $indentFirstLine = true,
?int $lineLength = null, ?int $lineLength = null,
?Formatter $tagFormatter = null ?Formatter $tagFormatter = null,
string $lineEnding = "\n"
) { ) {
$this->indent = $indent; $this->indent = $indent;
$this->indentString = $indentString; $this->indentString = $indentString;
$this->isFirstLineIndented = $indentFirstLine; $this->isFirstLineIndented = $indentFirstLine;
$this->lineLength = $lineLength; $this->lineLength = $lineLength;
$this->tagFormatter = $tagFormatter ?: new PassthroughFormatter(); $this->tagFormatter = $tagFormatter ?: new PassthroughFormatter();
$this->lineEnding = $lineEnding;
} }
/** /**
@@ -96,7 +103,7 @@ class Serializer
$comment = $this->addTagBlock($docblock, $wrapLength, $indent, $comment); $comment = $this->addTagBlock($docblock, $wrapLength, $indent, $comment);
return $comment . $indent . ' */'; return str_replace("\n", $this->lineEnding, $comment . $indent . ' */');
} }
private function removeTrailingSpaces(string $indent, string $text): string private function removeTrailingSpaces(string $indent, string $text): string