From dd870cadf1a40e55f135b8498bbc56709b1c631c Mon Sep 17 00:00:00 2001 From: Recigio Poffo Date: Tue, 8 Nov 2016 16:17:18 -0200 Subject: [PATCH] Class to generate #Name-Value parser code Write Name-Value code with php. --- .../PHP/Himaker_Filemaker_EncodeBar.php | 168 ++++++++++++++++++ 1 file changed, 168 insertions(+) create mode 100644 Functions/#Name-Value/PHP/Himaker_Filemaker_EncodeBar.php diff --git a/Functions/#Name-Value/PHP/Himaker_Filemaker_EncodeBar.php b/Functions/#Name-Value/PHP/Himaker_Filemaker_EncodeBar.php new file mode 100644 index 0000000..9f2f78a --- /dev/null +++ b/Functions/#Name-Value/PHP/Himaker_Filemaker_EncodeBar.php @@ -0,0 +1,168 @@ +parseToBar(); + } + + /** + * Himaker_Filemaker_DecoderToBar constructor. + * @param $source json + */ + public function __construct($source) + { + $this->_source = $source; + $this->_currentLevel = -1; + } + + /** + * Main call to parser + * @return mixed + */ + public function parseToBar(){ + + $this->_arrayData = json_decode($this->_source, true); + $data = $this->parseItem(null,$this->_arrayData)." ;"; + + return $data; + + } + + /** + * Parser item + * @return mixed + */ + public function parseItem($key, $item){ + + $data = ""; + + //choose correct parser + if(is_array($item)){ + $data .= $this->parseArray($key,$item); + } else { + $data .= $this->parseValue($key,$item); + } + + return $data; + + } + + /** + * Parser value + * @return mixed + */ + public function parseValue($key, $item){ + + //verify level to write correct syntax + $extra =''; + if($this->_currentLevel == $this->_lastLevel) + $extra .= $this->writeBars($this->_currentLevel-1).'"'; + + //verify array type to writte correct + if(!is_int($key)){ + return $extra.'$'.$key.' = '.$this->writeBars($this->_currentLevel).'"'.$item.$this->writeBars($this->_currentLevel).'";'.$this->writeBars($this->_currentLevel-1).'¶'.''; + } else { + return $extra.$item.$this->writeBars($this->_currentLevel).'"'.$this->writeBars($this->_currentLevel-1).'¶'.''; + + } + } + + /** + * Parser array + * @return mixed + */ + public function parseArray($key, $array) + { + + //add one deep level + $this->_currentLevel++; + $data = ""; + + //verify array type and position + if (!is_int($key) && $this->_currentLevel > 0){ + + if ($this->_currentLevel ==1 ) + $data .= '$' . $key . ' = "' . $this->writeBars($this->_currentLevel) . '"'.''; + else + $data .= '$' . $key . ' = '.$this->writeBars($this->_currentLevel-1).'"' . $this->writeBars($this->_currentLevel) . '"'.''; + + } + + //send child's to parser + foreach ($array as $keyChild => $itemChild ) { + $data .= $this->parseItem($keyChild,$itemChild); + } + + //save last deep level + $this->_lastLevel = $this->_currentLevel; + + //remove emove one deep level + $this->_currentLevel--; + + //write close command's + if ($this->_currentLevel > 1){ + + if(!is_int($key)) + $data .= $this->writeBars($this->_currentLevel).'" ;' . $this->writeBars($this->_currentLevel-1) . '¶'.''; + else + $data .= $this->writeBars($this->_currentLevel).'"' . $this->writeBars($this->_currentLevel-1) . '¶'.''; + } + + //final instructions + if ($this->_currentLevel == 0){ + $data .= $this->writeBars($this->_currentLevel).'"¶"'; + } + + return $data; + } + + /** + * Write \'s + * + * @param $level + * @return string + */ + public function writeBars($level){ + + $barras = pow(2,$level); + + if($barras > 1) + $barras = $barras-1; + + $stringbarras =''; + for($i=0; $i<$barras; $i++){ + $stringbarras .='\\'; + } + + return $stringbarras; + + } + + +}