1 | <?php |
||
21 | class Serializer |
||
22 | { |
||
23 | /** @var string The string to indent the comment with. */ |
||
24 | protected $indentString = ' '; |
||
25 | |||
26 | /** @var int The number of times the indent string is repeated. */ |
||
27 | protected $indent = 0; |
||
28 | |||
29 | /** @var bool Whether to indent the first line with the given indent amount and string. */ |
||
30 | protected $isFirstLineIndented = true; |
||
31 | |||
32 | /** @var int|null The max length of a line. */ |
||
33 | protected $lineLength = null; |
||
34 | |||
35 | /** |
||
36 | * Create a Serializer instance. |
||
37 | * |
||
38 | * @param int $indent The number of times the indent string is repeated. |
||
39 | * @param string $indentString The string to indent the comment with. |
||
40 | * @param bool $indentFirstLine Whether to indent the first line. |
||
41 | * @param int|null $lineLength The max length of a line or NULL to disable line wrapping. |
||
42 | */ |
||
43 | 8 | public function __construct($indent = 0, $indentString = ' ', $indentFirstLine = true, $lineLength = null) |
|
44 | { |
||
45 | 8 | Assert::integer($indent); |
|
46 | 7 | Assert::string($indentString); |
|
47 | 6 | Assert::boolean($indentFirstLine); |
|
48 | 5 | Assert::nullOrInteger($lineLength); |
|
49 | |||
50 | 4 | $this->indent = $indent; |
|
51 | 4 | $this->indentString = $indentString; |
|
52 | 4 | $this->isFirstLineIndented = $indentFirstLine; |
|
53 | 4 | $this->lineLength = $lineLength; |
|
54 | 4 | } |
|
55 | |||
56 | /** |
||
57 | * Generate a DocBlock comment. |
||
58 | * |
||
59 | * @param DocBlock $docblock The DocBlock to serialize. |
||
60 | * |
||
61 | * @return string The serialized doc block. |
||
62 | */ |
||
63 | 4 | public function getDocComment(DocBlock $docblock) |
|
64 | { |
||
65 | 4 | $indent = str_repeat($this->indentString, $this->indent); |
|
66 | 4 | $firstIndent = $this->isFirstLineIndented ? $indent : ''; |
|
67 | // 3 === strlen(' * ') |
||
68 | 4 | $wrapLength = $this->lineLength ? $this->lineLength - strlen($indent) - 3 : null; |
|
69 | |||
70 | 4 | $text = $this->removeTrailingSpaces( |
|
71 | 4 | $indent, |
|
72 | 4 | $this->addAsterisksForEachLine( |
|
73 | 4 | $indent, |
|
74 | 4 | $this->getSummaryAndDescriptionTextBlock($docblock, $wrapLength) |
|
75 | 4 | ) |
|
76 | 4 | ); |
|
77 | |||
78 | 4 | $comment = "{$firstIndent}/**\n{$indent} * {$text}\n{$indent} *\n"; |
|
79 | 4 | $comment = $this->addTagBlock($docblock, $wrapLength, $indent, $comment); |
|
80 | 4 | $comment .= $indent . ' */'; |
|
81 | |||
82 | 4 | return $comment; |
|
83 | } |
||
84 | |||
85 | /** |
||
86 | * @param $indent |
||
87 | * @param $text |
||
88 | * @return mixed |
||
89 | */ |
||
90 | 4 | private function removeTrailingSpaces($indent, $text) |
|
94 | |||
95 | /** |
||
96 | * @param $indent |
||
97 | * @param $text |
||
98 | * @return mixed |
||
99 | */ |
||
100 | 4 | private function addAsterisksForEachLine($indent, $text) |
|
104 | |||
105 | /** |
||
106 | * @param DocBlock $docblock |
||
107 | * @param $wrapLength |
||
108 | * @return string |
||
109 | */ |
||
110 | 4 | private function getSummaryAndDescriptionTextBlock(DocBlock $docblock, $wrapLength) |
|
120 | |||
121 | /** |
||
122 | * @param DocBlock $docblock |
||
123 | * @param $wrapLength |
||
124 | * @param $indent |
||
125 | * @param $comment |
||
126 | * @return string |
||
127 | */ |
||
128 | 4 | private function addTagBlock(DocBlock $docblock, $wrapLength, $indent, $comment) |
|
143 | } |
||
144 |