Skip to content
Merged
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
93 changes: 44 additions & 49 deletions src/tdt/formatters/strategies/CSV.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ public function printHeader(){
header("Content-Type: text/csv;charset=UTF-8");
}

/**
* encloses the $element in double quotes
/**
* Encloses the $element in double quotes.
*/
private function enclose($element){
$element = rtrim($element, '"');
Expand All @@ -28,73 +28,68 @@ private function enclose($element){
}

public function printBody(){

$keys = array_keys(get_object_vars($this->objectToPrint));
$key = $keys[0];
$this->objectToPrint = $this->objectToPrint->$key;

if(!is_array($this->objectToPrint)){
throw new TDTException(500,array("CSVFormatter - You can only request CSV on an array" , array("CSV", "json", "rdf", "xml", "n3","ttl")));
$exception_config = array();
$exception_config["log_dir"] = Config::get("general", "logging", "path");
$exception_config["url"] = Config::get("general", "hostname") . Config::get("general", "subdir") . "error";
throw new TDTException(452, array("You can only request a CSV formatter on a tabular datastructure."), $exception_config);
}
if(isset($this->objectToPrint[0])){
//print the header row
$headerrow = array();
if(is_object($this->objectToPrint[0])){
$headerrow = array_keys(get_object_vars($this->objectToPrint[0]));
}else{
$headerrow = array_keys($this->objectToPrint[0]);
}

// we're going to enclose all of our fields in double quotes
$enclosedHeaderrow = array();

foreach($headerrow as $element){
array_push($enclosedHeaderrow,$this->enclose($element));
$header_printed = false;
foreach($this->objectToPrint as $row){
if(is_object($row)){
$row = get_object_vars($row);
}else if(!is_array($row)){
echo $row . "\n";
continue;
}

echo implode(";",$enclosedHeaderrow);
if(sizeof($enclosedHeaderrow) > 0){
echo "\n";
if(!$header_printed){
$i = 0;
foreach($row as $key => $value){
echo $this->enclose($key);
echo sizeof($row)-1 != $i ? ";" : "\n";
$i++;
}
$header_printed = true;
}

foreach($this->objectToPrint as $row){
if(is_object($row)){
$row = get_object_vars($row);
}else if(!is_array($row)){
echo $row . "\n";
continue;
}
$i = 0;
foreach($row as $element){

$i = 0;
foreach($row as $element){
if(is_object($element)){
if(isset($element->id)){
echo $element->id;
}else if(isset($element->name)){
echo $element->name;
}else{
echo "OBJECT";
}
}
elseif(is_array($element)){
if(isset($element["id"])){
echo $element["id"];
}else if(isset($element["name"])){
echo $element["name"];
}else{
echo "OBJECT";
}
if(is_object($element)){
if(isset($element->id)){
echo $element->id;
}else if(isset($element->name)){
echo $element->name;
}else{
echo "OBJECT";
}
else{
echo $this->enclose($element);
}
elseif(is_array($element)){
if(isset($element["id"])){
echo $element["id"];
}else if(isset($element["name"])){
echo $element["name"];
}else{
echo "OBJECT";
}
echo sizeof($row)-1 != $i ? ";" : "\n";
$i++;
}
else{
echo $this->enclose($element);
}
echo sizeof($row)-1 != $i ? ";" : "\n";
$i++;
}
}
}


public static function getDocumentation(){
return "A CSV formatter. Works only on tabular data.";
}
Expand Down
74 changes: 30 additions & 44 deletions src/tdt/formatters/strategies/XML.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@
*/

namespace tdt\formatters\strategies;

define("NUMBER_TAG_PREFIX","_");

class XML extends \tdt\formatters\AStrategy{
//make a stack of array information, always work on the last one
//for nested array support
Expand Down Expand Up @@ -49,17 +52,16 @@ public function printBody(){
$this->objectToPrint->$rootname = $wrapper;
}

$this->printObject($this->rootname . " version=\"1.0\" timestamp=\"" . time() . "\"",$this->objectToPrint->$rootname);
$this->printObject($this->rootname . " version=\"1.0\" timestamp=\"" . time() . "\"", $this->objectToPrint->$rootname);
echo "</$this->rootname>";
}

private function printObject($name,$object,$nameobject=null){

//check on first character
if(preg_match("/^[0-9]+.*/", $name)){
$name = "i" . $name; // add an i
}
$name = utf8_encode($name);
$name = NUMBER_TAG_PREFIX . $name; // add an i
}
echo "<".$name;
//If this is not an object, it must have been an empty result
//thus, we'll be returning an empty tag
Expand Down Expand Up @@ -87,19 +89,20 @@ private function printObject($name,$object,$nameobject=null){
echo ">" . htmlspecialchars($value, ENT_QUOTES);
$tag_close = TRUE;
}else{
$key = htmlspecialchars(str_replace(" ","",$key));
$key = utf8_encode($key);
$key = htmlspecialchars(str_replace(" ","",$key));

$value = htmlspecialchars($value, ENT_QUOTES);
$value = utf8_encode($value);
$value = htmlspecialchars($value, ENT_QUOTES);

if($this->isNotAnAttribute($key)){
if(!$tag_close){
echo ">";
$tag_close = TRUE;
}

echo "<$key>" . $value . "</$key>";
if(preg_match("/^[0-9]+.*/", $key)){
$key = NUMBER_TAG_PREFIX . $key; // add an i
}
echo "<".$key.">" . $value . "</$key>";
}else{
// To be discussed: strip the _ or not to strip the _
//$key = substr($key, 1);
Expand All @@ -113,70 +116,54 @@ private function printObject($name,$object,$nameobject=null){
echo ">";
}


if($name != $nameobject){
$boom = explode(" ",$name);
if(count($boom) == 1){
$name = utf8_encode($name);
if(count($boom) == 1){
echo "</$name>";
}
}

}
}

private function isNotAnAttribute($key){
//echo strpos($key,"_");
private function isNotAnAttribute($key){
return $key[0] != "_";
}

private function printArray($name,$array){
//check on first character
//check on first character
if(preg_match("/^[0-9]+.*/", $name)){
$name = "i" . $name; // add an i
$name = NUMBER_TAG_PREFIX . $name;
}
$index = 0;

if(empty($array)){
$name = utf8_encode($name);
if(empty($array)){
echo "<$name></$name>";
}

foreach($array as $key => $value){
$nametag = $name;
if(is_object($value)){
$this->printObject($nametag,$value,$name);
$name = utf8_encode($name);
$this->printObject($nametag,$value,$name);
echo "</$name>";
}else if(is_array($value) && !$this->isHash($value)){
$name = utf8_encode($name);
}else if(is_array($value) && !$this->isHash($value)){
echo "<".$name. ">";
$this->printArray($nametag,$value);
$name = utf8_encode($name);
$this->printArray($nametag,$value);
echo "</".$name.">";
}else if(is_array($value) && $this->isHash($value)){
$name = utf8_encode($name);
}else if(is_array($value) && $this->isHash($value)){
echo "<".$name. ">";
$this->printArray($key,$value);
$name = utf8_encode($name);
$this->printArray($key,$value);
echo "</".$name.">";
}else{// no array in arrays are allowed!!
$name = htmlspecialchars(str_replace(" ","",$name));
$name = utf8_encode($name);

$value = htmlspecialchars($value);
$value = utf8_encode($value);
}else{
$name = htmlspecialchars(str_replace(" ","",$name));
$value = htmlspecialchars($value);
$key = htmlspecialchars(str_replace(" ","",$key));

$key = htmlspecialchars(str_replace(" ","",$key));
$key = utf8_encode($key);

if($this->isHash($array)){
//if this is an associative array, don't print it by name of the parent
//check on first character
if($this->isHash($array)){
if(preg_match("/^[0-9]+.*/", $key)){
$key = "i" . $key; // add an i
$key = NUMBER_TAG_PREFIX . $key;
}
echo "<".$key . ">" . $value . "</".$key.">";
echo "<".$key . ">" . $value . "</".$key.">";
}else{
echo "<".$name. ">".$value."</".$name.">";
}
Expand All @@ -186,7 +173,7 @@ private function printArray($name,$array){
}
}

// check if we have an hash or a normal 'numberice array ( php doesn't know the difference btw, it just doesn't care. )
// Check if we have an hash or a normal 'numeric' array ( php doesn't know the difference btw, it just doesn't care. )
private function isHash($arr){
return array_keys($arr) !== range(0, count($arr) - 1);
}
Expand All @@ -203,5 +190,4 @@ public function printGraph() {
/* Serialize a triples array */
echo $ser->getSerializedTriples($triples);
}

}