brianseitel / ook
A package that transforms XML/JSON documents from one standard to another
Installs: 616
Dependents: 0
Suggesters: 0
Security: 0
Stars: 1
Watchers: 2
Forks: 1
Open Issues: 0
Type:project
Requires
- php: >=5.5.9
- symfony/yaml: ^3.0
Requires (Dev)
- phpunit/phpunit: 5.3.*
This package is not auto-updated.
Last update: 2024-11-09 19:51:07 UTC
README
A translator for XML/JSON files using a YAML or JSON configuration.
What is "Ook"?
In the Discworld series by Larry Niven, the Librarian is a wizard turned into an orangutan early in the series. He understands Morporkian (English) perfectly well, but always speaks in orangutan, using words like "Ook" and "Eek". Most human characters have little trouble understanding this, but every now and again someone unfamiliar with the Librarian meets him and cannot quite figure out what that particular "Ook" meant. The Librarian tried to address this issue by writing an Orangutan-Morpokian dictionary, but has not progressed beyond "Ook" yet.
Oh, I see.
Yeah, pretty funny, right?
Quick Start
Define a configuration
Ook utilizes the "dot" notation when defining rules for translating between arrays. Here's a quick example of what dot notation is:
$array = ['items' => [ 'item' => [ ['name' => 'computer', 'price' => 1000], ['name' => 'monitor', 'price' => 150] ] ]; Arr::get('items.item.0.price') // 1000
We'll use the same thing to define a ruleset for XML:
sample.xml
<items> <item> <name>computer</name> <price>1000</price> </item> <item> <name>monitor</name> <price>150</price> </item> </items>
Ook will convert this to an array with a key of item.price
. Let's imagine we want to translate this from item.price
to inventory.item.price
config.yaml
inventory.item.*.name: items.item.*.name inventory.item.*.price: items.item.*.price
To translate it, we simply run:
$librarian = new Ook\Librarian('sample.xml', 'config.yaml'); $output = $librarian->transform();
This will return:
Array
(
[inventory] => Array
(
[item] => Array
(
[0] => Array
(
[name] => computer
[price] => 1000
)
[1] => Array
(
[name] => monitor
[price] => 150
)
)
)
)
Additional Details
Check out the /examples
directory for more samples.