1: <?php
2: namespace Pharborist;
3:
4: 5: 6: 7: 8: 9: 10: 11:
12: class LineCommentBlockNode extends ParentNode {
13: use UncommentTrait;
14:
15: 16: 17: 18: 19: 20: 21: 22:
23: public static function create($comment) {
24: $block_comment = new LineCommentBlockNode();
25: $comment = trim($comment);
26: $lines = array_map('rtrim', explode("\n", $comment));
27: foreach ($lines as $line) {
28: $comment_node = new CommentNode(T_COMMENT, '// ' . $line . "\n");
29: $block_comment->addChild($comment_node);
30: }
31: return $block_comment;
32: }
33:
34: 35: 36:
37: public function getCommentText() {
38: $comment = '';
39: $child = $this->firstChild();
40: $first = TRUE;
41: while ($child) {
42: if ($child instanceof CommentNode) {
43: if ($first) {
44: $first = FALSE;
45: }
46: else {
47: $comment .= "\n";
48: }
49: $comment .= $child->getCommentText();
50: }
51: $child = $child->next;
52: }
53: return $comment;
54: }
55:
56: 57: 58: 59: 60: 61: 62:
63: public function setIndent($indent) {
64:
65: $this->removeIndent();
66:
67: $this->addIndent($indent);
68: return $this;
69: }
70:
71: 72: 73: 74: 75:
76: public function removeIndent() {
77: $this->children(function (Node $node) {
78: return !($node instanceof CommentNode);
79: })->remove();
80: return $this;
81: }
82:
83: 84: 85: 86: 87: 88: 89:
90: public function addIndent($whitespace) {
91: $has_indent = $this->children(function (Node $node) {
92: return !($node instanceof CommentNode);
93: })->count() > 0;
94: if ($has_indent) {
95: $this->children(Filter::isInstanceOf('\Pharborist\WhitespaceNode'))->each(function (WhitespaceNode $ws_node) use ($whitespace) {
96: $ws_node->setText($ws_node->getText() . $whitespace);
97: });
98: }
99: else {
100: $this->children()->before(Token::whitespace($whitespace));
101: }
102: return $this;
103: }
104: }
105: