spyc.php
来自「QeePHP v2.1.2116 bulid 090120」· PHP 代码 · 共 742 行 · 第 1/2 页
PHP
742 行
// Propogate value array $value = array(); foreach ($explode as $v) { $value[] = $this->_toType($v); } } elseif (strpos($value,': ')!==false && !preg_match('/^{(.+)/',$value)) { // It's a map $array = explode(': ',$value); $key = trim($array[0]); array_shift($array); $value = trim(implode(': ',$array)); $value = $this->_toType($value); $value = array($key => $value); } elseif (preg_match("/{(.+)}$/",$value,$matches)) { // Inline Mapping // Take out strings sequences and mappings $explode = $this->_inlineEscape($matches[1]); // Propogate value array $array = array(); foreach ($explode as $v) { $array = $array + $this->_toType($v); } $value = $array; } elseif (strtolower($value) == 'null' or $value == '' or $value == '~') { $value = null; } elseif (preg_match ('/^[0-9]+$/', $value)) { $value = (int)$value; } elseif (in_array(strtolower($value), array('true', 'on', '+', 'yes', 'y'))) { $value = true; } elseif (in_array(strtolower($value), array('false', 'off', '-', 'no', 'n'))) { $value = false; } elseif (is_numeric($value)) { $value = (float)$value; } else { // Just a normal string, right? // $value = trim(preg_replace('/#(.+)$/','',$value)); } // print_r ($value); return $value; } /** * Used in inlines to check for more inlines or quoted strings * @access private * @return array */ private function _inlineEscape($inline) { // There's gotta be a cleaner way to do this... // While pure sequences seem to be nesting just fine, // pure mappings and mappings with sequences inside can't go very // deep. This needs to be fixed. $saved_strings = array(); // Check for strings $regex = '/(?:(")|(?:\'))((?(1)[^"]+|[^\']+))(?(1)"|\')/'; if (preg_match_all($regex,$inline,$strings)) { $saved_strings = $strings[0]; $inline = preg_replace($regex,'YAMLString',$inline); } unset($regex); // Check for sequences if (preg_match_all('/\[(.+)\]/U',$inline,$seqs)) { $inline = preg_replace('/\[(.+)\]/U','YAMLSeq',$inline); $seqs = $seqs[0]; } // Check for mappings if (preg_match_all('/{(.+)}/U',$inline,$maps)) { $inline = preg_replace('/{(.+)}/U','YAMLMap',$inline); $maps = $maps[0]; } $explode = explode(', ',$inline); // Re-add the sequences if (!empty($seqs)) { $i = 0; foreach ($explode as $key => $value) { if (strpos($value,'YAMLSeq') !== false) { $explode[$key] = str_replace('YAMLSeq',$seqs[$i],$value); ++$i; } } } // Re-add the mappings if (!empty($maps)) { $i = 0; foreach ($explode as $key => $value) { if (strpos($value,'YAMLMap') !== false) { $explode[$key] = str_replace('YAMLMap',$maps[$i],$value); ++$i; } } } // Re-add the strings if (!empty($saved_strings)) { $i = 0; foreach ($explode as $key => $value) { while (strpos($value,'YAMLString') !== false) { $explode[$key] = preg_replace('/YAMLString/',$saved_strings[$i],$value, 1); ++$i; $value = $explode[$key]; } } } return $explode; } private function literalBlockContinues ($line, $lineIndent) { if (!trim($line)) return true; if ($this->_getIndent($line) > $lineIndent) return true; return false; } private function addArray ($array, $indent) { $key = key ($array); if (!isset ($array[$key])) return false; if ($array[$key] === array()) { $array[$key] = ''; }; $value = $array[$key]; // Unfolding inner array tree as defined in $this->_arrpath. //$_arr = $this->result; $_tree[0] = $_arr; $i = 1; $tempPath = Spyc::flatten ($this->path); eval ('$_arr = $this->result' . $tempPath . ';'); if ($this->_containsGroupAlias) { do { if (!isset($this->SavedGroups[$this->_containsGroupAlias])) { echo "Bad group name: $this->_containsGroupAlias."; break; } $groupPath = $this->SavedGroups[$this->_containsGroupAlias]; eval ('$value = $this->result' . Spyc::flatten ($groupPath) . ';'); } while (false); $this->_containsGroupAlias = false; } // Adding string or numeric key to the innermost level or $this->arr. if ($key) $_arr[$key] = $value; else { if (!is_array ($_arr)) { $_arr = array ($value); $key = 0; } else { $_arr[] = $value; end ($_arr); $key = key ($_arr); } } $this->path[$indent] = $key; eval ('$this->result' . $tempPath . ' = $_arr;'); if ($this->_containsGroupAnchor) { $this->SavedGroups[$this->_containsGroupAnchor] = $this->path; $this->_containsGroupAnchor = false; } } private function flatten ($array) { $tempPath = array(); if (!empty ($array)) { foreach ($array as $_) { if (!is_int($_)) $_ = "'$_'"; $tempPath[] = "[$_]"; } } //end ($tempPath); $latestKey = key($tempPath); $tempPath = implode ('', $tempPath); return $tempPath; } private function startsLiteralBlock ($line) { $lastChar = substr (trim($line), -1); if (in_array ($lastChar, $this->LiteralBlockMarkers)) return $lastChar; return false; } private function addLiteralLine ($literalBlock, $line, $literalBlockStyle) { $line = $this->stripIndent($line); $line = str_replace ("\r\n", "\n", $line); if ($literalBlockStyle == '|') { return $literalBlock . $line; } if (strlen($line) == 0) return $literalBlock . "\n"; // echo "|$line|"; if ($line != "\n") $line = trim ($line, "\r\n ") . " "; return $literalBlock . $line; } private function revertLiteralPlaceHolder ($lineArray, $literalBlock) { foreach ($lineArray as $k => $_) { if (substr($_, -1 * strlen ($this->LiteralPlaceHolder)) == $this->LiteralPlaceHolder) $lineArray[$k] = rtrim ($literalBlock, " \r\n"); } return $lineArray; } private function stripIndent ($line, $indent = -1) { if ($indent == -1) $indent = $this->_getIndent($line); return substr ($line, $indent); } private function getParentPathByIndent ($indent) { if ($indent == 0) return array(); $linePath = $this->path; do { end($linePath); $lastIndentInParentPath = key($linePath); if ($indent <= $lastIndentInParentPath) array_pop ($linePath); } while ($indent <= $lastIndentInParentPath); return $linePath; } private function clearBiggerPathValues ($indent) { if ($indent == 0) $this->path = array(); if (empty ($this->path)) return true; foreach ($this->path as $k => $_) { if ($k > $indent) unset ($this->path[$k]); } return true; } private function isComment ($line) { if (preg_match('/^#/', $line)) return true; return false; } private function isArrayElement ($line) { if (!$line) return false; if ($line[0] != '-') return false; if (strlen ($line) > 3) if (substr($line,0,3) == '---') return false; return true; } private function isHashElement ($line) { if (!preg_match('/^(.+?):/', $line, $matches)) return false; $allegedKey = $matches[1]; if ($allegedKey) return true; //if (substr_count($allegedKey, ) return false; } private function isLiteral ($line) { if ($this->isArrayElement($line)) return false; if ($this->isHashElement($line)) return false; return true; } private function startsMappedSequence ($line) { if (preg_match('/^-(.*):$/',$line)) return true; } private function returnMappedSequence ($line) { $array = array(); $key = trim(substr(substr($line,1),0,-1)); $array[$key] = ''; return $array; } private function returnMappedValue ($line) { $array = array(); $key = trim(substr($line,0,-1)); $array[$key] = ''; return $array; } private function startsMappedValue ($line) { if (preg_match('/^(.*):$/',$line)) return true; } private function returnKeyValuePair ($line) { $array = array(); if (preg_match('/^(.+):/',$line,$key)) { // It's a key/value pair most likely // If the key is in double quotes pull it out if (preg_match('/^(["\'](.*)["\'](\s)*:)/',$line,$matches)) { $value = trim(str_replace($matches[1],'',$line)); $key = $matches[2]; } else { // Do some guesswork as to the key and the value $explode = explode(':',$line); $key = trim($explode[0]); array_shift($explode); $value = trim(implode(':',$explode)); } // Set the type of the value. Int, string, etc $value = $this->_toType($value); if (empty($key)) { $array[] = $value; } else { $array[$key] = $value; } } return $array; } private function returnArrayElement ($line) { if (strlen($line) <= 1) return array(array()); // Weird %) $array = array(); $value = trim(substr($line,1)); $value = $this->_toType($value); $array[] = $value; return $array; } private function nodeContainsGroup ($line) { if (strpos($line, '&') === false && strpos($line, '*') === false) return false; // Please die fast ;-) if (preg_match('/^(&[^ ]+)/', $line, $matches)) return $matches[1]; if (preg_match('/^(\*[^ ]+)/', $line, $matches)) return $matches[1]; if (preg_match('/(&[^" ]+$)/', $line, $matches)) return $matches[1]; if (preg_match('/(\*[^" ]+$)/', $line, $matches)) return $matches[1]; return false; } private function addGroup ($line, $group) { if (substr ($group, 0, 1) == '&') $this->_containsGroupAnchor = substr ($group, 1); if (substr ($group, 0, 1) == '*') $this->_containsGroupAlias = substr ($group, 1); //print_r ($this->path); } private function stripGroup ($line, $group) { $line = trim(str_replace($group, '', $line)); return $line; }}?>
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?