Ejemplo n.º 1
0
<?php

try {
    $node = Craur::createFromJson('{"key":"valu}');
    assert(false);
} catch (Exception $exception) {
    /*
     * Great, it broke! :)
     */
}
Ejemplo n.º 2
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!");
Ejemplo n.º 3
0
/* Craur#get */
$node = Craur::createFromJson('{"book": {"name": "MyBook", "authors": ["Hans", "Paul"]}}');
$book = $node->get('book');
assert($book->get('name') == 'MyBook');
assert($book->get('price', 20) == 20);
$authors = $node->get('book.authors[]');
assert(count($authors) == 2);
/* Craur#getWithFilter */
function isACheapBook(Craur $value)
{
    if ($value->get('price') > 20) {
        throw new Exception('Is no cheap book!');
    }
    return $value;
}
$node = Craur::createFromJson('{"books": [{"name":"A", "price": 30}, {"name": "B", "price": 10}, {"name": "C", "price": 15}]}');
$cheap_books = $node->getWithFilter('books[]', 'isACheapBook');
assert(count($cheap_books) == 2);
assert($cheap_books[0]->get('name') == 'B');
assert($cheap_books[1]->get('name') == 'C');
/* Craur#saveToCsvFile */
$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');
Ejemplo n.º 4
0
<?php

$node = Craur::createFromJson('{"book": {"@": "My Book"}}');
assert(strpos($node->toXmlString(), 'My Book') > 0);
<?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);
Ejemplo n.º 6
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);
<?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'));
<?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

$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);