/** * Ensures there is a choices array set * * @param array $options */ public function __construct($options = array()) { parent::__construct($options); // Ensure we have choices to gather values from if (empty($this->choices)) { throw new Kohana_Exception('Field_Enum must have a `choices` property set'); } // Convert non-associative values to associative ones if (!arr::is_assoc($this->choices)) { $this->choices = array_combine($this->choices, $this->choices); } }
/** * Convert an associative array to XML * If array is multi-dimensional, it will attempt to detect keyed vs ordered and build XML accordingly * * @param array associative array * @param string root element * @return string XML String */ public static function assoc2xml($assoc, $root_element = "xml") { if (!is_array($assoc) || count($assoc) == 0) { return "<" . $root_element . " />\n"; } foreach ($assoc as $key => $value) { if ($value === YES) { $value = "true"; } else { if ($value === NO) { $value = "false"; } else { if (!is_array($value) && strlen($value) == 0) { $xml .= "<" . $key . " />\n"; continue; } else { if (is_array($value)) { if (arr::is_assoc($value)) { $xml .= xml::assoc2xml($value, $key); } else { $xml .= xml::ordered2xml($value, $key); } continue; } else { if (self::use_cdata($value)) { $value = "<![CDATA[" . $value . "]]>"; } else { $value = htmlentities($value); } } } } } $xml .= "<" . $key . ">" . $value . "</" . $key . ">\n"; } return "<" . $root_element . ">\n" . xml::pad($xml) . "</" . $root_element . ">\n"; }
/** * Returns a single line representation of a variable. Internally, this is * used only for showing function arguments in stack traces. * * echo Kohana::debug_var($my_var); * * @param mixed variable to debug * @return string */ public static function debug_var($var) { switch (gettype($var)) { case 'null': return 'NULL'; break; case 'boolean': return $var ? 'TRUE' : 'FALSE'; break; case 'string': return var_export($var, TRUE); break; case 'object': return 'object ' . get_class($var); break; case 'array': if (arr::is_assoc($var)) { return print_r($var, TRUE); } return 'array(' . implode(', ', array_map(array(__CLASS__, __FUNCTION__), $var)) . ')'; break; default: return var_export($var, TRUE); break; } }
private static function parse_plist_value($value, $key = false, $tab_level = 0) { static $data_pattern = '/([0-9]{4}\\-[0-9]{2}\\-[0-9]{2}T[0-9]{2}\\:[0-9]{2}\\:[0-9]{2})(Z|[\\-\\+][0-9]{2}\\:[0-9]{2})/'; // Determine the type switch (true) { // integer case is_numeric($value) && is_float($value) === FALSE: $svalue = '<integer>' . $value . '</integer>'; break; // real // real case is_numeric($value) && is_float($value) === TRUE: $svalue = '<real>' . $value . '</real>'; break; // date // date case !is_array($value) && preg_match($data_pattern, $value): $value = preg_replace($data_pattern, '$1Z', $value); $svalue = '<date>' . $value . '</date>'; break; // string // string case is_string($value): // TODO: correctly parse & $svalue = '<string>' . htmlspecialchars(iconv('UTF-8', 'UTF-8//IGNORE', $value), ENT_NOQUOTES, 'UTF-8') . '</string>'; break; // true // true case $value === TRUE: $svalue = '<true />'; break; // false // false case $value === FALSE: $svalue = '<false />'; break; // dict // dict case is_array($value) && arr::is_assoc($value) === TRUE: $svalue = '<dict>' . plist::$eol; $tab_level++; foreach ($value as $vkey => $vvalue) { $svalue .= self::parse_plist_value($vvalue, $vkey, $tab_level); } $tab_level--; $svalue .= self::tabs($tab_level) . '</dict>'; $svalue; break; // array // array case is_array($value) && arr::is_assoc($value) === FALSE: $svalue = '<array>' . plist::$eol; $tab_level++; foreach ($value as $vvalue) { $svalue .= self::parse_plist_value($vvalue, FALSE, $tab_level); } $tab_level--; $svalue .= self::tabs($tab_level) . '</array>'; break; } return self::tabs($tab_level) . ($key ? '<key>' . $key . '</key>' . plist::$eol . self::tabs($tab_level) : '') . $svalue . plist::$eol; }
public static function parse_xml_value($value, $tag = '', $tab_level = 0) { $svalue = ''; // Determine the type switch (true) { // bool case is_bool($value): $svalue = "<{$tag}>" . ($value ? "1" : "0") . "</{$tag}>"; break; // string // string case is_string($value): if (empty($value)) { $svalue = "<{$tag} />"; } else { // TODO: correctly parse & if (stripos($value, '<![CDATA[') === 0) { $svalue = "<{$tag}>" . iconv('UTF-8', 'UTF-8//IGNORE', $value) . "</{$tag}>"; } else { $svalue = "<{$tag}>" . htmlspecialchars(iconv('UTF-8', 'UTF-8//IGNORE', $value), ENT_NOQUOTES, 'UTF-8') . "</{$tag}>"; } } break; // assoc array // assoc array case is_array($value): if (empty($value)) { $svalue = "<{$tag} />"; } elseif (arr::is_assoc($value)) { if ($tag != '') { $svalue = '<' . $tag . '>' . arr::$eol; $tab_level++; } foreach ($value as $vkey => $vvalue) { $svalue .= self::parse_xml_value($vvalue, $vkey, $tab_level); } if ($tag != '') { $tab_level--; $svalue .= self::tabs($tab_level) . '</' . $tag . '>'; } } else { if ($tag != '') { $svalue = "<{$tag}>" . arr::$eol; $tab_level++; } foreach ($value as $vvalue) { $svalue .= self::parse_xml_value($vvalue, arr::$xml_default_tag, $tab_level); } if ($tag != '') { $tab_level--; $svalue .= self::tabs($tab_level) . "</{$tag}>"; } } break; // others // others default: $svalue = "<{$tag}>{$value}</{$tag}>"; break; } return self::tabs($tab_level) . $svalue . arr::$eol; }