<?php $craur = new Craur(array('name' => 'My Book', 'year' => '2012', 'categories' => array('comedy', 'fantasy'), 'authors' => array(array('name' => 'Paul'), array('name' => 'Erwin')), 'pages' => '212')); $expected_data = array(0 => 'My Book', 1 => '2012', 4 => '212'); $result_data = CraurCsvWriter::extractDirectDescendants($craur, array('name', 'year', 'categories[]', 'authors[].name', 'pages'), ''); assert(json_encode($expected_data) == json_encode($result_data)); $craur = new Craur(array('name' => 'My Book', 'year' => '2012', 'categories' => array(array('name' => 'comedy')), 'authors' => array(array('name' => 'Paul', 'age' => '30'), array('name' => 'Erwin', 'age' => '20')), 'pages' => '212')); $expected_data = array(array('My Book', '2012', 'comedy', 'Paul', '30', '212'), array('My Book', '2012', '', 'Erwin', '20', '212')); $result_data = CraurCsvWriter::extractAllDescendants($craur, array('name', 'year', 'categories[].name', 'authors[].name', 'authors[].age', 'pages'), ''); assert(json_encode($expected_data) == json_encode($result_data)); $craur = new Craur(array('book' => array(array('name' => 'My Book', 'year' => '2012', 'categories' => array(array('name' => 'comedy')), 'authors' => array(array('name' => 'Paul', 'age' => '30'), array('name' => 'Erwin', 'age' => '20')), 'pages' => '212')))); $expected_data = array(array('My Book', '2012', 'comedy', 'Paul', '30', '212'), array('My Book', '2012', '', 'Erwin', '20', '212')); $result_data = CraurCsvWriter::extractAllDescendants($craur, array('book[].name', 'book[].year', 'book[].categories[].name', 'book[].authors[].name', 'book[].authors[].age', 'book[].pages'), ''); assert(json_encode($expected_data) == json_encode($result_data)); $craur = new Craur(array('name' => 'My Book', 'year' => '2012', 'authors' => array(array('name' => 'Paul', 'age' => '30'), array('name' => 'Erwin', 'age' => '20'), array('name' => 'Hans', 'age' => '10')), 'categories' => array('comedy', 'fantasy'), 'pages' => '212')); $expected_data = array(array('My Book', '2012', 'Paul', '30', 'comedy', '212'), array('My Book', '2012', 'Erwin', '20', 'fantasy', '212'), array('My Book', '2012', 'Hans', '10', '', '212')); $result_data = CraurCsvWriter::extractAllDescendants($craur, array('name', 'year', 'authors[].name', 'authors[].age', 'categories[]', 'pages'), ''); assert(json_encode($expected_data) == json_encode($result_data));
/** * This function is used internally to map a csv row into an object. * * @private * * @example * $row_data = array( * 'My Book', * 2012, * 'Hans', * '32' * ); * $field_mappings = array( * 'book[].name', * 'book[].year', * 'book[].author[].name', * 'book[].author[].age' * ); * $expected_entry = array( * 'book' => array( * 'name' => 'My Book', * 'year' => 2012, * 'author' => array( * 'name' => 'Hans', * 'age' => '32' * ) * ) * ); * * assert(json_encode($expected_entry) === json_encode(CraurCsvReader::expandPathsIntoArray($row_data, $field_mappings))); * @return array */ static function expandPathsIntoArray(array $row_data, array $field_mappings, $prefix = '') { $sub_keys = CraurCsvWriter::getSubKeysForFieldMappingAndPrefix($field_mappings, $prefix); $direct_keys = CraurCsvWriter::getDirectKeysForFieldMappingAndPrefix($field_mappings, $prefix); $entry = array(); foreach ($direct_keys as $pos => $direct_key) { if (!empty($row_data[$pos])) { if (empty($direct_key)) { $entry[] = $row_data[$pos]; } else { $entry[$direct_key] = $row_data[$pos]; } } } foreach ($sub_keys as $sub_key => $fields) { /* * If we have real sub values (like author[].name and such * stuff, we need to do the recursion) */ $sub_prefix = ($prefix === '' ? '' : $prefix) . $sub_key . '[]'; /* * If we don't have something like author[].name and we * have just: categories[], we can luckily do an implode * on all fields (which are empty) :). * * In this case, we just want to (string)-casted value without * the dot. Otherwise we need to add a . to the prefix! */ if (implode('', $fields) !== '') { $sub_prefix .= '.'; } $entry[$sub_key] = self::expandPathsIntoArray($row_data, $field_mappings, $sub_prefix); if (empty($entry[$sub_key])) { unset($entry[$sub_key]); } } return $entry; }
/** * Will store the csv file with the objects content according to the given `$field_mappings`. * * @example * $data = array( * 'book' => array( * array( * 'name' => 'My Book', * 'year' => '2012', * 'author' => array( * array('name' => 'Hans'), * array('name' => 'Paul') * ) * ), * array( * 'name' => 'My second Book', * 'year' => '2010', * 'author' => array( * array('name' => 'Erwin') * ) * ) * ) * ); * * $shelf = new Craur($data); * $shelf->saveToCsvFile('fixtures/temp_csv_file.csv', array( * 'book[].name', * 'book[].year', * 'book[].author[].name', * )); * * // csv file will look like this now: * // book[].name;book[].year;book[].author[].name * // "My Book";2012;Hans * // "My Book";2012;Paul * // "My second Book";2010;Erwin * * assert(json_encode(array($data)) == Craur::createFromCsvFile('fixtures/temp_csv_file.csv', array( * 'book[].name', * 'book[].year', * 'book[].author[].name', * ))->toJsonString()); * * unlink('fixtures/temp_csv_file.csv'); * * @return void */ public function saveToCsvFile($csv_file_path, array $field_mappings) { /* * Clean the file */ file_put_contents($csv_file_path, ''); $file_handle = fopen($csv_file_path, 'w'); fputcsv($file_handle, $field_mappings, ';'); $writer = new CraurCsvWriter($this, $field_mappings); $writer->writeToCsvFileHandle($file_handle); fclose($file_handle); }
$entry = new Craur(array('name' => 'My Book', 'year' => '2012')); $field_mappings = array('name', 'year'); $expected_row_data = array('My Book', '2012'); $results_row_data = CraurCsvWriter::extractAllDescendants($entry, $field_mappings); assert(json_encode(array($expected_row_data)) == json_encode($results_row_data)); $entry = new Craur(array('name' => 'My Book', 'year' => '2012', 'author' => array('name' => 'Hans', 'age' => '32'))); $field_mappings = array('name', 'year', 'author[].name', 'author[].age'); $expected_row_data = array('My Book', '2012', 'Hans', '32'); $results_row_data = CraurCsvWriter::extractAllDescendants($entry, $field_mappings); assert(json_encode(array($expected_row_data)) == json_encode($results_row_data)); $entry = new Craur(array('name' => 'My Book', 'year' => '2012', 'author' => array('name' => 'Hans', 'age' => '32'))); $field_mappings = array('name', 'year', 'author[].name', 'author[].age'); $expected_row_data = array('My Book', '2012', 'Hans', '32'); $results_row_data = CraurCsvWriter::extractAllDescendants($entry, $field_mappings); assert(json_encode(array($expected_row_data)) == json_encode($results_row_data)); $entry = new Craur(array('book' => array('name' => 'My Book', 'year' => '2012', 'author' => array('name' => 'Hans', 'age' => '32')))); $field_mappings = array('book[].name', 'book[].year', 'book[].author[].name', 'book[].author[].age'); $expected_row_data = array('My Book', '2012', 'Hans', '32'); $results_row_data = CraurCsvWriter::extractAllDescendants($entry, $field_mappings); assert(json_encode(array($expected_row_data)) == json_encode($results_row_data)); $entry = new Craur(array('book' => array(array('name' => 'My Book', 'year' => '2012', 'author' => array('name' => 'Hans', 'age' => '32')), array('name' => 'My second Book', 'year' => '2010', 'author' => array('name' => 'Paul', 'age' => '20'))))); $field_mappings = array('book[].name', 'book[].year', 'book[].author[].name', 'book[].author[].age'); $expected_rows_data = array(array('My Book', '2012', 'Hans', '32'), array('My second Book', '2010', 'Paul', '20')); $results_row_data = CraurCsvWriter::extractAllDescendants($entry, $field_mappings); assert(json_encode($expected_rows_data) == json_encode($results_row_data)); $entry = new Craur(array('book' => array(array('name' => 'My second Book', 'year' => '2010', 'author' => array('name' => 'Paul', 'age' => '20')), array('name' => 'My Book', 'year' => '2012', 'author' => array(array('name' => 'Hans', 'age' => '32'), array('name' => 'Erwin', 'age' => '10')))))); $field_mappings = array('book[].name', 'book[].year', 'book[].author[].name', 'book[].author[].age'); $expected_rows_data = array(array('My second Book', '2010', 'Paul', '20'), array('My Book', '2012', 'Hans', '32'), array('My Book', '2012', 'Erwin', '10')); $results_row_data = CraurCsvWriter::extractAllDescendants($entry, $field_mappings); assert(json_encode($expected_rows_data) == json_encode($results_row_data));