/** * Escapes the defined value, depending to the defined type. * * @param mixed $str The value to escape * @param string $type The escaping type (One of the ESCAPE_* constants. * @return string * @uses \Beluga\escapeXML * @uses \url_encode * @uses \json_encode * @uses \Beluga\escapeXMLArg * @since v0.1 */ function escape(string $str, $type = ESCAPE_HTML_ALL) : string { switch ($type) { case ESCAPE_HTML: return escapeXML($str); case ESCAPE_URL: return \urlencode($str); case ESCAPE_JSON: return \json_encode($str); default: return escapeXMLArg($str); } }
/** * Builds a XML conform attribute string from a associative array (1 dimensional array!) * * If a array key not begins with a-z, A-Z or a underscore it is prefixed by a underscore! * * @param array $attributes The associative array, defining the attributes * @return string */ public static function CreateAttributeString(array $attributes) : string { if (empty($attributes) || static::IsNumericIndicated($attributes)) { // If no attributes are defined, or if the are not a associative array, return a empty string return ''; } $res = []; foreach ($attributes as $k => $v) { $key = $k; if (!\preg_match('~^[A-Za-z_]~', $k)) { // numeric keys (attribute names) should be prefixed with a underscore $key = '_' . $k; } if (\is_bool($v)) { $vl = $v ? '1' : '0'; } elseif (\is_int($v) || \is_double($v) || \is_float($v)) { $vl = \strval($v); } else { $vl = escapeXMLArg((string) $v); } $res[] = \sprintf('%s="%s"', $key, $vl); } return ' ' . \join(' ', $res); }