ILIAS  trunk Revision v11.0_alpha-3011-gc6b235a2e85
class.ilXMLResultSet.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
28{
29 private array $colspecs = [];
30 private array $rows = [];
31
32 public function getColumnName(int $index): ?string
33 {
34 if ($index < 0 || $index > count($this->colspecs)) {
35 return null;
36 }
37 return $this->colspecs[$index] instanceof ilXMLResultSetColumn ? $this->colspecs[$index]->getName() : null;
38 }
39
43 public function addColumn(string $columnname): void
44 {
45 $this->colspecs[] = new ilXMLResultSetColumn(count($this->colspecs), $columnname);
46 }
47
51 public function getIndexForColumn(string $columnname): int
52 {
53 $idx = 0;
54 foreach ($this->colspecs as $colspec) {
55 if (strcasecmp($columnname, $colspec->getName()) === 0) {
56 return $idx;
57 }
58 $idx++;
59 }
60 return -1;
61 }
62
66 public function hasColumn(string $columnname): bool
67 {
68 return $this->getIndexForColumn($columnname) !== -1;
69 }
70
75 public function getColSpecs(): array
76 {
77 return $this->colspecs;
78 }
79
84 public function getRows(): array
85 {
86 return $this->rows;
87 }
88
89 public function addRow(ilXMLResultSetRow $row): void
90 {
91 $this->rows[] = $row;
92 }
93
101 public function setArray(array $array): void
102 {
103 $this->addArray($array, true);
104 }
105
115 public function addArray(array $array, bool $overwrite = false): void
116 {
117 if ($overwrite) {
118 $this->clear();
119 }
120 foreach ($array as $row) {
121 if ($overwrite) {
122 // add column names from first row
123 $columnNames = array_keys($row);
124 foreach ($columnNames as $columnName) {
125 $this->addColumn($columnName);
126 }
127 $overwrite = false;
128 }
129 $xmlRow = new ilXMLResultSetRow();
130 $xmlRow->setValues($row);
131 $this->addRow($xmlRow);
132 }
133 }
134
135 public function clear(): void
136 {
137 $this->rows = array();
138 $this->colspecs = array();
139 }
140
141 public function getColumnCount(): int
142 {
143 return count($this->colspecs);
144 }
145
146 public function getRowCount(): int
147 {
148 return count($this->rows);
149 }
150
154 public function getRow($idx): ilXMLResultSetRow
155 {
156 if ($idx < 0 || $idx >= $this->getRowCount()) {
157 throw new DomainException("Index too small or too big: " . $idx);
158 }
159 return $this->rows[$idx];
160 }
161
168 public function getValue(int $rowIdx, $colIdx): string
169 {
170 $row = $this->getRow($rowIdx);
171
172 if (!is_numeric($colIdx)) {
173 $colIdx = $this->getIndexForColumn($colIdx);
174 }
175 return $row->getValue($colIdx);
176 }
177}
Column Class for XMLResultSet.
Row Class for XMLResultSet.
setArray(array $array)
Clear table value and sets them based on array.
addColumn(string $columnname)
create a new column with columnname and attach it to column list
getRow($idx)
return row for index idx
getColumnName(int $index)
getIndexForColumn(string $columnname)
return index for column name
getRows()
return array of ilXMLResultSetRow
addRow(ilXMLResultSetRow $row)
addArray(array $array, bool $overwrite=false)
Add table values.
getValue(int $rowIdx, $colIdx)
return column value at colidx and rowidx
getColSpecs()
return array of ilXMLResultSetColumn
hasColumn(string $columnname)
has column name