Permalink
Browse files

Added first commit of the Plist creation functionality

  • Loading branch information...
1 parent d75797c commit 86c9db52843c263bd2178a331ff38418a790b613 @paulbain committed Mar 30, 2011
Showing with 96 additions and 0 deletions.
  1. +15 −0 LICENCE.txt
  2. 0 README → README.markdown
  3. +81 −0 lib/Bain/Plist.php
View
@@ -0,0 +1,15 @@
+Bain PHP Library - A collection of utility classes written in PHP
+Copyright (C) 2011 Paul Bain
+
+This program is free software: you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation, either version 3 of the License, or
+at your option) any later version.
+
+This program is distributed in the hope that it will be useful,
+but WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+GNU General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with this program. If not, see <http://www.gnu.org/licenses/>.
File renamed without changes.
View
@@ -0,0 +1,81 @@
+<?php
+class Bain_Plist{
+
+ private $_maxDepth;
+ private $_types;
+ private $_guessType;
+ private $_data;
+
+ public function __construct($data, $maxDepth=null, $types=array(),$guessType=false){
+ if(!is_null($maxDepth)){
+ $this->_maxDepth = $maxDepth;
+ }
+ $this->_types = $types;
+ $this->_guessType = false;
+ $this->_data = $data;
+ }
+
+ public function setTypes($types,$merge=false){
+ if(true==$merge){
+ array_merge($this->_types,$types);
+ } else {
+ $this->_types = $types;
+ }
+ }
+
+ public function __toString(){
+ return $this->toXml($this->_data);
+ }
+
+ protected function toXml($data,$depth=0,$dict=true){
+ if(empty($data)){
+ return '';
+ }
+ if($depth>0){
+ $xml='';
+ foreach($data as $k=>$v){
+ if(is_array($v)){
+ if(empty($v) || is_numeric(key($v))){
+ $xml.='<key>'.$k.'</key>';
+ $xml.='<array>'.$this->toXml($v,$depth+1,false).'</array>';
+ } else {
+ $xml.=$this->toXml($v,$depth+1);
+ }
+ } else {
+ $xml.='<key>'.$k.'</key>';
+ $type = $this->getType($k);
+ switch($type){
+ case 'boolean':
+ $xml.='<'.(($v)?'true':'false').'/>';
+ break;
+ case 'real':
+ case 'integer':
+ if(empty($v)){
+ $v=0;
+ }
+ default:
+ $xml.='<'.$type.'>'.htmlspecialchars($v,null,'UTF-8').'</'.$type.'>';
+ }
+ }
+ }
+ if($dict){
+ $xml='<dict>'.$xml.'</dict>';
+ }
+ return $xml;
+ }
+ return '<?xml version="1.0" encoding="UTF-8"?><plist version="1.0">'.$this->toXml($data, $depth+1).'</plist>';
+ }
+
+ private function getType($keyName){
+ if(array_key_exists($keyName,$this->_types)){
+ return $this->_types[$keyName];
+ }
+ //@todo make it guess the type if $this->_guessType is true - is a performance issue
+ return 'string';
+ }
+
+
+
+
+
+}

0 comments on commit 86c9db5

Please sign in to comment.