API - add field options to getoptions metadata
[civicrm-core.git] / CRM / Utils / String.php
index fcabb6b7d8ea529f9f5ada767f915c409bc00252..b561cad091520ce2d7915a0d803cabe34aff5ade 100644 (file)
  | GNU Affero General Public License or the licensing of CiviCRM,     |
  | see the CiviCRM license FAQ at http://civicrm.org/licensing        |
  +--------------------------------------------------------------------+
-*/
+ */
 
 /**
  *
  * @package CRM
  * @copyright CiviCRM LLC (c) 2004-2014
  * $Id$
- *
  */
 
 require_once 'HTML/QuickForm/Rule/Email.php';
 
 /**
- * This class contains string functions
+ * This class contains string functions.
  *
  */
 class CRM_Utils_String {
@@ -48,16 +47,13 @@ class CRM_Utils_String {
   const ALPHANUMERIC = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890';
 
   /**
-   * Convert a display name into a potential variable
-   * name that we could use in forms/code
-   *
-   * @param name Name of the string
+   * Convert a display name into a potential variable name.
    *
+   * @param $title title of the string
    * @param int $maxLength
    *
    * @return string
    *   An equivalent variable name.
-   * @static
    */
   public static function titleToVar($title, $maxLength = 31) {
     $variable = self::munge($title, '_', $maxLength);
@@ -72,8 +68,7 @@ class CRM_Utils_String {
   }
 
   /**
-   * Given a string, replace all non alpha numeric characters and
-   * spaces with the replacement character
+   * Replace all non alpha numeric characters and spaces with the replacement character.
    *
    * @param string $name
    *   The name to be worked on.
@@ -82,12 +77,11 @@ class CRM_Utils_String {
    * @param int $len
    *   Length of valid variables.
    *
-   *
-   * @return string returns the manipulated string
-   * @static
+   * @return string
+   *   returns the manipulated string
    */
   public static function munge($name, $char = '_', $len = 63) {
-    // replace all white space and non-alpha numeric with $char
+    // Replace all white space and non-alpha numeric with $char
     // we only use the ascii character set since mysql does not create table names / field names otherwise
     // CRM-11744
     $name = preg_replace('/[^a-zA-Z0-9]+/', $char, trim($name));
@@ -103,11 +97,11 @@ class CRM_Utils_String {
 
   /**
    * Convert possibly underscore separated words to camel case with special handling for 'UF'
-   * e.g
-   * membership_payment returns MembershipPayment
+   * e.g membership_payment returns MembershipPayment
+   *
    * @param string $string
    *
-   * @return string string
+   * @return string
    */
   public static function convertStringToCamel($string) {
     $fragments = explode('_', $string);
@@ -122,16 +116,15 @@ class CRM_Utils_String {
   }
 
   /**
-   *
-   * Takes a variable name and munges it randomly into another variable name
+   * Takes a variable name and munges it randomly into another variable name.
    *
    * @param string $name
    *   Initial Variable Name.
    * @param int $len
    *   Length of valid variables.
    *
-   * @return string  Randomized Variable Name
-   * @static
+   * @return string
+   *   Randomized Variable Name
    */
   public static function rename($name, $len = 4) {
     $rand = substr(uniqid(), 0, $len);
@@ -140,15 +133,16 @@ class CRM_Utils_String {
 
   /**
    * Takes a string and returns the last tuple of the string.
-   * useful while converting file names to class names etc
+   *
+   * Useful while converting file names to class names etc
    *
    * @param string $string
    *   The input string.
-   * @param \char|string $char $char the character used to demarcate the componets
-   *
+   * @param string $char
+   *   Character used to demarcate the components
    *
-   * @return string the last component
-   * @static
+   * @return string
+   *   The last component
    */
   public static function getClassName($string, $char = '_') {
     $names = array();
@@ -161,8 +155,9 @@ class CRM_Utils_String {
   }
 
   /**
-   * Appends a name to a string and seperated by delimiter.
-   * does the right thing for an empty string
+   * Appends a name to a string and separated by delimiter.
+   *
+   * Does the right thing for an empty string
    *
    * @param string $str
    *   The string to be appended to.
@@ -170,9 +165,6 @@ class CRM_Utils_String {
    *   The delimiter to use.
    * @param mixed $name
    *   The string (or array of strings) to append.
-   *
-   * @return void
-   * @static
    */
   public static function append(&$str, $delim, $name) {
     if (empty($name)) {
@@ -203,15 +195,15 @@ class CRM_Utils_String {
   }
 
   /**
-   * Determine if the string is composed only of ascii characters
+   * Determine if the string is composed only of ascii characters.
    *
    * @param string $str
    *   Input string.
    * @param bool $utf8
    *   Attempt utf8 match on failure (default yes).
    *
-   * @return boolean    true if string is ascii
-   * @static
+   * @return bool
+   *   true if string is ascii
    */
   public static function isAscii($str, $utf8 = TRUE) {
     if (!function_exists('mb_detect_encoding')) {
@@ -246,7 +238,7 @@ class CRM_Utils_String {
   }
 
   /**
-   * Determine the string replacements for redaction
+   * Determine the string replacements for redaction.
    * on the basis of the regular expressions
    *
    * @param string $str
@@ -254,8 +246,8 @@ class CRM_Utils_String {
    * @param array $regexRules
    *   Regular expression to be matched w/ replacements.
    *
-   * @return array $match      array of strings w/ corresponding redacted outputs
-   * @static
+   * @return array
+   *   array of strings w/ corresponding redacted outputs
    */
   public static function regex($str, $regexRules) {
     //redact the regular expressions
@@ -312,9 +304,8 @@ class CRM_Utils_String {
    *
    * @param string $str
    *   Input string.
-   * @static
    *
-   * @return boolean
+   * @return bool
    */
   public static function isUtf8($str) {
     if (!function_exists(mb_detect_encoding)) {
@@ -322,9 +313,9 @@ class CRM_Utils_String {
       $str = preg_replace('/\s+/', '', $str);
 
       /* pattern stolen from the php.net function documentation for
-             * utf8decode();
-             * comment by JF Sebastian, 30-Mar-2005
-             */
+       * utf8decode();
+       * comment by JF Sebastian, 30-Mar-2005
+       */
 
       return preg_match('/^([\x00-\x7f]|[\xc2-\xdf][\x80-\xbf]|\xe0[\xa0-\xbf][\x80-\xbf]|[\xe1-\xec][\x80-\xbf]{2}|\xed[\x80-\x9f][\x80-\xbf]|[\xee-\xef][\x80-\xbf]{2}|f0[\x90-\xbf][\x80-\xbf]{2}|[\xf1-\xf3][\x80-\xbf]{3}|\xf4[\x80-\x8f][\x80-\xbf]{2})*$/', $str);
       // ||
@@ -344,8 +335,8 @@ class CRM_Utils_String {
    * @param string $url2
    *   The second url to be matched against.
    *
-   * @return boolean true if the urls match, else false
-   * @static
+   * @return bool
+   *   true if the urls match, else false
    */
   public static function match($url1, $url2) {
     $url1 = strtolower($url1);
@@ -363,13 +354,13 @@ class CRM_Utils_String {
   }
 
   /**
-   * Extract variable values
+   * Extract the civicrm path from the url.
    *
-   * @param mix $query
-   *   This is basically url.
+   * @param string $query
+   *   A url string.
    *
-   * @return mix $v  returns civicrm url (eg: civicrm/contact/search/...)
-   * @static
+   * @return string|null
+   *   civicrm url (eg: civicrm/contact/search)
    */
   public static function extractURLVarValue($query) {
     $config = CRM_Core_Config::singleton();
@@ -393,8 +384,7 @@ class CRM_Utils_String {
    * @param string $str
    *   The string to be translated.
    *
-   * @return boolean
-   * @static
+   * @return bool
    */
   public static function strtobool($str) {
     if (!is_scalar($str)) {
@@ -413,8 +403,7 @@ class CRM_Utils_String {
    * @param string $str
    *   The string to be translated.
    *
-   * @return boolean
-   * @static
+   * @return bool
    */
   public static function strtoboolstr($str) {
     if (!is_scalar($str)) {
@@ -438,8 +427,8 @@ class CRM_Utils_String {
    * @param string $html
    *   The string to be converted.
    *
-   * @return string       the converted string
-   * @static
+   * @return string
+   *   the converted string
    */
   public static function htmlToText($html) {
     require_once 'packages/html2text/rcube_html2text.php';
@@ -530,7 +519,8 @@ class CRM_Utils_String {
    * @param string $full
    *   All alternatives as a long string (or some other text).
    *
-   * @return string       only the first alternative found (or the text without alternatives)
+   * @return string
+   *   only the first alternative found (or the text without alternatives)
    */
   public static function stripAlternatives($full) {
     $matches = array();
@@ -553,8 +543,8 @@ class CRM_Utils_String {
    * @param string $string
    *   Input string to be cleaned.
    *
-   * @return string the cleaned string
-   * @static
+   * @return string
+   *   the cleaned string
    */
   public static function stripSpaces($string) {
     return (empty($string)) ? $string : preg_replace("/\s{2,}/", " ", trim($string));
@@ -571,10 +561,10 @@ class CRM_Utils_String {
    * @param string $replace
    *   The character to replace it with.
    *
-   * @return string the sanitized string
-   * @static
+   * @return string
+   *   the sanitized string
    */
-  static function stripPathChars(
+  public static function stripPathChars(
     $string,
     $search = NULL,
     $replace = NULL
@@ -588,10 +578,23 @@ class CRM_Utils_String {
 
     if ($_searchChars == NULL) {
       $_searchChars = array(
-        '&', ';', ',', '=', '$',
-        '"', "'", '\\',
-        '<', '>', '(', ')',
-        ' ', "\r", "\r\n", "\n", "\t",
+        '&',
+        ';',
+        ',',
+        '=',
+        '$',
+        '"',
+        "'",
+        '\\',
+        '<',
+        '>',
+        '(',
+        ')',
+        ' ',
+        "\r",
+        "\r\n",
+        "\n",
+        "\t",
       );
       $_replaceChar = '_';
     }
@@ -616,8 +619,8 @@ class CRM_Utils_String {
    * @param string $string
    *   The input string.
    *
-   * @return string the cleaned up string
-   * @static
+   * @return string
+   *   the cleaned up string
    */
   public static function purifyHTML($string) {
     static $_filter = NULL;
@@ -653,7 +656,7 @@ class CRM_Utils_String {
   }
 
   /**
-   * Generate a random string
+   * Generate a random string.
    *
    * @param $len
    * @param $alphabet
@@ -678,7 +681,8 @@ class CRM_Utils_String {
    *   E.g. "view all contacts". Syntax: "[prefix:]name".
    * @param null $defaultPrefix
    *
-   * @return array (0 => string|NULL $prefix, 1 => string $value)
+   * @return array
+   *   (0 => string|NULL $prefix, 1 => string $value)
    */
   public static function parsePrefix($delim, $string, $defaultPrefix = NULL) {
     $pos = strpos($string, $delim);
@@ -700,7 +704,8 @@ class CRM_Utils_String {
    * @param int $percent
    *   The percentage of the user portion to be masked.
    *
-   * @return string returns the masked Email address
+   * @return string
+   *   returns the masked Email address
    */
   public static function maskEmail($email, $maskChar = '*', $percent = 50) {
     list($user, $domain) = preg_split("/@/", $email);
@@ -709,14 +714,14 @@ class CRM_Utils_String {
     $offset = floor(($len - $maskCount) / 2);
 
     $masked = substr($user, 0, $offset)
-       . str_repeat($maskChar, $maskCount)
-       . substr($user, $maskCount + $offset);
+      . str_repeat($maskChar, $maskCount)
+      . substr($user, $maskCount + $offset);
 
     return ($masked . '@' . $domain);
   }
 
   /**
-   * This function compares two strings
+   * This function compares two strings.
    *
    * @param string $strOne
    *   String one.
@@ -725,7 +730,8 @@ class CRM_Utils_String {
    * @param bool $case
    *   Boolean indicating whether you want the comparison to be case sensitive or not.
    *
-   * @return boolean TRUE (string are identical); FALSE (strings are not identical)
+   * @return bool
+   *   TRUE (string are identical); FALSE (strings are not identical)
    */
   public static function compareStr($strOne, $strTwo, $case) {
     if ($case == TRUE) {
@@ -758,11 +764,28 @@ class CRM_Utils_String {
    * (because most other odd characters are %-escaped in URLs; and %-escaped
    * strings don't need any extra escaping in HTML).
    *
-   * @param string $url
+   * @param string $htmlUrl
    *   URL with HTML entities.
-   * @return string URL without HTML entities
+   * @return string
+   *   URL without HTML entities
    */
   public static function unstupifyUrl($htmlUrl) {
     return str_replace('&amp;', '&', $htmlUrl);
   }
+
+  /**
+   * Formats a string of attributes for insertion in an html tag.
+   *
+   * @param array $attributes
+   *
+   * @return string
+   */
+  public static function htmlAttributes($attributes) {
+    $output = '';
+    foreach ($attributes as $name => $vals) {
+      $output .= " $name=\"" . htmlspecialchars(implode(' ', (array) $vals)) . '"';
+    }
+    return ltrim($output);
+  }
+
 }