Skip to content

Commit

Permalink
Added support to literal strings in TomlBuilder
Browse files Browse the repository at this point in the history
  • Loading branch information
yosymfony committed Mar 7, 2015
2 parents 9d70455 + 4ca73ce commit c36f603
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 3 deletions.
7 changes: 5 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
CHANGELOG
=========
0.3.1 (2015-03-07)
------------------
* Added support to literal strings in TomlBuilder.

0.3.0
-----
0.3.0 (2015-03-06)
------------------
* Support for TOML 0.4.0.
* CS fixes.

Expand Down
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ You can create inline TOML string with TomlBuilder. TomlBuilder uses Fluent inte
->addValue('name', "Toml", 'This is your name')
->addValue('newline', "This string has a \n new line character.")
->addValue('winPath', "C:\\Users\\nodejs\\templates")
->addValue('literal', '@<\i\c*\s*>') // literals starts with '@'.
->addValue('unicode', 'unicode character: ' . json_decode('"\u03B4"'))

->addTable('data.bool')
Expand Down Expand Up @@ -120,6 +121,7 @@ The result of this example:
name = "Toml" #This is your name
newline = "This string has a \n new line character."
winPath = "C:\\Users\\nodejs\\templates"
literal = '<\i\c*\s*>'
unicode = "unicode character: δ"

[data.bool]
Expand Down
7 changes: 6 additions & 1 deletion src/TomlBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,10 @@ private function dumpValue($val)

private function dumpString($val)
{
if (0 === strpos($val, '@')) {
return "'".preg_replace('/@/', '', $val, 1)."'";
}

$normalized = $this->normalizeString($val);

if (false === $this->isStringValid($normalized)) {
Expand Down Expand Up @@ -257,7 +261,7 @@ private function dumpComment($val)

private function dumpDatetime($val)
{
return $val->format('Y-m-d\TH:i:s\Z'); // Only full zulu form
return $val->format('Y-m-d\TH:i:s\Z'); // ZULU form
}

private function dumpInteger($val)
Expand Down Expand Up @@ -418,6 +422,7 @@ private function isStringValid($val)

$noSpecialCharacter = str_replace($allowed, '', $val);
$noSpecialCharacter = preg_replace('/\\\\u([0-9a-fA-F]{4})/', '', $noSpecialCharacter);
$noSpecialCharacter = preg_replace('/\\\\u([0-9a-fA-F]{8})/', '', $noSpecialCharacter);

$pos = strpos($noSpecialCharacter, '\\');

Expand Down
28 changes: 28 additions & 0 deletions tests/TomlBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,34 @@ public function testStringEscapesDoubleQuote()
$this->assertNotNull(Toml::Parse($result));
}

public function testKeyLiteralString()
{
$tb = new TomlBuilder();

$result = $tb->addValue('regex', "@<\i\c*\s*>")
->getTomlString();

$array = Toml::Parse($result);

$this->assertNotNull($array);

$this->assertEquals('<\i\c*\s*>', $array['regex']);
}

public function testKeyLiteralStringEscapingAt()
{
$tb = new TomlBuilder();

$result = $tb->addValue('regex', "@@<\i\c*\s*>")
->getTomlString();

$array = Toml::Parse($result);

$this->assertNotNull($array);

$this->assertEquals('@<\i\c*\s*>', $array['regex']);
}

public function testKeySpecialChars()
{
$tb = new TomlBuilder();
Expand Down

0 comments on commit c36f603

Please sign in to comment.