Exemplo n.º 1
0
$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');
/* CraurCsvWriter#extractDirectDescendants */
$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));
/* CraurCsvWriter#extractAllDescendants */
$craur = new Craur(array('name' => 'My Book', 'year' => '2012', 'authors' => array(array('name' => 'Paul', 'age' => '30'), array('name' => 'Erwin', 'age' => '20')), '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'));
$result_data = CraurCsvWriter::extractAllDescendants($craur, array('name', 'year', 'authors[].name', 'authors[].age', 'categories[]', 'pages'), '');
assert(json_encode($expected_data) == json_encode($result_data));
/* CraurCsvReader#mergePathEntriesRecursive */
$entries = array(array('book' => array('name' => 'My Book', 'year' => 2012, 'author' => array('name' => 'Hans', 'age' => '32'))), array('book' => array('name' => 'My Book', 'year' => 2012, 'author' => array('name' => 'Paul', 'age' => '20'))), array('book' => array('name' => 'My second Book', 'year' => 2010, 'author' => array('name' => 'Erwin', 'age' => '10'))));
$merged_entries = CraurCsvReader::mergePathEntriesRecursive($entries);
assert(count($merged_entries) === 1);
assert(json_encode(array('book' => array(array('name' => 'My Book', 'year' => 2012, 'author' => array(array('name' => 'Hans', 'age' => '32'), array('name' => 'Paul', 'age' => '20'))), array('name' => 'My second Book', 'year' => 2010, 'author' => array(array('name' => 'Erwin', 'age' => '10')))))) === json_encode($merged_entries[0]));
/* CraurCsvReader#expandPathsIntoArray */
$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)));
Exemplo n.º 2
0
<?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]));
Exemplo n.º 3
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);
 }