Skip to content

Commit

Permalink
Merge pull request #4 from pietercolpaert/test-php-compliancy
Browse files Browse the repository at this point in the history
Enable support for PHP5.6
  • Loading branch information
pietercolpaert committed Apr 12, 2017
2 parents bc0344b + 7cdb699 commit 21f7a4b
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 42 deletions.
5 changes: 4 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
language: php
php:
- "7.0"
- '5.6'
- '7.0'
- '7.1'
- nightly
sudo: false
env:
before_script:
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ $writer->addPrefixes($prefixes);
//Creates blank node($predicate and/or $object are optional)
$writer->blank($predicate, $object);
//Creates rdf:list with $elements
$list = $writer->list($elements);
$list = $writer->addList($elements);

//Returns the current output it is already able to create and clear the internal memory use (useful for streaming)
$out .= $writer->read();
Expand Down
15 changes: 8 additions & 7 deletions src/N3Lexer.php
Original file line number Diff line number Diff line change
Expand Up @@ -385,18 +385,19 @@ private function unescape($item) {
return preg_replace_callback($this->escapeSequence, function ($sequence, $unicode4, $unicode8, $escapedChar) {
$charCode;
if ($unicode4) {
$charCode = parseInt($unicode4, 16);
return fromCharCode($charCode);
$charCode = intval($unicode4, 16);
return mb_convert_encoding('&#' . intval($charCode) . ';', 'UTF-8', 'HTML-ENTITIES');
}
else if ($unicode8) {
$charCode = parseInt($unicode8, 16);
if ($charCode <= 0xFFFF) return fromCharCode($charCode);
return fromCharCode(0xD800 . (($charCode -= 0x10000) / 0x400), 0xDC00 . ($charCode & 0x3FF));
$charCode = intval($unicode8, 16);
return mb_convert_encoding('&#' . intval($charCode) . ';', 'UTF-8', 'HTML-ENTITIES');
//if ($charCode <= 0xFFFF) return fromCharCode($charCode);
//return fromCharCode(0xD800 . (($charCode -= 0x10000) / 0x400), 0xDC00 . ($charCode & 0x3FF));
}
else {
$replacement = escapeReplacements[$escapedChar];
$replacement = $this->escapeReplacements[$escapedChar];
if (!$replacement)
throw new Error();
throw new \Exception();
return $replacement;
}
},$item);
Expand Down
25 changes: 14 additions & 11 deletions src/TriGWriter.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,8 @@ class TriGWriter
// Characters in literals that require escaping
CONST ESCAPE = "/[\"\\\t\n\r\f]/u"; #/u';
CONST ESCAPEALL = "/[\"\\\t\n\r\b\f]/u";
CONST ESCAPEREPLACEMENTS = [
'\\' => '\\\\', '"' => '\\"', "\t" => "\\t",
"\n" => '\\n', "\r" => "\\r", "\b"=> "\\b", "\f"=> "\\f"
];
//HHVM does not allow this to be a constant
private $ESCAPEREPLACEMENTS;

// ### `_prefixRegex` matches a prefixed name or IRI that begins with one of the added prefixes
private $prefixRegex = "/$0^/";
Expand All @@ -29,6 +27,10 @@ class TriGWriter

public function __construct($options = [])
{
$this->ESCAPEREPLACEMENTS = [
'\\' => '\\\\', '"' => '\\"', "\t" => "\\t",
"\n" => '\\n', "\r" => "\\r", "\b"=> "\\b", "\f"=> "\\f"
];
$this->initWriter ();
/* Initialize writer, depending on the format*/
$this->subject = null;
Expand All @@ -43,11 +45,11 @@ public function __construct($options = [])
}

// TODO: I think we could do without this...
$this->characterReplacer = function ($character) {
/*$this->characterReplacer = function ($character) {
// Replace a single character by its escaped version
$character = $character[0];
if (isset($character) && isset(self::ESCAPEREPLACEMENTS[$character])) {
return self::ESCAPEREPLACEMENTS[$character];
if (strlen($character) > 0 && isset(self::ESCAPEREPLACEMENTS[$character[0]])) {
return self::ESCAPEREPLACEMENTS[$character[0]];
} else {
// Replace a single character with its 4-bit unicode escape sequence
$result = "";
Expand All @@ -64,7 +66,7 @@ public function __construct($options = [])
}
return $result;
}
};
};*/
}

private function initWriter ()
Expand Down Expand Up @@ -161,8 +163,9 @@ private function encodeIriOrBlankNode ($entity) {
return $entity;
}
// Escape special characters
if (preg_match(self::ESCAPE, $entity))
$entity = preg_replace_callback(self::ESCAPEALL, $this->characterReplacer,$entity);
//if (preg_match(self::ESCAPE, $entity))
// $entity = preg_replace_callback(self::ESCAPEALL, $this->characterReplacer,$entity);

// Try to represent the IRI as prefixed name
preg_match($this->prefixRegex, $entity, $prefixMatch);
if (!isset($prefixMatch[1]) && !isset($prefixMatch[2])) {
Expand Down Expand Up @@ -349,7 +352,7 @@ public function blank ($predicate = null, $object = null) {
}

// ### `list` creates a list node with the given content
public function list ($elements = null) {
public function addList ($elements = null) {
$length = 0;
if (isset($elements)) {
$length = sizeof($elements);
Expand Down
44 changes: 22 additions & 22 deletions test/TriGWriterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,17 +81,17 @@ public function testLiterals()
$this->shouldSerialize(['a', 'b', '"c' . "\f" . 'de"'],
'<a> <b> """c' . "\f" . 'de""".' . "\n");

//should serialize a literal containing a line separator',
$this->shouldSerialize(['a', 'b', "\"c\u{2028}de\""],
'<a> <b> "c' . "\u{2028}" . 'de".' . "\n");
//should serialize a literal containing a line separator', - These tests willl not work for PHP5.6, hence commented. PHP7 only introduced the unicode escape sequence.
//$this->shouldSerialize(['a', 'b', "\"c\u{2028}de\""],
//'<a> <b> "c' . "\u{2028}" . 'de".' . "\n");

//should serialize a literal containing a paragraph separator',
$this->shouldSerialize(['a', 'b', "\"c\u{2029}de\""],
'<a> <b> "c' . "\u{2029}" .'de".' . "\n");
//$this->shouldSerialize(['a', 'b', "\"c\u{2029}de\""],
//'<a> <b> "c' . "\u{2029}" .'de".' . "\n");

//should serialize a literal containing special unicode characters',
$this->shouldSerialize(['a', 'b', "\"c\u{0000}\u{0001}\""],
'<a> <b> "c'."\u{0000}\u{0001}" . '".' . "\n");
//$this->shouldSerialize(['a', 'b', "\"c\u{0000}\u{0001}\""],
//'<a> <b> "c'."\u{0000}\u{0001}" . '".' . "\n");
}

public function testBlankNodes()
Expand Down Expand Up @@ -211,9 +211,9 @@ public function testQuads ()
'<\\U0001d400> {' . "\n" . '<\\U0001d400> <\\U0001d400> "\\U0001d400"^^<\\U0001d400>' . "\n" . '}' . "\n");
*/
//should not use escape sequences in blank nodes',
$this->shouldSerialize(["_:\u{d835}\u{dc00}", "_:\u{d835}\u{dc00}", "_:\u{d835}\u{dc00}", "_:\u{d835}\u{dc00}"],
"_:\u{d835}\u{dc00} {" . "\n" . "_:\u{d835}\u{dc00} _:\u{d835}\u{dc00} _:\u{d835}\u{dc00}" . "\n" . '}' . "\n");
}
//$this->shouldSerialize(["_:\u{d835}\u{dc00}", "_:\u{d835}\u{dc00}", "_:\u{d835}\u{dc00}", "_:\u{d835}\u{dc00}"],
//"_:\u{d835}\u{dc00} {" . "\n" . "_:\u{d835}\u{dc00} _:\u{d835}\u{dc00} _:\u{d835}\u{dc00}" . "\n" . '}' . "\n");
}

public function testCallbackOnEnd () {
//sends output through end
Expand Down Expand Up @@ -386,47 +386,47 @@ public function testLists ()
{
//should serialize triples with an empty list as object', function (done) {
$writer = new TriGWriter();
$writer->addTriple('a1', 'b', $writer->list());
$writer->addTriple('a2', 'b', $writer->list([]));
$writer->addTriple('a1', 'b', $writer->addList());
$writer->addTriple('a2', 'b', $writer->addList([]));
$writer->end(function ($error, $output) {
$this->assertEquals('<a1> <b> ().' . "\n" . '<a2> <b> ().' . "\n",$output);
});

//should serialize triples with a one-element list as object', function (done) {
$writer = new TriGWriter();
$writer->addTriple('a1', 'b', $writer->list(['c']));
$writer->addTriple('a2', 'b', $writer->list(['"c"']));
$writer->addTriple('a1', 'b', $writer->addList(['c']));
$writer->addTriple('a2', 'b', $writer->addList(['"c"']));
$writer->end(function ($error, $output) {
$this->assertEquals('<a1> <b> (<c>).' . "\n" . '<a2> <b> ("c").' . "\n",$output);
});

//should serialize triples with a three-element list as object', function (done) {
$writer = new TriGWriter();
$writer->addTriple('a1', 'b', $writer->list(['c', 'd', 'e']));
$writer->addTriple('a2', 'b', $writer->list(['"c"', '"d"', '"e"']));
$writer->addTriple('a1', 'b', $writer->addList(['c', 'd', 'e']));
$writer->addTriple('a2', 'b', $writer->addList(['"c"', '"d"', '"e"']));
$writer->end(function ($error, $output) {
$this->assertEquals('<a1> <b> (<c> <d> <e>).' . "\n" . '<a2> <b> ("c" "d" "e").' . "\n",$output);
});

//should serialize triples with an empty list as subject', function (done) {
$writer = new TriGWriter();
$writer->addTriple($writer->list(), 'b1', 'c');
$writer->addTriple($writer->list([]), 'b2', 'c');
$writer->addTriple($writer->addList(), 'b1', 'c');
$writer->addTriple($writer->addList([]), 'b2', 'c');
$writer->end(function ($error, $output) {
$this->assertEquals('() <b1> <c>;' . "\n" . ' <b2> <c>.' . "\n",$output);
});

//should serialize triples with a one-element list as subject', function (done) {
$writer = new TriGWriter();
$writer->addTriple($writer->list(['a']), 'b1', 'c');
$writer->addTriple($writer->list(['a']), 'b2', 'c');
$writer->addTriple($writer->addList(['a']), 'b1', 'c');
$writer->addTriple($writer->addList(['a']), 'b2', 'c');
$writer->end(function ($error, $output) {
$this->assertEquals('(<a>) <b1> <c>;' . "\n" . ' <b2> <c>.' . "\n",$output);
});

//should serialize triples with a three-element list as subject', function (done) {
$writer = new TriGWriter();
$writer->addTriple($writer->list(['a', '"b"', '"c"']), 'd', 'e');
$writer->addTriple($writer->addList(['a', '"b"', '"c"']), 'd', 'e');
$output = $writer->end();
$this->assertEquals('(<a> "b" "c") <d> <e>.' . "\n",$output);
}
Expand All @@ -436,7 +436,7 @@ public function testPartialRead ()
{
//should only partially output the already given data and then continue writing until end
$writer = new TriGWriter();
$writer->addTriple($writer->list(['a', '"b"', '"c"']), 'd', 'e');
$writer->addTriple($writer->addList(['a', '"b"', '"c"']), 'd', 'e');
$output = $writer->read();
$this->assertEquals('(<a> "b" "c") <d> <e>', $output);
$writer->addTriple('a', 'b', 'c');
Expand Down

0 comments on commit 21f7a4b

Please sign in to comment.