Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: append comment to the Response element #797

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/Twilio/TwiML/TwiML.php
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,8 @@ private function buildElement(TwiML $twiml, DOMDocument $document): DOMElement {
foreach ($twiml->children as $child) {
if (\is_string($child)) {
$element->appendChild($document->createTextNode($child));
} elseif (\is_a($child, "Twilio\TwiML\Voice\Comment")) {
$element->appendChild($document->createComment($child->children[0]));
} else {
$element->appendChild($this->buildElement($child, $document));
}
Expand Down
22 changes: 22 additions & 0 deletions src/Twilio/TwiML/Voice/Comment.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

namespace Twilio\TwiML\Voice;

use Twilio\TwiML\TwiML;

class Comment extends TwiML {
/**
* Comment constructor.
*
* @param string $message Message to comment
*/
public function __construct($message) {

// "--" is not allowed in xml comments
while (strpos($message, "--") !== false) {
$message = str_replace("--", "-", $message);
}

parent::__construct('Comment', $message);
}
}
12 changes: 11 additions & 1 deletion src/Twilio/TwiML/VoiceResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -219,4 +219,14 @@ public function stop(): Voice\Stop {
public function refer($attributes = []): Voice\Refer {
return $this->nest(new Voice\Refer($attributes));
}
}

/**
* Add Comment child.
*
* @param string $message The content of the comment
* @return Voice\Comment Child element.
*/
public function comment($message): Voice\Comment {
return $this->nest(new Voice\Comment($message));
}
}
21 changes: 21 additions & 0 deletions tests/Twilio/Unit/TwiML/Voice/CommentTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

namespace TwiML\Voice;

use Twilio\Tests\Unit\TwiML\TwiMLTest;
use Twilio\TwiML\VoiceResponse;

class CommentTest extends TwiMLTest {
public function testCommentToResponse(): void {
$response = new VoiceResponse();
$response->comment('this is a comment');

$this->compareXml('<Response><!--this is a comment--></Response>', $response);
}
public function testSketchyCommentToResponse(): void {
$response = new VoiceResponse();
$response->comment('this is --- a sketchy ---- comment');

$this->compareXml('<Response><!--this is - a sketchy - comment--></Response>', $response);
}
}