[ Index ]

PHP Cross Reference of Joomla 4.2.2 documentation

title

Body

[close]

/libraries/vendor/joomla/string/src/ -> Normalise.php (source)

   1  <?php
   2  /**
   3   * Part of the Joomla Framework String Package
   4   *
   5   * @copyright  Copyright (C) 2005 - 2021 Open Source Matters, Inc. All rights reserved.
   6   * @license    GNU General Public License version 2 or later; see LICENSE
   7   */
   8  
   9  namespace Joomla\String;
  10  
  11  /**
  12   * Joomla Framework String Normalise Class
  13   *
  14   * @since  1.0
  15   */
  16  abstract class Normalise
  17  {
  18      /**
  19       * Method to convert a string from camel case.
  20       *
  21       * This method offers two modes. Grouped allows for splitting on groups of uppercase characters as follows:
  22       *
  23       * "FooBarABCDef"            becomes  array("Foo", "Bar", "ABC", "Def")
  24       * "JFooBar"                 becomes  array("J", "Foo", "Bar")
  25       * "J001FooBar002"           becomes  array("J001", "Foo", "Bar002")
  26       * "abcDef"                  becomes  array("abc", "Def")
  27       * "abc_defGhi_Jkl"          becomes  array("abc_def", "Ghi_Jkl")
  28       * "ThisIsA_NASAAstronaut"   becomes  array("This", "Is", "A_NASA", "Astronaut"))
  29       * "JohnFitzgerald_Kennedy"  becomes  array("John", "Fitzgerald_Kennedy"))
  30       *
  31       * Non-grouped will split strings at each uppercase character.
  32       *
  33       * @param   string   $input    The string input (ASCII only).
  34       * @param   boolean  $grouped  Optionally allows splitting on groups of uppercase characters.
  35       *
  36       * @return  array|string  The space separated string, as an array if grouped.
  37       *
  38       * @since   1.0
  39       */
  40  	public static function fromCamelCase($input, $grouped = false)
  41      {
  42          return $grouped
  43              ? preg_split('/(?<=[^A-Z_])(?=[A-Z])|(?<=[A-Z])(?=[A-Z][^A-Z_])/x', $input)
  44              : trim(preg_replace('#([A-Z])#', ' $1', $input));
  45      }
  46  
  47      /**
  48       * Method to convert a string into camel case.
  49       *
  50       * @param   string  $input  The string input (ASCII only).
  51       *
  52       * @return  string  The camel case string.
  53       *
  54       * @since   1.0
  55       */
  56  	public static function toCamelCase($input)
  57      {
  58          // Convert words to uppercase and then remove spaces.
  59          $input = static::toSpaceSeparated($input);
  60          $input = ucwords($input);
  61          $input = str_ireplace(' ', '', $input);
  62  
  63          return $input;
  64      }
  65  
  66      /**
  67       * Method to convert a string into dash separated form.
  68       *
  69       * @param   string  $input  The string input (ASCII only).
  70       *
  71       * @return  string  The dash separated string.
  72       *
  73       * @since   1.0
  74       */
  75  	public static function toDashSeparated($input)
  76      {
  77          // Convert spaces and underscores to dashes.
  78          return preg_replace('#[ \-_]+#', '-', $input);
  79      }
  80  
  81      /**
  82       * Method to convert a string into space separated form.
  83       *
  84       * @param   string  $input  The string input (ASCII only).
  85       *
  86       * @return  string  The space separated string.
  87       *
  88       * @since   1.0
  89       */
  90  	public static function toSpaceSeparated($input)
  91      {
  92          // Convert underscores and dashes to spaces.
  93          return preg_replace('#[ \-_]+#', ' ', $input);
  94      }
  95  
  96      /**
  97       * Method to convert a string into underscore separated form.
  98       *
  99       * @param   string  $input  The string input (ASCII only).
 100       *
 101       * @return  string  The underscore separated string.
 102       *
 103       * @since   1.0
 104       */
 105  	public static function toUnderscoreSeparated($input)
 106      {
 107          // Convert spaces and dashes to underscores.
 108          return preg_replace('#[ \-_]+#', '_', $input);
 109      }
 110  
 111      /**
 112       * Method to convert a string into variable form.
 113       *
 114       * @param   string  $input  The string input (ASCII only).
 115       *
 116       * @return  string  The variable string.
 117       *
 118       * @since   1.0
 119       */
 120  	public static function toVariable($input)
 121      {
 122          // Remove dashes and underscores, then convert to camel case.
 123          $input = static::toCamelCase($input);
 124  
 125          // Remove leading digits.
 126          $input = preg_replace('#^[0-9]+#', '', $input);
 127  
 128          // Lowercase the first character.
 129          $input = lcfirst($input);
 130  
 131          return $input;
 132      }
 133  
 134      /**
 135       * Method to convert a string into key form.
 136       *
 137       * @param   string  $input  The string input (ASCII only).
 138       *
 139       * @return  string  The key string.
 140       *
 141       * @since   1.0
 142       */
 143  	public static function toKey($input)
 144      {
 145          // Remove spaces and dashes, then convert to lower case.
 146          return strtolower(static::toUnderscoreSeparated($input));
 147      }
 148  }


Generated: Wed Sep 7 05:41:13 2022 Chilli.vc Blog - For Webmaster,Blog-Writer,System Admin and Domainer