/ Published in: PHP

Convert a php array to an apple plist xml or text format
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
<?PHP /** * PropertyList class * Implements writing Apple Property List (.plist) XML and text files from an array. * * @author Jesus A. Alvarez <[email protected]> */ function plist_encode_text ($obj) { $plist = new PropertyList($obj); return $plist->text(); } function plist_encode_xml ($obj) { $plist = new PropertyList($obj); return $plist->xml(); } class PropertyList { private $obj, $xml, $text; public function __construct ($obj) { $this->obj = $obj; } private static function is_assoc ($array) { } public function xml () { $x = new XMLWriter(); $x->openMemory(); $x->setIndent(TRUE); $x->startDocument('1.0', 'UTF-8'); $x->writeDTD('plist', '-//Apple//DTD PLIST 1.0//EN', 'http://www.apple.com/DTDs/PropertyList-1.0.dtd'); $x->startElement('plist'); $x->writeAttribute('version', '1.0'); $this->xmlWriteValue($x, $this->obj); $x->endElement(); // plist $x->endDocument(); $this->xml = $x->outputMemory(); return $this->xml; } public function text() { $text = ''; $this->textWriteValue($text, $this->obj); $this->text = $text; return $this->text; } private function xmlWriteDict(XMLWriter $x, &$dict) { $x->startElement('dict'); foreach($dict as $k => &$v) { $x->writeElement('key', $k); $this->xmlWriteValue($x, $v); } $x->endElement(); // dict } private function xmlWriteArray(XMLWriter $x, &$arr) { $x->startElement('array'); foreach($arr as &$v) $this->xmlWriteValue($x, $v); $x->endElement(); // array } private function xmlWriteValue(XMLWriter $x, &$v) { $x->writeElement('integer', $v); $x->writeElement('real', $v); $x->writeElement('string', $v); $x->writeElement($v?'true':'false'); elseif (PropertyList::is_assoc($v)) $this->xmlWriteDict($x, $v); $this->xmlWriteArray($x, $v); $x->writeElement('data', $v->base64EncodedData()); $x->writeElement('date', $v->encodedDate()); else { $x->writeElement('string', $v); } } private function textWriteValue(&$text, &$v, $indentLevel = 0) { $this->textWriteString($text, $v); $text .= $v?'YES':'NO'; elseif (PropertyList::is_assoc($v)) $this->textWriteDict($text, $v, $indentLevel); $this->textWriteArray($text, $v, $indentLevel); $text .= '<' . $v->hexEncodedData() . '>'; $text .= '"' . $v->ISO8601Date() . '"'; else { $this->textWriteString($text, $v); } } private function textWriteString(&$text, &$str) { else $text .= '"' . $this->textEncodeString($str) . '"'; } private function textEncodeString(&$str) { $newstr = ''; $i = 0; while($i < $len) { if ($ch == 0x22 || $ch == 0x5C) { // escape double quote, backslash $i++; } else if ($ch >= 0x07 && $ch <= 0x0D ){ // control characters with escape sequences $i++; } else if ($ch < 32) { // other non-printable characters escaped as unicode $i++; } else if ($ch < 128) { // ascii printable $i++; } else if ($ch == 192 || $ch == 193) { // invalid encoding of ASCII characters $i++; } else if (($ch & 0xC0) == 0x80){ // part of a lost multibyte sequence, skip $i++; } else if (($ch & 0xE0) == 0xC0) { // U+0080 - U+07FF (2 bytes) $i += 2; } else if (($ch & 0xF0) == 0xE0) { // U+0800 - U+FFFF (3 bytes) $i += 3; } else if (($ch & 0xF8) == 0xF0) { // U+10000 - U+3FFFF (4 bytes) $i += 4; } else { // 5 and 6 byte sequences are not valid UTF-8 $i++; } } return $newstr; } private function textWriteDict(&$text, &$dict, $indentLevel) { $text .= '{}'; return; } $text .= "{\n"; $indent = ''; $indentLevel++; foreach($dict as $k => &$v) { $text .= $indent; $this->textWriteValue($text, $k); $text .= ' = '; $this->textWriteValue($text, $v, $indentLevel); $text .= ";\n"; } } private function textWriteArray(&$text, &$arr, $indentLevel) { $text .= '()'; return; } $text .= "(\n"; $indent = ''; $indentLevel++; foreach($arr as &$v) { $text .= $indent; $this->textWriteValue($text, $v, $indentLevel); $text .= ",\n"; } } } class PlistData { private $data; public function __construct($str) { $this->data = $str; } public function base64EncodedData () { } public function hexEncodedData () { $hexstr = ''; for($i = 0; $i < $len; $i += 4) } } class PlistDate { private $dateval; public function __construct($init = NULL) { $this->dateval = $init; elseif ($init == NULL) } public function ISO8601Date() { } } ?>
Comments
