dracoblue / craur Goto Github PK
View Code? Open in Web Editor NEWA lossless xml to json and json to xml converter (and csv/xlsx/yaml). Writing PHP Json/Xml/Csv/Yaml/excel Importers made easy.
A lossless xml to json and json to xml converter (and csv/xlsx/yaml). Writing PHP Json/Xml/Csv/Yaml/excel Importers made easy.
Add $default_value to provide a default default value, if default_values is not set.
This:
$ cat fail_with_empty_xml_test.php
<?php
try
{
$node = Craur::createFromXml('');
assert(false);
}
catch (Exception $exception)
{
/*
* Great, it broke! :)
*/
}
Fails wrong:
Fatal error: DOMDocument::loadXML(): Empty string supplied as input in /Volumes/development/workspaces/craur/php/Craur.class.php on line 52 in /Volumes/development/workspaces/craur/php/vendor/naith/naith/NaithCliRunner.class.php on line 87
[ ] fail_with_empty_xml_test.php
-> broken! (Exit code: 255)
am empty xml-tag for example:
<neu neu="true" neuType="Erstausstrahlung"/>
throws an exception when it is converted to a string:
[23-Mar-2015 10:44:26 Europe/Berlin] PHP Fatal error: Method Craur::__toString() must not throw an exception in .../class.php on line 0
the error messages is very hard to debug. maybe it is a better solution to display the given xml-attributes.
It would be quite nice, to use craur also for ini parsing/writing. Reading can be done easily with http://php.net/manual/de/function.parse-ini-file.php
If the value, which is returned, is a associative array it doesn't get returned.
$xml_string = file_get_contents(dirname(__FILE__) . '/fixtures/example_atom_feed.xml');
$document = Craur::createFromXml($xml_string);
// at the moment this: fails
$feed = $document->get('feed');
// at the moment this: works
$entries = $document->get('feed.entry[]');
This should be possible to do:
$feed = $document->get('feed');
$entries = $feed->get('entry[]');
composer.json
doesn't validate at the moment. At least a name
and description
field are necessary. While at it some more information should be added (like homepage
, keywords
etc.).
This has the advantage, that Packagist
support should be possible.
It would be quite handy if one could read key value properties files (know from the java world)
It should be possible to pass in the separator used for fgetcsv in Craur::createFromCsvFile
i got some issues with html5 tags: "Invalid html (Tag nav invalid,...."
when i do suppress the exception:
$error = libxml_get_last_error();
if ($error)
{
#throw new Exception('Invalid html (' . trim($error->message) . ', line: ' . $error->line . ', col: ' . $error->column . '): ' . $html_string);
}
the result seems to be fine:
$craur_node = Craur::createFromHtml($new_html_body);
var_dump($craur_node->toJsonString());
the var_dump returns perfect valid json. maybe craur could suppress any errors containing html5 tags?
$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'));
Is PHP8 support planned?
The version information used in the CLI version of craur as well as the version number in the composer.json
file should be changed. After tagging a release with a version number the current dev version should be set as well (until the next tag/release is due).
most of the time i use craur the xml-files i do import does not got "valid default" xml-tags. for example, the xml should look like this
<item>
<titel>foo</titel>
<genre>bar</genre>
<description>foo</description>
</item>
our customers delivers something like:
<item>
<titel>hans</titel>
<genre>peter</genre>
</item>
<item>
<titel>test</titel>
<description>test123</description>
</item>
so every time i need to write some custom mapping code
protected function mapCraurToArray(Craur $craur, $map,$default_value = '')
{
foreach ($map as $craur_key => $value)
{
try
{
$target_array[$value] = $craur->get($craur_key);
}
catch (Exception $e)
{
$target_array[$value] = $default_value;
}
}
return $target_array;
}
maybe something like that could be implemented in craur too.
Possible patch based on PHPExcel:
Index: php/Craur.class.php
===================================================================
--- php/Craur.class.php (revision 763)
+++ php/Craur.class.php (working copy)
@@ -255,6 +255,39 @@
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);
+ }
+
public function __construct(array $data)
{
$this->data = $data;
Sometimes you have a database and want to use it as source for craur. It would be handy if Craur could import this on it's own, so you don't have to wrap the pdo connection and insert each row into Craur.
The current shell script and class should get some test coverage.
develop@server61 /srv/htdocs/user/oskar/Craur $ make install-dependencies
make[1]: Entering directory `/srv/htdocs/user/oskar/Craur'
#!/usr/bin/env php
Some settings on your machine make Composer unable to work properly.
Make sure that you fix the issues listed below and run this script again:
The allow_url_fopen setting is incorrect.
Add the following to the end of your `php.ini`:
allow_url_fopen = On
The php.ini used by your command-line PHP is: /etc/php/php.ini
make[1]: *** [install-composer] Error 1
make[1]: Leaving directory `/srv/htdocs/user/oskar/Craur'
make: *** [install-dependencies] Error 2
This one:
<?php
error_reporting(E_ALL | E_STRICT);
require_once('../Craur.class.php');
$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);
fails in the second assertion:
<feed><title lang="en"></title></feed>
Correct would be:
<feed><title lang="en">Example Feed</title></feed>
Craur should support Yaml as format. Maybe something like ''''createFromYaml()'''.
ini_set('display_errors', 'STDERR'); redirects exceptions and such to stderr without polluting the stdout
Current behaviour:
<?php
$craur = Craur::createFromHtml('<a href="http://dracoblue.net">dracoblue</a>');
assert($craur->get('html.body.a') == 'dracoblue');
assert($craur->get('html.body.a.@href') == 'http://dracoblue.net');
Expected behaviour:
<?php
$craur = Craur::createFromHtml('<a href="http://dracoblue.net">dracoblue</a>');
assert($craur->get('a') == 'dracoblue');
assert($craur->get('a.@href') == 'http://dracoblue.net');
Composer reports that codeplex/phpexcel is abandoned. Please upgrade to phpoffice/phpexcel.
$craur->getWithFilter('my_value', $callback);
The callback would be checked with is_callable and throw an exception if the path cannot be found.
If you provide invalid xml/json it should throw an exception, currently it fails silently or with a warning.
If we have the input:
<document>
<body>
<header name="hans">
<mau you="me">mom</mau>
<pau>la</pau>
</header>
<footer name="hans">
emil
</footer>
</document>
it would be great if one could do:
"document.body.header[mau=mom].pau"
to fetch "la"
or
"document.body.header[mau.@you=me].pau"
to fetch "la"
or
"document.body.header[mau=mom][]"
to fetch [{"mau" => "mom", "pau" => "la"}]
.
or
"document.body.footer[@name=hans]"
to fetch "emil".
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.