PageRenderTime 60ms CodeModel.GetById 22ms RepoModel.GetById 1ms app.codeStats 0ms

/Croogo/Lib/Utility/StringConverter.php

https://github.com/kareypowell/croogo
PHP | 209 lines | 135 code | 13 blank | 61 comment | 33 complexity | b7c87d3c70b9120ea8f09f820688ff5a MD5 | raw file
  1. <?php
  2. /**
  3. * StringConverter
  4. *
  5. * @package Croogo.Croogo.Lib.Utility
  6. * @license http://www.opensource.org/licenses/mit-license.php The MIT License
  7. * @link http://www.croogo.org
  8. */
  9. class StringConverter {
  10. /**
  11. * Parses bb-code like string.
  12. *
  13. * Example: string containing [menu:main option1="value"] will return an array like
  14. *
  15. * Array
  16. * (
  17. * [main] => Array
  18. * (
  19. * [option1] => value
  20. * )
  21. * )
  22. *
  23. * @param string $exp
  24. * @param string $text
  25. * @param array $options
  26. * @return array
  27. */
  28. public function parseString($exp, $text, $options = array()) {
  29. $_options = array(
  30. 'convertOptionsToArray' => false,
  31. );
  32. $options = array_merge($_options, $options);
  33. $output = array();
  34. preg_match_all('/\[(' . $exp . '):([A-Za-z0-9_\-]*)(.*?)\]/i', $text, $tagMatches);
  35. for ($i = 0, $ii = count($tagMatches[1]); $i < $ii; $i++) {
  36. $regex = '/(\S+)=[\'"]?((?:.(?![\'"]?\s+(?:\S+)=|[>\'"]))+.)[\'"]?/i';
  37. preg_match_all($regex, $tagMatches[3][$i], $attributes);
  38. $alias = $tagMatches[2][$i];
  39. $aliasOptions = array();
  40. for ($j = 0, $jj = count($attributes[0]); $j < $jj; $j++) {
  41. $aliasOptions[$attributes[1][$j]] = $attributes[2][$j];
  42. }
  43. if ($options['convertOptionsToArray']) {
  44. foreach ($aliasOptions as $optionKey => $optionValue) {
  45. if (!is_array($optionValue) && strpos($optionValue, ':') !== false) {
  46. $aliasOptions[$optionKey] = $this->stringToArray($optionValue);
  47. }
  48. }
  49. }
  50. $output[$alias] = $aliasOptions;
  51. }
  52. return $output;
  53. }
  54. /**
  55. * Converts formatted string to array
  56. *
  57. * A string formatted like 'Node.type:blog;' will be converted to
  58. * array('Node.type' => 'blog');
  59. *
  60. * @param string $string in this format: Node.type:blog;Node.user_id:1;
  61. * @return array
  62. */
  63. public function stringToArray($string) {
  64. $string = explode(';', $string);
  65. $stringArr = array();
  66. foreach ($string as $stringElement) {
  67. if ($stringElement != null) {
  68. $stringElementE = explode(':', $stringElement);
  69. if (isset($stringElementE['1'])) {
  70. $value = $stringElementE['1'];
  71. if (strpos($value, ',') !== false) {
  72. $value = explode(',', $value);
  73. }
  74. $stringArr[$stringElementE['0']] = $value;
  75. } else {
  76. $stringArr[] = $stringElement;
  77. }
  78. }
  79. }
  80. return $stringArr;
  81. }
  82. /**
  83. * Converts strings like controller:abc/action:xyz/ to arrays
  84. *
  85. * @param string|array $link link
  86. * @return array
  87. */
  88. public function linkStringToArray($link) {
  89. if (is_array($link)) {
  90. $link = key($link);
  91. }
  92. if (($pos = strpos($link, '?')) !== false) {
  93. parse_str(substr($link, $pos + 1), $query);
  94. $link = substr($link, 0, $pos);
  95. }
  96. $link = explode('/', $link);
  97. $prefixes = Configure::read('Routing.prefixes');
  98. $linkArr = array_fill_keys($prefixes, false);
  99. foreach ($link as $linkElement) {
  100. if ($linkElement != null) {
  101. $linkElementE = explode(':', $linkElement);
  102. if (isset($linkElementE['1'])) {
  103. if (in_array($linkElementE['0'], $prefixes)) {
  104. $linkArr[$linkElementE['0']] = strcasecmp($linkElementE['1'], 'false') === 0 ? false : true;
  105. } else {
  106. $linkArr[$linkElementE['0']] = urldecode($linkElementE['1']);
  107. }
  108. } else {
  109. $linkArr[] = $linkElement;
  110. }
  111. }
  112. }
  113. if (!isset($linkArr['plugin'])) {
  114. $linkArr['plugin'] = false;
  115. }
  116. if (isset($query)) {
  117. $linkArr['?'] = $query;
  118. }
  119. return $linkArr;
  120. }
  121. /**
  122. * Converts array into string controller:abc/action:xyz/value1/value2?foo=bar
  123. *
  124. * @param array $url link
  125. * @return array
  126. */
  127. public function urlToLinkString($url) {
  128. $result = array();
  129. $actions = array_merge(array(
  130. 'admin' => false, 'plugin' => false,
  131. 'controller' => false, 'action' => false
  132. ),
  133. $url
  134. );
  135. $queryString = null;
  136. foreach ($actions as $key => $val) {
  137. if (is_string($key)) {
  138. if (is_bool($val)) {
  139. if ($val === true) {
  140. $result[] = $key;
  141. }
  142. } elseif ($key == '?') {
  143. $queryString = '?' . http_build_query($val);
  144. } else {
  145. $result[] = $key . ':' . $val;
  146. }
  147. } else {
  148. $result[] = $val;
  149. }
  150. }
  151. return join('/', $result) . $queryString;
  152. }
  153. /**
  154. * Extract the first paragraph from $text
  155. *
  156. * Options:
  157. *
  158. * - `tag` Wrap the returned value with a <p> tag. Default is `true`
  159. * - `regex` Regex expression to determine a paragraph
  160. * - `stripTags Strip all tags within the paragraph. Default is `true`
  161. * - `newline` Determine paragraph based on newlines instead of html <p> tag.
  162. * Default is false
  163. *
  164. * @param string $text Html text
  165. * @param array $options Options
  166. * @return string
  167. */
  168. public function firstPara($text, $options = array()) {
  169. $paragraph = null;
  170. $options = array_merge(array(
  171. 'tag' => false,
  172. 'regex' => '#<p[^>]*>(.*)</p>#isU',
  173. 'stripTags' => true,
  174. 'newline' => false,
  175. ), $options);
  176. if ($options['regex']) {
  177. preg_match($options['regex'], $text, $matches);
  178. if (isset($matches[1])) {
  179. $paragraph = $matches[1];
  180. }
  181. }
  182. if (empty($paragraph) && $options['newline']) {
  183. $paragraphs = preg_split('/\r\n|\r|\n/', $text);
  184. $paragraph = empty($paragraphs[0]) ? null : $paragraphs[0];
  185. }
  186. if ($paragraph) {
  187. if ($options['stripTags']) {
  188. $paragraph = strip_tags($paragraph);
  189. }
  190. if ($options['tag']) {
  191. $paragraph = '<p>' . $paragraph . '</p>';
  192. }
  193. }
  194. return $paragraph;
  195. }
  196. }