class Serializer
Same name in this branch
- 11.1.x vendor/symfony/serializer/Serializer.php \Symfony\Component\Serializer\Serializer
- 11.1.x core/modules/jsonapi/src/Serializer/Serializer.php \Drupal\jsonapi\Serializer\Serializer
- 11.1.x core/modules/rest/src/Plugin/views/style/Serializer.php \Drupal\rest\Plugin\views\style\Serializer
Converts a DocBlock back from an object to a complete DocComment including Asterisks.
Hierarchy
- class \phpDocumentor\Reflection\DocBlock\Serializer
Expanded class hierarchy of Serializer
9 string references to 'Serializer'
- RegisterSerializationClassesCompilerPass::process in core/
modules/ serialization/ src/ RegisterSerializationClassesCompilerPass.php - You can modify the container here before it is dumped to PHP code.
- RequestHandler::create in core/
modules/ rest/ src/ RequestHandler.php - Instantiates a new instance of this class.
- RestExport::defineOptions in core/
modules/ rest/ src/ Plugin/ views/ display/ RestExport.php - Overrides \Drupal\views\Plugin\views\display\DisplayPluginBase:defineOptions().
- Serializer::create in core/
modules/ rest/ src/ Plugin/ views/ style/ Serializer.php - Creates an instance of the plugin.
- SerializerDataCollector::getName in vendor/
symfony/ serializer/ DataCollector/ SerializerDataCollector.php - Returns the name of the collector.
File
-
vendor/
phpdocumentor/ reflection-docblock/ src/ DocBlock/ Serializer.php, line 29
Namespace
phpDocumentor\Reflection\DocBlockView source
class Serializer {
/** @var string The string to indent the comment with. */
protected string $indentString = ' ';
/** @var int The number of times the indent string is repeated. */
protected int $indent = 0;
/** @var bool Whether to indent the first line with the given indent amount and string. */
protected bool $isFirstLineIndented = true;
/** @var int|null The max length of a line. */
protected ?int $lineLength = null;
/** @var Formatter A custom tag formatter. */
protected Formatter $tagFormatter;
private string $lineEnding;
/**
* Create a Serializer instance.
*
* @param int $indent The number of times the indent string is repeated.
* @param string $indentString The string to indent the comment with.
* @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 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(int $indent = 0, string $indentString = ' ', bool $indentFirstLine = true, ?int $lineLength = null, ?Formatter $tagFormatter = null, string $lineEnding = "\n") {
$this->indent = $indent;
$this->indentString = $indentString;
$this->isFirstLineIndented = $indentFirstLine;
$this->lineLength = $lineLength;
$this->tagFormatter = $tagFormatter ?: new PassthroughFormatter();
$this->lineEnding = $lineEnding;
}
/**
* Generate a DocBlock comment.
*
* @param DocBlock $docblock The DocBlock to serialize.
*
* @return string The serialized doc block.
*/
public function getDocComment(DocBlock $docblock) : string {
$indent = str_repeat($this->indentString, $this->indent);
$firstIndent = $this->isFirstLineIndented ? $indent : '';
// 3 === strlen(' * ')
$wrapLength = $this->lineLength !== null ? $this->lineLength - strlen($indent) - 3 : null;
$text = $this->removeTrailingSpaces($indent, $this->addAsterisksForEachLine($indent, $this->getSummaryAndDescriptionTextBlock($docblock, $wrapLength)));
$comment = $firstIndent . "/**\n";
if ($text) {
$comment .= $indent . ' * ' . $text . "\n";
$comment .= $indent . " *\n";
}
$comment = $this->addTagBlock($docblock, $wrapLength, $indent, $comment);
return str_replace("\n", $this->lineEnding, $comment . $indent . ' */');
}
private function removeTrailingSpaces(string $indent, string $text) : string {
return str_replace(sprintf("\n%s * \n", $indent), sprintf("\n%s *\n", $indent), $text);
}
private function addAsterisksForEachLine(string $indent, string $text) : string {
return str_replace("\n", sprintf("\n%s * ", $indent), $text);
}
private function getSummaryAndDescriptionTextBlock(DocBlock $docblock, ?int $wrapLength) : string {
$text = $docblock->getSummary() . ((string) $docblock->getDescription() ? "\n\n" . $docblock->getDescription() : '');
if ($wrapLength !== null) {
$text = wordwrap($text, $wrapLength);
return $text;
}
return $text;
}
private function addTagBlock(DocBlock $docblock, ?int $wrapLength, string $indent, string $comment) : string {
foreach ($docblock->getTags() as $tag) {
$tagText = $this->tagFormatter
->format($tag);
if ($wrapLength !== null) {
$tagText = wordwrap($tagText, $wrapLength);
}
$tagText = str_replace("\n", sprintf("\n%s * ", $indent), $tagText);
$comment .= sprintf("%s * %s\n", $indent, $tagText);
}
return $comment;
}
}
Members
Title Sort descending | Modifiers | Object type | Summary |
---|---|---|---|
Serializer::$indent | protected | property | @var int The number of times the indent string is repeated. |
Serializer::$indentString | protected | property | @var string The string to indent the comment with. |
Serializer::$isFirstLineIndented | protected | property | @var bool Whether to indent the first line with the given indent amount and string. |
Serializer::$lineEnding | private | property | |
Serializer::$lineLength | protected | property | @var int|null The max length of a line. |
Serializer::$tagFormatter | protected | property | @var Formatter A custom tag formatter. |
Serializer::addAsterisksForEachLine | private | function | |
Serializer::addTagBlock | private | function | |
Serializer::getDocComment | public | function | Generate a DocBlock comment. |
Serializer::getSummaryAndDescriptionTextBlock | private | function | |
Serializer::removeTrailingSpaces | private | function | |
Serializer::__construct | public | function | Create a Serializer instance. |