[TASK] Remove leading slash from use statements
[Packages/TYPO3.CMS.git] / typo3 / sysext / core / Tests / Functional / DataHandling / Framework / DataSet.php
1 <?php
2 namespace TYPO3\CMS\Core\Tests\Functional\DataHandling\Framework;
3
4 /*
5 * This file is part of the TYPO3 CMS project.
6 *
7 * It is free software; you can redistribute it and/or modify it under
8 * the terms of the GNU General Public License, either version 2
9 * of the License, or any later version.
10 *
11 * For the full copyright and license information, please read the
12 * LICENSE.txt file that was distributed with this source code.
13 *
14 * The TYPO3 project - inspiring people to share!
15 */
16
17 use TYPO3\CMS\Core\Utility\GeneralUtility;
18
19 /**
20 * DataHandler DataSet
21 */
22 class DataSet {
23
24 /**
25 * @var array
26 */
27 protected $data;
28
29 /**
30 * @param string $fileName
31 * @return DataSet
32 */
33 public static function read($fileName) {
34 $data = self::parseData(self::readData($fileName));
35
36 return GeneralUtility::makeInstance(
37 \TYPO3\CMS\Core\Tests\Functional\DataHandling\Framework\DataSet::class,
38 $data
39 );
40 }
41
42 /**
43 * @param string $fileName
44 * @return array
45 * @throws \RuntimeException
46 */
47 protected static function readData($fileName) {
48 if (!file_exists($fileName)) {
49 throw new \RuntimeException('File "' . $fileName . '" does not exist');
50 }
51
52 $rawData = array();
53 $fileHandle = fopen($fileName, 'r');
54 while (($values = fgetcsv($fileHandle, 0)) !== FALSE) {
55 $rawData[] = $values;
56 }
57 fclose($fileHandle);
58 return $rawData;
59 }
60
61 /**
62 * Parses CSV data.
63 *
64 * Special values are:
65 * + "\NULL" to treat as NULL value
66 * + "\*" to ignore value during comparison
67 *
68 * @param array $rawData
69 * @return array
70 */
71 protected static function parseData(array $rawData) {
72 $data = array();
73 $tableName = NULL;
74 $fieldCount = NULL;
75 $idIndex = NULL;
76 foreach ($rawData as $values) {
77 if (!empty($values[0])) {
78 // Skip comment lines, starting with "#"
79 if ($values[0]{0} === '#') {
80 continue;
81 }
82 $tableName = $values[0];
83 $fieldCount = NULL;
84 $idIndex = NULL;
85 if (!isset($data[$tableName])) {
86 $data[$tableName] = array();
87 }
88 } elseif (implode('', $values) === '') {
89 $tableName = NULL;
90 $fieldCount = NULL;
91 $idIndex = NULL;
92 } elseif ($tableName !== NULL && !empty($values[1])) {
93 array_shift($values);
94 if (!isset($data[$tableName]['fields'])) {
95 $data[$tableName]['fields'] = array();
96 foreach ($values as $value) {
97 if (empty($value)) {
98 continue;
99 }
100 $data[$tableName]['fields'][] = $value;
101 $fieldCount = count($data[$tableName]['fields']);
102 }
103 if (in_array('uid', $values)) {
104 $idIndex = array_search('uid', $values);
105 $data[$tableName]['idIndex'] = $idIndex;
106 }
107 } else {
108 if (!isset($data[$tableName]['elements'])) {
109 $data[$tableName]['elements'] = array();
110 }
111 $values = array_slice($values, 0, $fieldCount);
112 foreach ($values as &$value) {
113 if ($value === '\\NULL') {
114 $value = NULL;
115 }
116 }
117 unset($value);
118 $element = array_combine($data[$tableName]['fields'], $values);
119 if ($idIndex !== NULL) {
120 $data[$tableName]['elements'][$values[$idIndex]] = $element;
121 } else {
122 $data[$tableName]['elements'][] = $element;
123 }
124 }
125 }
126 }
127 return $data;
128 }
129
130 /**
131 * @param array $data
132 */
133 public function __construct(array $data) {
134 $this->data = $data;
135 }
136
137 /**
138 * @return array
139 */
140 public function getTableNames() {
141 return array_keys($this->data);
142 }
143
144 /**
145 * @param string $tableName
146 * @return NULL|array
147 */
148 public function getFields($tableName) {
149 $fields = NULL;
150 if (isset($this->data[$tableName]['fields'])) {
151 $fields = $this->data[$tableName]['fields'];
152 }
153 return $fields;
154 }
155
156 /**
157 * @param string $tableName
158 * @return NULL|integer
159 */
160 public function getIdIndex($tableName) {
161 $idIndex = NULL;
162 if (isset($this->data[$tableName]['idIndex'])) {
163 $idIndex = $this->data[$tableName]['idIndex'];
164 }
165 return $idIndex;
166 }
167
168 /**
169 * @param string $tableName
170 * @return NULL|array
171 */
172 public function getElements($tableName) {
173 $elements = NULL;
174 if (isset($this->data[$tableName]['elements'])) {
175 $elements = $this->data[$tableName]['elements'];
176 }
177 return $elements;
178 }
179
180 /**
181 * @param string $fileName
182 */
183 public function persist($fileName) {
184 $fileHandle = fopen($fileName, 'w');
185
186 foreach ($this->data as $tableName => $tableData) {
187 if (empty($tableData['fields']) || empty($tableData['elements'])) {
188 continue;
189 }
190
191 $fields = $tableData['fields'];
192 array_unshift($fields, '');
193
194 fputcsv($fileHandle, array($tableName));
195 fputcsv($fileHandle, $fields);
196
197 foreach ($tableData['elements'] as $element) {
198 array_unshift($element, '');
199 fputcsv($fileHandle, $element);
200 }
201 }
202
203 fclose($fileHandle);
204 }
205
206 }