Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
20 / 20
100.00% covered (success)
100.00%
7 / 7
CRAP
100.00% covered (success)
100.00%
1 / 1
ExportedTable
100.00% covered (success)
100.00%
20 / 20
100.00% covered (success)
100.00%
7 / 7
13
100.00% covered (success)
100.00%
1 / 1
 __construct
100.00% covered (success)
100.00%
11 / 11
100.00% covered (success)
100.00%
1 / 1
5
 getColumns
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getRows
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getTableName
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 addColumn
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 addRow
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getColumn
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
3
1<?php
2
3/**
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; under version 2
7 * of the License (non-upgradable).
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
17 *
18 * Copyright (c) 2020 (original work) Open Assessment Technologies SA;
19 *
20 */
21
22namespace oat\tao\model\export\implementation\sql;
23
24/**
25 * Class ExportedTable
26 * @package oat\tao\model\export\implementation\sql
27 */
28class ExportedTable
29{
30    /**
31     * @var ExportedColumn[] $columns
32     */
33    private $columns = [];
34
35    /**
36     * @var array
37     */
38    private $rows = [];
39
40    /**
41     * @var string
42     */
43    private $tableName;
44
45    /**
46     * ExportedTable constructor.
47     *
48     * @param array $rows
49     * @param array $typeMapping
50     * @param string $tableName
51     */
52    public function __construct(array $rows, array $typeMapping, string $tableName)
53    {
54        $this->tableName = $tableName;
55
56        foreach ($rows as $row) {
57            $rowFields = [];
58            foreach ($row as $key => $value) {
59                $column = $this->getColumn($key);
60                if (!$column) {
61                    $columnType = isset($typeMapping[$key]) ? $typeMapping[$key] : ExportedColumn::TYPE_VARCHAR;
62                    $column = new ExportedColumn($key, $columnType);
63                    $this->addColumn($column);
64                }
65                $rowFields[] = new ExportedField($column, $value);
66            }
67
68            $this->addRow($rowFields);
69        }
70    }
71
72    /**
73     * @return ExportedColumn[]
74     */
75    public function getColumns(): array
76    {
77        return $this->columns;
78    }
79
80    /**
81     * @return array
82     */
83    public function getRows(): array
84    {
85        return $this->rows;
86    }
87
88    /**
89     * @return string
90     */
91    public function getTableName(): string
92    {
93        return $this->tableName;
94    }
95
96    /**
97     * @param ExportedColumn $column
98     */
99    private function addColumn(ExportedColumn $column)
100    {
101        $this->columns[] = $column;
102    }
103
104    /**
105     * @param ExportedField[] $row
106     */
107    private function addRow(array $row)
108    {
109        $this->rows[] = $row;
110    }
111
112    private function getColumn($name)
113    {
114        foreach ($this->columns as $column) {
115            if ($column->getName() === $name) {
116                return $column;
117            }
118        }
119
120        return null;
121    }
122}