ILIAS  release_8 Revision v8.19
All Data Structures Namespaces Files Functions Variables Modules Pages
class.ilCSVWriter.php
Go to the documentation of this file.
1 <?php
2 
3 /******************************************************************************
4  *
5  * This file is part of ILIAS, a powerful learning management system.
6  *
7  * ILIAS is licensed with the GPL-3.0, you should have received a copy
8  * of said license along with the source code.
9  *
10  * If this is not the case or you just want to try ILIAS, you'll find
11  * us at:
12  * https://www.ilias.de
13  * https://github.com/ILIAS-eLearning
14  *
15  *****************************************************************************/
16 
17 
19 {
20  private string $csv = '';
21  private string $separator = ',';
22  private string $delimiter = '"';
23  private string $new_line = "\n";
24  private bool $do_utf8_decoding = false;
25  private bool $first_entry = true;
26 
27  public function setSeparator(string $a_sep): void
28  {
29  $this->separator = $a_sep;
30  }
31 
32  public function setDelimiter(string $a_del): void
33  {
34  $this->delimiter = $a_del;
35  }
36 
37  public function addRow(): void
38  {
39  $this->csv .= $this->new_line;
40  $this->first_entry = true;
41  }
42 
43  public function setDoUTF8Decoding($do_utf8_decoding): void
44  {
45  $this->do_utf8_decoding = (bool) $do_utf8_decoding;
46  }
47 
48  public function addColumn(string $a_col): void
49  {
50  if (!$this->first_entry) {
51  $this->csv .= $this->separator;
52  }
53  $this->csv .= $this->delimiter;
54  $this->csv .= $this->quote($a_col);
55  $this->csv .= $this->delimiter;
56  $this->first_entry = false;
57  }
58 
59  public function getCSVString(): string
60  {
61  return $this->csv;
62  }
63 
64  private function quote(string $a_str): string
65  {
66  return str_replace(
67  $this->delimiter,
68  $this->delimiter . $this->delimiter,
69  ($this->do_utf8_decoding) ? utf8_decode($a_str) : $a_str
70  );
71  }
72 }
addColumn(string $a_col)
setSeparator(string $a_sep)
setDelimiter(string $a_del)
quote(string $a_str)
setDoUTF8Decoding($do_utf8_decoding)