croogo /Croogo/Lib/Utility/StringConverter.php

Language PHP Lines 210
MD5 Hash b7c87d3c70b9120ea8f09f820688ff5a
Repository https://github.com/kareypowell/croogo.git View Raw File View Project SPDX
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
<?php

/**
 * StringConverter
 *
 * @package  Croogo.Croogo.Lib.Utility
 * @license  http://www.opensource.org/licenses/mit-license.php The MIT License
 * @link     http://www.croogo.org
 */
class StringConverter {

/**
 * Parses bb-code like string.
 *
 * Example: string containing [menu:main option1="value"] will return an array like
 *
 * Array
 * (
 *     [main] => Array
 *         (
 *             [option1] => value
 *         )
 * )
 *
 * @param string $exp
 * @param string $text
 * @param array  $options
 * @return array
 */
	public function parseString($exp, $text, $options = array()) {
		$_options = array(
			'convertOptionsToArray' => false,
		);
		$options = array_merge($_options, $options);

		$output = array();
		preg_match_all('/\[(' . $exp . '):([A-Za-z0-9_\-]*)(.*?)\]/i', $text, $tagMatches);
		for ($i = 0, $ii = count($tagMatches[1]); $i < $ii; $i++) {
			$regex = '/(\S+)=[\'"]?((?:.(?![\'"]?\s+(?:\S+)=|[>\'"]))+.)[\'"]?/i';
			preg_match_all($regex, $tagMatches[3][$i], $attributes);
			$alias = $tagMatches[2][$i];
			$aliasOptions = array();
			for ($j = 0, $jj = count($attributes[0]); $j < $jj; $j++) {
				$aliasOptions[$attributes[1][$j]] = $attributes[2][$j];
			}
			if ($options['convertOptionsToArray']) {
				foreach ($aliasOptions as $optionKey => $optionValue) {
					if (!is_array($optionValue) && strpos($optionValue, ':') !== false) {
						$aliasOptions[$optionKey] = $this->stringToArray($optionValue);
					}
				}
			}
			$output[$alias] = $aliasOptions;
		}
		return $output;
	}

/**
 * Converts formatted string to array
 *
 * A string formatted like 'Node.type:blog;' will be converted to
 * array('Node.type' => 'blog');
 *
 * @param string $string in this format: Node.type:blog;Node.user_id:1;
 * @return array
 */
	public function stringToArray($string) {
		$string = explode(';', $string);
		$stringArr = array();
		foreach ($string as $stringElement) {
			if ($stringElement != null) {
				$stringElementE = explode(':', $stringElement);
				if (isset($stringElementE['1'])) {
					$value = $stringElementE['1'];
					if (strpos($value, ',') !== false) {
						$value = explode(',', $value);
					}
					$stringArr[$stringElementE['0']] = $value;
				} else {
					$stringArr[] = $stringElement;
				}
			}
		}
		return $stringArr;
	}

/**
 * Converts strings like controller:abc/action:xyz/ to arrays
 *
 * @param string|array $link link
 * @return array
 */
	public function linkStringToArray($link) {
		if (is_array($link)) {
			$link = key($link);
		}
		if (($pos = strpos($link, '?')) !== false) {
			parse_str(substr($link, $pos + 1), $query);
			$link = substr($link, 0, $pos);
		}
		$link = explode('/', $link);
		$prefixes = Configure::read('Routing.prefixes');
		$linkArr = array_fill_keys($prefixes, false);
		foreach ($link as $linkElement) {
			if ($linkElement != null) {
				$linkElementE = explode(':', $linkElement);
				if (isset($linkElementE['1'])) {
					if (in_array($linkElementE['0'], $prefixes)) {
						$linkArr[$linkElementE['0']] = strcasecmp($linkElementE['1'], 'false') === 0 ? false : true;
					} else {
						$linkArr[$linkElementE['0']] = urldecode($linkElementE['1']);
					}
				} else {
					$linkArr[] = $linkElement;
				}
			}
		}
		if (!isset($linkArr['plugin'])) {
			$linkArr['plugin'] = false;
		}

		if (isset($query)) {
			$linkArr['?'] = $query;
		}

		return $linkArr;
	}

/**
 * Converts array into string controller:abc/action:xyz/value1/value2?foo=bar
 *
 * @param array $url link
 * @return array
 */
	public function urlToLinkString($url) {
		$result = array();
		$actions = array_merge(array(
			'admin' => false, 'plugin' => false,
			'controller' => false, 'action' => false
			),
			$url
		);
		$queryString = null;
		foreach ($actions as $key => $val) {
			if (is_string($key)) {
				if (is_bool($val)) {
					if ($val === true) {
						$result[] = $key;
					}
				} elseif ($key == '?') {
					$queryString = '?' . http_build_query($val);
				} else {
					$result[] = $key . ':' . $val;
				}
			} else {
				$result[] = $val;
			}
		}
		return join('/', $result) . $queryString;
	}

/**
 * Extract the first paragraph from $text
 *
 * Options:
 *
 * - `tag` Wrap the returned value with a <p> tag. Default is `true`
 * - `regex` Regex expression to determine a paragraph
 * - `stripTags Strip all tags within the paragraph. Default is `true`
 * - `newline` Determine paragraph based on newlines instead of html <p> tag.
 *    Default is false
 *
 * @param string $text Html text
 * @param array $options Options
 * @return string
 */
	public function firstPara($text, $options = array()) {
		$paragraph = null;
		$options = array_merge(array(
			'tag' => false,
			'regex' => '#<p[^>]*>(.*)</p>#isU',
			'stripTags' => true,
			'newline' => false,
		), $options);

		if ($options['regex']) {
			preg_match($options['regex'], $text, $matches);
			if (isset($matches[1])) {
				$paragraph = $matches[1];
			}
		}

		if (empty($paragraph) && $options['newline']) {
			$paragraphs = preg_split('/\r\n|\r|\n/', $text);
			$paragraph = empty($paragraphs[0]) ? null : $paragraphs[0];
		}

		if ($paragraph) {
			if ($options['stripTags']) {
				$paragraph = strip_tags($paragraph);
			}
			if ($options['tag']) {
				$paragraph = '<p>' . $paragraph . '</p>';
			}
		}
		return $paragraph;
	}

}
Back to Top