Skip to content

Commit c5e702d

Browse files
committed
Allow serializer to have a configurable line-ending
1 parent bca4974 commit c5e702d

File tree

3 files changed

+11
-4
lines changed

3 files changed

+11
-4
lines changed

examples/03-reconstituting-a-docblock.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
$docblock = $factory->create($docComment);
2121

2222
// Create the serializer that will reconstitute the DocBlock back to its original form.
23-
$serializer = new Serializer();
23+
$serializer = new Serializer(0, '', true, null, null, PHP_EOL);
2424

2525
// Reconstitution is performed by the `getDocComment()` method.
2626
$reconstitutedDocComment = $serializer->getDocComment($docblock);

examples/04-adding-your-own-tag.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -126,5 +126,5 @@ public function __toString(): string
126126

127127
// As an experiment: let's reconstitute the DocBlock and observe that because we added a __toString() method
128128
// to the tag class that we can now also see it.
129-
$serializer = new Serializer();
129+
$serializer = new Serializer(0, '',true, null, null, PHP_EOL);
130130
$reconstitutedDocComment = $serializer->getDocComment($docblock);

src/DocBlock/Serializer.php

+9-2
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,10 @@ class Serializer
4242

4343
/** @var Formatter A custom tag formatter. */
4444
protected $tagFormatter;
45+
/**
46+
* @var string
47+
*/
48+
private $lineEnding;
4549

4650
/**
4751
* Create a Serializer instance.
@@ -51,19 +55,22 @@ class Serializer
5155
* @param bool $indentFirstLine Whether to indent the first line.
5256
* @param int|null $lineLength The max length of a line or NULL to disable line wrapping.
5357
* @param Formatter $tagFormatter A custom tag formatter, defaults to PassthroughFormatter.
58+
* @param string $lineEnding Line ending used in the output, by default \n is used.
5459
*/
5560
public function __construct(
5661
int $indent = 0,
5762
string $indentString = ' ',
5863
bool $indentFirstLine = true,
5964
?int $lineLength = null,
60-
?Formatter $tagFormatter = null
65+
?Formatter $tagFormatter = null,
66+
string $lineEnding = "\n"
6167
) {
6268
$this->indent = $indent;
6369
$this->indentString = $indentString;
6470
$this->isFirstLineIndented = $indentFirstLine;
6571
$this->lineLength = $lineLength;
6672
$this->tagFormatter = $tagFormatter ?: new PassthroughFormatter();
73+
$this->lineEnding = $lineEnding;
6774
}
6875

6976
/**
@@ -96,7 +103,7 @@ public function getDocComment(DocBlock $docblock): string
96103

97104
$comment = $this->addTagBlock($docblock, $wrapLength, $indent, $comment);
98105

99-
return $comment . $indent . ' */';
106+
return str_replace("\n", $this->lineEnding, $comment . $indent . ' */');
100107
}
101108

102109
private function removeTrailingSpaces(string $indent, string $text): string

0 commit comments

Comments
 (0)