|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Part of Windwalker project. |
| 4 | + * |
| 5 | + * @copyright Copyright (C) 2014 {ORGANIZATION}. All rights reserved. |
| 6 | + * @license GNU General Public License version 2 or later; |
| 7 | + */ |
| 8 | + |
| 9 | +namespace Windwalker\String; |
| 10 | + |
| 11 | +use Windwalker\Utilities\ArrayHelper; |
| 12 | + |
| 13 | +/** |
| 14 | + * The String class. |
| 15 | + * |
| 16 | + * @since {DEPLOY_VERSION} |
| 17 | + */ |
| 18 | +abstract class String |
| 19 | +{ |
| 20 | + /** |
| 21 | + * Increment styles. |
| 22 | + * |
| 23 | + * @var array |
| 24 | + * @since {DEPLOY_VERSION} |
| 25 | + */ |
| 26 | + protected static $incrementStyles = array( |
| 27 | + 'dash' => array( |
| 28 | + '#-(\d+)$#', |
| 29 | + '-%d' |
| 30 | + ), |
| 31 | + 'default' => array( |
| 32 | + array('#\((\d+)\)$#', '#\(\d+\)$#'), |
| 33 | + array(' (%d)', '(%d)'), |
| 34 | + ), |
| 35 | + ); |
| 36 | + |
| 37 | + /** |
| 38 | + * isEmptyString |
| 39 | + * |
| 40 | + * @param string $string |
| 41 | + * |
| 42 | + * @return boolean |
| 43 | + */ |
| 44 | + public static function isEmpty($string) |
| 45 | + { |
| 46 | + if (is_array($string) || is_object($string)) |
| 47 | + { |
| 48 | + return empty($string); |
| 49 | + } |
| 50 | + |
| 51 | + $string = (string) $string; |
| 52 | + |
| 53 | + return !(boolean) strlen($string); |
| 54 | + } |
| 55 | + |
| 56 | + /** |
| 57 | + * isZero |
| 58 | + * |
| 59 | + * @param string $string |
| 60 | + * |
| 61 | + * @return boolean |
| 62 | + */ |
| 63 | + public static function isZero($string) |
| 64 | + { |
| 65 | + return $string === '0' || $string === 0; |
| 66 | + } |
| 67 | + |
| 68 | + /** |
| 69 | + * Quote a string. |
| 70 | + * |
| 71 | + * @param string $string The string to quote. |
| 72 | + * @param string $quote The quote symbol. |
| 73 | + * |
| 74 | + * @return string Quoted string. |
| 75 | + */ |
| 76 | + public static function quote($string, $quote = "''") |
| 77 | + { |
| 78 | + if (!strlen($quote)) |
| 79 | + { |
| 80 | + return $string; |
| 81 | + } |
| 82 | + |
| 83 | + if (empty($quote[1])) |
| 84 | + { |
| 85 | + $quote[1] = $quote[0]; |
| 86 | + } |
| 87 | + |
| 88 | + return $quote[0] . $string . $quote[1]; |
| 89 | + } |
| 90 | + |
| 91 | + /** |
| 92 | + * Back quote a string. |
| 93 | + * |
| 94 | + * @param string $string The string to quote. |
| 95 | + * |
| 96 | + * @return string Quoted string. |
| 97 | + */ |
| 98 | + public static function backquote($string) |
| 99 | + { |
| 100 | + return static::quote($string, '``'); |
| 101 | + } |
| 102 | + |
| 103 | + /** |
| 104 | + * Parse variable and replace it. This method is a simple template engine. |
| 105 | + * |
| 106 | + * Example: The {{ foo.bar.yoo }} will be replace to value of `$data['foo']['bar']['yoo']` |
| 107 | + * |
| 108 | + * @param string $string The template to replace. |
| 109 | + * @param array $data The data to find. |
| 110 | + * @param array $tags The variable tags. |
| 111 | + * |
| 112 | + * @return string Replaced template. |
| 113 | + */ |
| 114 | + public static function parseVariable($string, $data = array(), $tags = array('{{', '}}')) |
| 115 | + { |
| 116 | + return preg_replace_callback( |
| 117 | + '/\{\{\s*(.+?)\s*\}\}/', |
| 118 | + function($match) use ($data) |
| 119 | + { |
| 120 | + $return = ArrayHelper::getByPath($data, $match[1]); |
| 121 | + |
| 122 | + if (is_array($return) || is_object($return)) |
| 123 | + { |
| 124 | + return print_r($return, 1); |
| 125 | + } |
| 126 | + else |
| 127 | + { |
| 128 | + return $return; |
| 129 | + } |
| 130 | + }, |
| 131 | + $string |
| 132 | + ); |
| 133 | + } |
| 134 | + |
| 135 | + /** |
| 136 | + * Increments a trailing number in a string. |
| 137 | + * |
| 138 | + * Used to easily create distinct labels when copying objects. The method has the following styles: |
| 139 | + * |
| 140 | + * default: "Label" becomes "Label (2)" |
| 141 | + * dash: "Label" becomes "Label-2" |
| 142 | + * |
| 143 | + * @param string $string The source string. |
| 144 | + * @param string $style The the style (default|dash). |
| 145 | + * @param integer $n If supplied, this number is used for the copy, otherwise it is the 'next' number. |
| 146 | + * |
| 147 | + * @return string The incremented string. |
| 148 | + * |
| 149 | + * @since {DEPLOY_VERSION} |
| 150 | + */ |
| 151 | + public static function increment($string, $style = 'default', $n = 0) |
| 152 | + { |
| 153 | + $styleSpec = isset(self::$incrementStyles[$style]) ? self::$incrementStyles[$style] : self::$incrementStyles['default']; |
| 154 | + |
| 155 | + // Regular expression search and replace patterns. |
| 156 | + if (is_array($styleSpec[0])) |
| 157 | + { |
| 158 | + $rxSearch = $styleSpec[0][0]; |
| 159 | + $rxReplace = $styleSpec[0][1]; |
| 160 | + } |
| 161 | + else |
| 162 | + { |
| 163 | + $rxSearch = $rxReplace = $styleSpec[0]; |
| 164 | + } |
| 165 | + |
| 166 | + // New and old (existing) sprintf formats. |
| 167 | + if (is_array($styleSpec[1])) |
| 168 | + { |
| 169 | + $newFormat = $styleSpec[1][0]; |
| 170 | + $oldFormat = $styleSpec[1][1]; |
| 171 | + } |
| 172 | + else |
| 173 | + { |
| 174 | + $newFormat = $oldFormat = $styleSpec[1]; |
| 175 | + } |
| 176 | + |
| 177 | + // Check if we are incrementing an existing pattern, or appending a new one. |
| 178 | + if (preg_match($rxSearch, $string, $matches)) |
| 179 | + { |
| 180 | + $n = empty($n) ? ($matches[1] + 1) : $n; |
| 181 | + $string = preg_replace($rxReplace, sprintf($oldFormat, $n), $string); |
| 182 | + } |
| 183 | + else |
| 184 | + { |
| 185 | + $n = empty($n) ? 2 : $n; |
| 186 | + $string .= sprintf($newFormat, $n); |
| 187 | + } |
| 188 | + |
| 189 | + return $string; |
| 190 | + } |
| 191 | +} |
0 commit comments