ILIAS  release_9 Revision v9.13-25-g2c18ec4c24f
class.ilXMLResultSet.php
Go to the documentation of this file.
1 <?php
2 
3 declare(strict_types=1);
4 
5 /*
6  +-----------------------------------------------------------------------------+
7  | ILIAS open source |
8  +-----------------------------------------------------------------------------+
9  | Copyright (c) 1998-2001 ILIAS open source, University of Cologne |
10  | |
11  | This program is free software; you can redistribute it and/or |
12  | modify it under the terms of the GNU General Public License |
13  | as published by the Free Software Foundation; either version 2 |
14  | of the License, or (at your option) any later version. |
15  | |
16  | This program is distributed in the hope that it will be useful, |
17  | but WITHOUT ANY WARRANTY; without even the implied warranty of |
18  | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
19  | GNU General Public License for more details. |
20  | |
21  | You should have received a copy of the GNU General Public License |
22  | along with this program; if not, write to the Free Software |
23  | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
24  +-----------------------------------------------------------------------------+
25 */
26 
27 include_once './webservice/soap/classes/class.ilXMLResultSetColumn.php';
28 include_once './webservice/soap/classes/class.ilXMLResultSetRow.php';
29 
37 {
38  private array $colspecs = [];
39  private array $rows = [];
40 
41  public function getColumnName(int $index): ?string
42  {
43  if ($index < 0 || $index > count($this->colspecs)) {
44  return null;
45  }
46  return $this->colspecs[$index] instanceof ilXMLResultSetColumn ? $this->colspecs[$index]->getName() : null;
47  }
48 
52  public function addColumn(string $columnname): void
53  {
54  $this->colspecs[] = new ilXMLResultSetColumn(count($this->colspecs), $columnname);
55  }
56 
60  public function getIndexForColumn(string $columnname): int
61  {
62  $idx = 0;
63  foreach ($this->colspecs as $colspec) {
64  if (strcasecmp($columnname, $colspec->getName()) === 0) {
65  return $idx;
66  }
67  $idx++;
68  }
69  return -1;
70  }
71 
75  public function hasColumn(string $columnname): bool
76  {
77  return $this->getIndexForColumn($columnname) !== -1;
78  }
79 
84  public function getColSpecs(): array
85  {
86  return $this->colspecs;
87  }
88 
93  public function getRows(): array
94  {
95  return $this->rows;
96  }
97 
98  public function addRow(ilXMLResultSetRow $row): void
99  {
100  $this->rows[] = $row;
101  }
102 
110  public function setArray(array $array): void
111  {
112  $this->addArray($array, true);
113  }
114 
124  public function addArray(array $array, bool $overwrite = false): void
125  {
126  if ($overwrite) {
127  $this->clear();
128  }
129  foreach ($array as $row) {
130  if ($overwrite) {
131  // add column names from first row
132  $columnNames = array_keys($row);
133  foreach ($columnNames as $columnName) {
134  $this->addColumn($columnName);
135  }
136  $overwrite = false;
137  }
138  $xmlRow = new ilXMLResultSetRow();
139  $xmlRow->setValues($row);
140  $this->addRow($xmlRow);
141  }
142  }
143 
144  public function clear(): void
145  {
146  $this->rows = array();
147  $this->colspecs = array();
148  }
149 
150  public function getColumnCount(): int
151  {
152  return count($this->colspecs);
153  }
154 
155  public function getRowCount(): int
156  {
157  return count($this->rows);
158  }
159 
163  public function getRow($idx): ilXMLResultSetRow
164  {
165  if ($idx < 0 || $idx >= $this->getRowCount()) {
166  throw new DomainException("Index too small or too big: " . $idx);
167  }
168  return $this->rows[$idx];
169  }
170 
177  public function getValue(int $rowIdx, $colIdx): string
178  {
179  $row = $this->getRow($rowIdx);
180 
181  if (!is_numeric($colIdx)) {
182  $colIdx = $this->getIndexForColumn($colIdx);
183  }
184  return $row->getValue($colIdx);
185  }
186 }
addArray(array $array, bool $overwrite=false)
Add table values.
Row Class for XMLResultSet.
addColumn(string $columnname)
create a new column with columnname and attach it to column list
getColumnName(int $index)
getColSpecs()
return array of ilXMLResultSetColumn
getValue(int $rowIdx, $colIdx)
return column value at colidx and rowidx
Column Class for XMLResultSet.
getRow($idx)
return row for index idx
getIndexForColumn(string $columnname)
return index for column name
setArray(array $array)
Clear table value and sets them based on array.
hasColumn(string $columnname)
has column name
getRows()
return array of ilXMLResultSetRow
addRow(ilXMLResultSetRow $row)