-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPropertyList.php
More file actions
95 lines (84 loc) · 2.29 KB
/
PropertyList.php
File metadata and controls
95 lines (84 loc) · 2.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
<?php
/**
* plist.php
* Property List parser class for PHP
* Shamelessly ripped from Theo Hultberg
* http://blog.iconara.net/2007/05/08/php-plist-parsing/
* Class wrapper by Filipp Lepalaan <filipp@mac.com>
* 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;
}
}
?>