TYPO3 CMS  TYPO3_6-2
DataSet.php
Go to the documentation of this file.
1 <?php
3 
17 use \TYPO3\CMS\Core\Utility\GeneralUtility;
18 
22 class DataSet {
23 
27  protected $data;
28 
33  public static function read($fileName) {
34  $data = self::parseData(self::readData($fileName));
35 
37  'TYPO3\\CMS\\Core\\Tests\\Functional\\DataHandling\\Framework\\DataSet',
38  $data
39  );
40  }
41 
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 
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 
133  public function __construct(array $data) {
134  $this->data = $data;
135  }
136 
140  public function getTableNames() {
141  return array_keys($this->data);
142  }
143 
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 
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 
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 
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 }