示例#1
0
function youngerThenThreeYears(Craur $value)
{
    if ($value->get('@age') < 3) {
        return $value;
    }
    throw new Exception('Is not younger then three years!');
}
示例#2
0
 /**
  * Only write the direct descendants into a new row.
  * 
  * @example
  *     $craur = new Craur(
  *         array(
  *             'name' => 'My Book',
  *             'year' => '2012',
  *             'categories' => array( // Will be ignored
  *                 'comedy',          
  *                 'fantasy'           
  *             ),
  *             'authors' => array( // Will be ignored
  *                 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)); 
  * @return array
  */
 static function extractDirectDescendants(Craur $craur, $field_mappings, $prefix = '')
 {
     $row = array();
     foreach ($field_mappings as $pos => $field_mapping_with_prefix) {
         if (substr($field_mapping_with_prefix, 0, strlen($prefix)) == $prefix) {
             /*
              * Get rid of the prefix
              */
             $field_mapping = substr($field_mapping_with_prefix, strlen($prefix));
             if (strpos($field_mapping, '.') === false) {
                 /*
                  * Something like: name, age or categories
                  * 
                  * So the $field_mappings[$pos] is something like: book[].name, book[].author[].age or book[].categories[]
                  * which indicates, if we want one or multiple values
                  */
                 if (substr($field_mapping, -2, 2) !== '[]') {
                     /*
                      * We want one value!
                      */
                     $row[$pos] = $craur->get($field_mapping, '');
                 }
             }
         }
     }
     return $row;
 }
function getFeedLink(Craur $value)
{
    if ($value->get('@rel', '') === 'feed') {
        return $value->get('@href');
    }
    throw new Exception('This is no feed link!');
}
<?php

$xml_string = file_get_contents(dirname(__FILE__) . '/fixtures/working_feed.xml');
$node = Craur::createFromXml($xml_string);
/*
 * JSON -> Craur -> XML -> Craur -> JSON 
 */
$json = $node->toJsonString();
$test_node_from_json = Craur::createFromJson($json);
$xml = $test_node_from_json->toXmlString();
$test_node_from_xml_from_json = Craur::createFromXml($xml);
$json_from_xml_from_json = $test_node_from_xml_from_json->toJsonString();
assert($json === $json_from_xml_from_json);
<?php

$invalid_xml = file_get_contents(dirname(__FILE__) . '/fixtures/xml_with_0_value.xml');
$craur = Craur::createFromXml($invalid_xml);
assert($craur->get('root.zerovalue') == '0');
<?php

try {
    $node = Craur::createFromXml('<book><author></author</book>');
    assert(false);
} catch (Exception $exception) {
    /*
     * Great, it broke! :)
     */
}
<?php

try {
    $node = Craur::createFromJson('{"key":"valu}');
    assert(false);
} catch (Exception $exception) {
    /*
     * Great, it broke! :)
     */
}
示例#8
0
<?php

$shelf = Craur::createFromCsvFile(dirname(__FILE__) . '/fixtures/books.csv', array('book[].name', 'book[].year', 'book[].author[].name', 'book[].author[].age', 'book[].reader[].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')));
        }
    } 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');
    }
}
示例#9
0
function isACheapBook(Craur $value)
{
    if ($value->get('price') > 20) {
        throw new Exception('Is no cheap book!');
    }
    return $value;
}
示例#10
0
<?php

$craur = Craur::createFromHtml('<!DOCTYPE html>' . PHP_EOL . '<html><head><title>HTML5 Test</title></head><body><aside>A sidebar?</aside></body></html>');
assert($craur->get('html.head.title') == 'HTML5 Test');
assert($craur->get('html.body.aside') == 'A sidebar?');
try {
    $craur = Craur::createFromHtml('<!DOCTYPE html>' . PHP_EOL . '<html><head><title>HTML5 Test</title></head><body><aside>A sidebar?</aside></body><html>');
    assert(false);
} catch (Exception $exception) {
    assert(strpos($exception->getMessage(), 'Invalid html') > -1);
}
try {
    $craur = Craur::createFromHtml('<!DOCTYPE html>' . PHP_EOL . '<html><head><title>HTML5 Test</title></head><body><aside>A sidebar?</aside><img></img></body></html>');
    assert(false);
} catch (Exception $exception) {
    assert(strpos($exception->getMessage(), 'Unexpected end tag : img') > -1);
}
try {
    $craur = Craur::createFromHtml('<!DOCTYPE html>' . PHP_EOL . '<html><head><title>HTML5 Test</title></head><body><img></img><aside>A sidebar?</aside></body></html>');
    assert(false);
} catch (Exception $exception) {
    assert(strpos($exception->getMessage(), 'Unexpected end tag : img') > -1);
}
示例#11
0
<?php

$craur = Craur::createFromHtml('<!DOCTYPE html>' . PHP_EOL . '<html><head><title>Test Title</title></head><body></body></html>');
assert($craur->get('html.head.title') == 'Test Title');
$craur = Craur::createFromHtml(file_get_contents(dirname(__FILE__) . '/fixtures/strict_html_file.html'));
assert($craur->get('html.head.title') == 'Test Title');
assert($craur->get('html.@xmlns:atom') == 'http://www.w3.org/2005/Atom');
assert($craur->get('html.body.p.img.@width') == '20');
assert($craur->get('html.body.p.img.@height') == '30');
assert($craur->get('html.body.p.img.@src') == 'http://example.org/image.png');
assert($craur->get('html.body.p') == 'testtest2');
try {
    $craur = Craur::createFromHtml('<!DOCTYPE html>' . PHP_EOL . '<html><head><title>Test Title</title></head><body></body><html>');
    assert(false);
} catch (Exception $exception) {
    assert(strpos($exception->getMessage(), 'Invalid html') > -1);
}
<?php

$data = array('book' => array(array('name' => 'My Book', 'year' => '2012', 'author' => array(array('name' => 'Hans', 'cities' => array('Berlin', 'London')), 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', 'book[].author[].cities[]'));
$result_csv_content = trim(file_get_contents('fixtures/temp_csv_file.csv'));
unlink('fixtures/temp_csv_file.csv');
$lines = array_slice(explode(PHP_EOL, $result_csv_content), 1);
$expected_lines = array('"My Book";2012;Hans;Berlin', '"My Book";2012;Hans;London', '"My Book";2012;Paul;', '"My second Book";2010;Erwin;');
assert(count(array_diff($expected_lines, $lines)) === 0);
<?php

$shelf = Craur::createFromCsvFile(dirname(__FILE__) . '/fixtures/books_with_multiple_categories.csv', array('book[].name', 'book[].year', 'book[].author[].name', 'book[].author[].age', 'book[].category[]', 'book[].category[]'));
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') == '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((string) $category, array('Fantasy', 'Comedy')));
        }
    }
}
<?php

$node = Craur::createFromJson('{"items":"hans"}');
/*
 * Even though items is no numeric array and contains directly
 * the member, we want to get it as items!
 */
$items = $node->get('items[]');
assert(count($items) == 1);
assert($items[0] == "hans");
<?php

$invalid_html = file_get_contents(dirname(__FILE__) . '/fixtures/invalid_character_in_html.html');
$craur = Craur::createFromHtml($invalid_html, 'iso-8859-1');
assert($craur->get('html.head.title') == 'xx');
示例#16
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!
     */
}
示例#17
0
<?php

$xml_string = file_get_contents(dirname(__FILE__) . '/fixtures/example_atom_feed.xml');
$document = Craur::createFromXml($xml_string);
/*
 * Can we grab all feeds? (it should be just one)
 */
$feeds = $document->get('feed[]');
assert(count($feeds) === 1);
$feed = $document->get('feed');
foreach ($feed->get('entry[]', array()) as $entry) {
    /*
     * At least 1 contributor
     */
    $contributors = $entry->get('contributor[]');
    assert(count($contributors) > 0);
    /*
     * first link.@rel must be alternate or self
     */
    assert(in_array($entry->get('link.@rel'), array('alternate', 'self')));
    $entry_data = $entry->getValues(array('id' => 'id', 'title' => 'title', 'link' => 'link.@href', 'author_name' => 'author.name'));
    assert(0 === count(array_diff(array_keys($entry_data), array('id', 'title', 'link', 'author_name'))));
}
<?php

$craur = Craur::createFromXml(file_get_contents('fixtures/example_atom_feed.xml'));
$craur->saveToCsvFile('fixtures/temp_csv_file.csv', array('feed.entry[].title', 'feed.entry[].link[].@rel', 'feed.entry[].link[].nonexistantsubkey[].name', 'feed.entry[].link[].@href'));
$result_csv_content = trim(file_get_contents('fixtures/temp_csv_file.csv'));
unlink('fixtures/temp_csv_file.csv');
$lines = array_slice(explode(PHP_EOL, $result_csv_content), 1);
$expected_lines = array('"Atom draft-07 snapshot";alternate;;http://example.org/2005/04/02/atom', '"Atom draft-07 snapshot";enclosure;;http://example.org/audio/ph34r_my_podcast.mp3');
assert(count(array_diff($expected_lines, $lines)) === 0);
示例#19
0
<?php

$node = Craur::createFromJson('{"book": {"@": "My Book"}}');
assert(strpos($node->toXmlString(), 'My Book') > 0);
<?php

$xml_string = file_get_contents(dirname(__FILE__) . '/fixtures/working_feed.xml');
$node = Craur::createFromXml($xml_string);
assert((string) $node->get('feed.title') === 'Example Feed');
/*
 * Test if a string works as default value
 */
assert($node->get('feed.non_existant_key', 'default') === 'default');
/*
 * Test if null and false work, too!
 */
assert($node->get('feed.non_existant_key', null) === null);
assert($node->get('feed.non_existant_key', false) === false);
try {
    $node->get('feed.non_existant_key');
    /*
     * This should not work!
     */
    assert(false);
} catch (Exception $exception) {
    /*
     * Nice, we got an exception!
     */
}
try {
    $node->get('feed.non_existant_key[]');
    /*
     * This should not work!
     */
    assert(false);
<?php

try {
    $node = Craur::createFromXml('');
    assert(false);
} catch (Exception $exception) {
    /*
     * Great, it broke! :)
     */
}
<?php

/*
 * @see <https://github.com/DracoBlue/Craur/issues/3>
 */
$node = Craur::createFromJson(json_encode(array('book' => array('author' => array(array('@name' => 'Hans'), array('@name' => 'Paul'))))));
/*
 * Should be the first name
 */
assert('Hans' == $node->get('book.author.@name'));
示例#23
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')));
        }
    }
}
示例#24
0
<?php

$node = new Craur(array('animals' => array(array('@name' => 'dog', '@age' => 6, '@height' => '50cm'), array('@name' => 'cat', '@age' => 2, '@height' => '30cm'), array('@name' => 'mouse', '@age' => 2, '@height' => '10cm'))));
function myFilterCallback($value)
{
    return str_replace('cm', '', $value);
}
/*
 * Test to rewrite the value with a filter
 */
$height = $node->getWithFilter('animals.@height', "myFilterCallback");
assert($height == "50");
/*
 * Test default value
 */
$width = $node->getWithFilter('animals.@width', "myFilterCallback", '100');
assert($width == "100");
/*
 * Test with invalid callback
 */
try {
    $node->getWithFilter('animals.@height', new stdClass());
    assert(false);
} catch (Exception $exception) {
    /*
     * This was no callback, so it must fail! Nice!
     */
}
/*
 * Test with invalid path and no default value
 */
示例#25
0
<?php

$shelf = Craur::createFromYamlFile(dirname(__FILE__) . '/fixtures/books.yaml');
assert(count($shelf->get('books[]')) === 2);
foreach ($shelf->get('books[]') as $book) {
    if ($book->get('name') === 'My Book') {
        assert(count($book->get('authors[]')) === 2);
        assert($book->get('authors.name') == 'Hans');
        assert($book->get('authors.age') == '32');
        foreach ($book->get('authors[]') as $author) {
            assert(in_array($author->get('age'), array('32', '20')));
            assert(in_array($author->get('name'), array('Hans', 'Paul')));
        }
    } elseif ($book->get('name') === 'My second Book') {
        assert(count($book->get('authors[]')) === 1);
        assert($book->get('authors.name') == 'Erwin');
        assert($book->get('authors.age') == '10');
    }
}
示例#26
0
<?php

$node = Craur::createFromJson(json_encode(array('feed' => array('@xmlns' => 'http://www.w3.org/2005/Atom', 'title' => array('@' => 'Example Feed', '@lang' => 'en'), 'link' => array(array('@href' => 'http://example.org/feed/', '@rel' => 'self'), array('@href' => 'http://example.org')), 'author' => array('name' => 'John Doe', 'email' => '*****@*****.**')))));
$values = $node->getValues(array('title' => 'feed.title', 'title_language' => 'feed.title.@lang', 'author_name' => 'feed.author.name', 'author_email' => 'feed.author.email'));
assert($values['title'] == 'Example Feed');
assert($values['title_language'] == 'en');
assert($values['author_name'] == 'John Doe');
assert($values['author_email'] == '*****@*****.**');
/*
 * Test overall default value
 */
$values = $node->getValues(array('title' => 'feed.title', 'non_existant' => 'feed.non_existant', 'non_existant2' => 'feed.non_existant2'), array('non_existant' => 'test'), false);
assert($values['title'] == 'Example Feed');
assert($values['non_existant'] === 'test');
assert($values['non_existant2'] === false);
示例#27
0
<?php

$invalid_xml = file_get_contents(dirname(__FILE__) . '/fixtures/invalid_xml.xml');
$craur = Craur::createFromXml($invalid_xml, 'iso-8859-1');
assert($craur->get('test.wrong') == 'xx');
<?php

return;
$data = array('issues' => array(array('number' => '123', 'labels' => array('bug', 'feature')), array('number' => '815', 'labels' => array())));
$shelf = new Craur($data);
$shelf->saveToCsvFile('fixtures/temp_csv_file.csv', array('issues[].number', 'issues[].labels[]'));
$result_csv_content = trim(file_get_contents('fixtures/temp_csv_file.csv'));
unlink('fixtures/temp_csv_file.csv');
echo $result_csv_content;
$lines = array_slice(explode(PHP_EOL, $result_csv_content), 1);
$expected_lines = array('123;bug', '123;feature', '815;');
assert(count(array_diff($expected_lines, $lines)) === 0);
示例#29
0
<?php

$node = Craur::createFromJson('{"items":[]}');
/*
 * This should work
 */
$node->get('items[]');
try {
    $node->get('noitems[]');
    assert(false);
} catch (Exception $exception) {
    /*
     * This should not work, since we have no noitems attribute at all!
     */
}
try {
    $node->get('items');
    assert(false);
} catch (Exception $exception) {
    /*
     * Yes, we expected it to fail since there is no value at items!
     */
}
/*
 * But it should work with default value!
 */
$node->get('items', "me item!");
<?php

/*
 * @see <https://github.com/DracoBlue/Craur/issues/1>
 */
$node = Craur::createFromJson(json_encode(array('feed' => array('title' => array('@' => 'Example Feed', '@lang' => 'en')))));
assert('Example Feed' == $node->get('feed.title'));
/*
 * So this Example Feed should be in the xml and json response
 */
assert(strpos($node->toJsonString(), 'Example Feed') > 0);
assert(strpos($node->toXmlString(), 'Example Feed') > 0);