From 4ea5f9d02a49bf303a9d268d8a40e8532f6009de Mon Sep 17 00:00:00 2001 From: Filipp Lepalaan Date: Sun, 6 Jun 2010 10:38:19 +0300 Subject: first commit --- PropertyList.php | 95 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 95 insertions(+) create mode 100644 PropertyList.php diff --git a/PropertyList.php b/PropertyList.php new file mode 100644 index 0000000..c407011 --- /dev/null +++ b/PropertyList.php @@ -0,0 +1,95 @@ + + * Usage: $plist = new PropertyList("/path/to/file.plist") + * $array = $plist->toArray() + */ +class PropertyList +{ + function __construct($xmlFile) { + if (!file_exists($xmlFile)) { + echo "{$xmlFile}: no such file"; + return false; + } + $document = new DOMDocument(); + $document->load($xmlFile); + $plistNode = $document->documentElement; + $this->root = $plistNode->firstChild; + while ($this->root->nodeName == "#text") { + $this->root = $this->root->nextSibling; + } + return $this; + } + + /** + * Return a PropertyList as array + */ + function toArray() { + return $this->parseValue($this->root); + } + + /** + * Route plist key value to the correct parsing method + */ + function parseValue($valueNode) { + $valueType = $valueNode->nodeName; + $transformerName = "parse_$valueType"; + if (is_callable(array($this, $transformerName))) { + return call_user_func(array($this, $transformerName), $valueNode); + } + return null; + } + + function parse_integer($integerNode) { + return $integerNode->textContent; + } + + function parse_string($stringNode) { + return $stringNode->textContent; + } + + function parse_date($dateNode) { + return $dateNode->textContent; + } + + function parse_true($trueNode) { + return true; + } + + function parse_false($trueNode) { + return false; + } + + function parse_dict($dictNode) { + $dict = array(); + for ($node = $dictNode->firstChild; $node != null; $node = $node->nextSibling) { + if ($node->nodeName == 'key') { + $key = $node->textContent; + $valueNode = $node->nextSibling; + while ($valueNode->nodeType == XML_TEXT_NODE) { + $valueNode = $valueNode->nextSibling; + } + $value = $this->parseValue($valueNode); + $dict[$key] = $value; + } + } + return $dict; + } + + function parse_array($arrayNode) { + $array = array(); + for ($node = $arrayNode->firstChild; $node != null; $node = $node->nextSibling) { + if ($node->nodeType == XML_ELEMENT_NODE) { + array_push($array, $this->parseValue($node)); + } + } + return $array; + } +} + +?> -- cgit v1.2.3