<?php $row_data = array('My Book', 2012, 'Hans', '32', 'fantasy'); $field_mappings = array('book[].name', 'book[].year', 'book[].author[].name', 'book[].author[].age', 'book[].category[]'); $expected_entry = array('book' => array('name' => 'My Book', 'year' => 2012, 'author' => array('name' => 'Hans', 'age' => '32'), 'category' => array('fantasy'))); $result_data = CraurCsvReader::expandPathsIntoArray($row_data, $field_mappings); assert(json_encode($expected_entry) === json_encode($result_data)); $row_data = array('My Book', 2012, 'Hans', '32', 'fantasy'); $field_mappings = array('book[].name', 'book[].year', 'book[].author[].name', 'book[].author[].age', 'book[].category[].name'); $expected_entry = array('book' => array('name' => 'My Book', 'year' => 2012, 'author' => array('name' => 'Hans', 'age' => '32'), 'category' => array('name' => 'fantasy'))); $result_data = CraurCsvReader::expandPathsIntoArray($row_data, $field_mappings); assert(json_encode($expected_entry) === json_encode($result_data)); $row_data = array('My Book', 2012, 'Hans', '32', ''); $field_mappings = array('book[].name', 'book[].year', 'book[].author[].name', 'book[].author[].age', 'book[].category[].name'); $expected_entry = array('book' => array('name' => 'My Book', 'year' => 2012, 'author' => array('name' => 'Hans', 'age' => '32'))); $result_data = CraurCsvReader::expandPathsIntoArray($row_data, $field_mappings); assert(json_encode($expected_entry) === json_encode($result_data)); $entries = array(array('book' => array('name' => 'My Book', 'year' => 2012, 'author' => array('name' => 'Hans', 'age' => '32'), 'category' => array('comedy'))), array('book' => array('name' => 'My Book', 'year' => 2012, 'author' => array('name' => 'Paul', 'age' => '20'), 'category' => array('fantasy'))), array('book' => array('name' => 'My second Book', 'year' => 2010, 'author' => array('name' => 'Erwin', 'age' => '10'), 'category' => array('comedy')))); $expected_entries = array('book' => array(array('name' => 'My Book', 'year' => 2012, 'author' => array(array('name' => 'Hans', 'age' => '32'), array('name' => 'Paul', 'age' => '20')), 'category' => array('comedy', 'fantasy')), array('name' => 'My second Book', 'year' => 2010, 'author' => array(array('name' => 'Erwin', 'age' => '10')), 'category' => array('comedy')))); $merged_entries = CraurCsvReader::mergePathEntriesRecursive($entries); assert(count($merged_entries) === 1); assert(json_encode($expected_entries) === json_encode($merged_entries[0]));
/** * Will load the first sheet of an xlsx file and fill the objects according to the given `$field_mappings`. * * @example * // If the file loooks like this: * // Book Name;Book Year;Author Name * // My Book;2012;Hans * // My Book;2012;Paul * // My second Book;2010;Erwin * $shelf = Craur::createFromExcelFile('fixtures/books.xlsx', array( * 'book[].name', * 'book[].year', * 'book[].author[].name', * )); * assert(count($shelf->get('book[]')) === 2); * foreach ($shelf->get('book[]') as $book) * { * assert(in_array($book->get('name'), array('My Book', 'My second Book'))); * foreach ($book->get('author[]') as $author) * { * assert(in_array($author->get('name'), array('Hans', 'Paul', 'Erwin'))); * } * } * * @return Craur */ static function createFromExcelFile($file_path, array $field_mappings) { $file_handle = null; if (!file_exists($file_path)) { throw new Exception('Cannot open file at ' . $file_path); } $row_number = 0; $current_entry = array(); $entries = array(); $excel_object = PHPExcel_IOFactory::load($file_path); $rows = $excel_object->getActiveSheet()->toArray(null, true, true, true); foreach ($rows as $row_data) { $row_number++; if ($row_number != 1) { $entries[] = CraurCsvReader::expandPathsIntoArray(array_values($row_data), $field_mappings); } } $merged_entries = CraurCsvReader::mergePathEntriesRecursive($entries); return new Craur($merged_entries); }