Пример #1
0
// My second Book;2010;Erwin
$shelf = Craur::createFromCsvFile('fixtures/books.csv', 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')));
    }
}
/* Craur#createFromExcelFile */
// 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')));
    }
}
/* Craur#createFromYamlFile */
// If the file loooks like this:
// * books:
//   -
//     name: My Book
//     year: 2012
//     authors:
//       -
Пример #2
0
<?php

$shelf = Craur::createFromExcelFile(dirname(__FILE__) . '/fixtures/books_with_categories.xlsx', array('book[].name', 'book[].year', 'book[].author[].name', 'book[].author[].age', 'book[].category[].name'));
assert(count($shelf->get('book[]')) === 2);
foreach ($shelf->get('book[]') as $book) {
    if ($book->get('name') === 'My Book') {
        assert(count($book->get('author[]')) === 2);
        assert($book->get('author.name') == 'Hans');
        assert($book->get('author.age') == '32');
        foreach ($book->get('author[]') as $author) {
            assert(in_array($author->get('age'), array('32', '20')));
            assert(in_array($author->get('name'), array('Hans', 'Paul')));
        }
        assert(count($book->get('category[]')) === 1);
        assert($book->get('category.name') == 'Fantasy');
    } elseif ($book->get('name') === 'My second Book') {
        assert(count($book->get('author[]')) === 1);
        assert($book->get('author.name') == 'Erwin');
        assert($book->get('author.age') == '10');
        assert(count($book->get('category[]')) === 2);
        foreach ($book->get('category[]') as $category) {
            assert(in_array($category->get('name'), array('Fantasy', 'Comedy')));
        }
    }
}
Пример #3
0
<?php

try {
    $shelf = Craur::createFromExcelFile(dirname(__FILE__) . '/fixtures/non_existing_books.xlsx', array('book[].name'));
    assert(false);
} catch (Exception $exception) {
    /*
     * Great, the file does not exist!
     */
}