Example #1
0
 /**
  * @param  FormatterData $data
  * @return mixed
  *
  * This method must use a FormatterData object and output
  * a formatted value.
  */
 public function encode(FormatterData $data)
 {
     $arrayData = $data->getData();
     $domDocument = new \DOMDocument("1.0");
     $container = $domDocument->appendChild(new \DOMElement($this->root));
     foreach ($arrayData as $key => $entry) {
         if (is_array($entry)) {
             $node = $container->appendChild(new \DOMElement($this->rowName));
             $this->recursiveBuild($entry, $node);
         } else {
             $node = new \DOMElement($this->nodeName);
             $container->appendChild($node);
             /** @var \DOMElement $lastChild */
             $lastChild = $container->lastChild;
             $lastChild->setAttribute("name", $key);
             $lastChild->setAttribute("value", $entry);
         }
     }
     $domDocument->preserveWhiteSpace = false;
     $domDocument->formatOutput = true;
     return $domDocument->saveXML();
 }
Example #2
0
 /**
  * @param  FormatterData $data
  * @return mixed
  *
  * This method must use a FormatterData object and output
  * a formatted value.
  */
 public function encode(FormatterData $data)
 {
     $string = "";
     /**
      * Get the first row and delimiters lengths
      */
     $firstRow = $data->getRow();
     $delimiterLength = strlen($this->delimiter);
     $lineReturnLength = strlen($this->lineReturn);
     if ($firstRow === false) {
         return "";
     }
     /**
      * check if $this->order doesn't have non-existing rows
      */
     $this->checkOrders($firstRow);
     $rawKeys = array_keys($firstRow);
     $keys = [];
     foreach ($rawKeys as $key) {
         $keys[$key] = $key;
     }
     $values = $data->getData();
     array_unshift($values, $keys);
     while (null !== ($row = array_shift($values))) {
         /**
          * First put the sorted ones
          */
         foreach ($this->order as $order) {
             $string .= $this->formatField($row[$order]);
             unset($row[$order]);
         }
         /**
          * Then place the fields,
          * order by name
          */
         ksort($row);
         foreach ($keys as $key) {
             if (array_key_exists($key, $row)) {
                 $string .= $this->formatField($row[$key]);
             }
         }
         $string = substr($string, 0, -$delimiterLength) . $this->lineReturn;
     }
     return substr($string, 0, -$lineReturnLength);
 }
Example #3
0
 public function testAddRow()
 {
     $data = new FormatterData();
     $row = ["title" => "A super book", "author" => "Manu"];
     $data->addRow($row);
     $this->assertEquals([$row], $data->getData());
     $this->assertEquals($row, $data->getRow());
 }
Example #4
0
 /**
  * @param  FormatterData $data
  * @return mixed
  *
  * This method must use a FormatterData object and output
  * a formatted value.
  */
 public function encode(FormatterData $data)
 {
     return json_encode($data->getData());
 }