浏览代码

Allow tokenize to get a callable for each token run

Mark Scherer 10 年之前
父节点
当前提交
f1eb0e8fd9
共有 3 个文件被更改,包括 26 次插入6 次删除
  1. 6 4
      Controller/Component/CommonComponent.php
  2. 13 2
      Lib/Utility/Utility.php
  3. 7 0
      Test/Case/Lib/Utility/UtilityTest.php

+ 6 - 4
Controller/Component/CommonComponent.php

@@ -752,7 +752,7 @@ class CommonComponent extends Component {
 	 * @param bool $camelize (true/false): problems with äöüß etc!
 	 * @return array Results as list
 	 */
-	public function parseList($string, $separator = null, $camelize = false, $capitalize = true) {
+	public static function parseList($string, $separator = null, $camelize = false, $capitalize = true) {
 		if ($separator === null) {
 			$separator = ',';
 		}
@@ -785,6 +785,7 @@ class CommonComponent extends Component {
 	 *
 	 * @param string $s
 	 * @return mixed
+	 * @deprecated
 	 */
 	public static function separators($s = null, $valueOnly = false) {
 		$separatorsValues = [SEPARATOR_COMMA => ',', SEPARATOR_SEMI => ';', SEPARATOR_SPACE => ' ', SEPARATOR_TAB => TB, SEPARATOR_NL => NL];
@@ -806,11 +807,11 @@ class CommonComponent extends Component {
 
 	/**
 	 * Expects email to be valid!
-	 * TODO: move to Lib
 	 *
 	 * @return array email - pattern: array('email'=>,'name'=>)
+	 * @deprecated
 	 */
-	public function splitEmail($email, $abortOnError = false) {
+	public function splitEmail($email) {
 		$array = ['email' => '', 'name' => ''];
 		if (($pos = mb_strpos($email, '<')) !== false) {
 			$name = substr($email, 0, $pos);
@@ -831,9 +832,9 @@ class CommonComponent extends Component {
 	}
 
 	/**
-	 * TODO: move to Lib
 	 * @param string $email
 	 * @param string $name (optional, will use email otherwise)
+	 * @deprecated
 	 */
 	public function combineEmail($email, $name = null) {
 		if (empty($email)) {
@@ -856,6 +857,7 @@ class CommonComponent extends Component {
 	 * @param string $email: well formatted email! (containing one @ and one .)
 	 * @param string $type (TODO: defaults to return all elements)
 	 * @return string or false on failure
+	 * @deprecated
 	 */
 	public function extractEmailInfo($email, $type = null) {
 		//$checkpos = strrpos($email, '@');

+ 13 - 2
Lib/Utility/Utility.php

@@ -40,23 +40,34 @@ class Utility {
 	 */
 	public static function tokenize($data, $separator = ',', array $options = []) {
 		$defaults = [
-			'clean' => true
+			'clean' => true,
+			'callback' => null
 		];
 		$options += $defaults;
 		if (empty($data)) {
 			return [];
 		}
+
 		$tokens = explode($separator, $data);
+		$tokens = array_map('trim', $tokens);
+
+		if ($options['callback']) {
+			foreach ($tokens as $key => $token) {
+				debug($token);
+				$tokens[$key] = $options['callback']($token);
+			}
+		}
+
 		if (empty($tokens) || !$options['clean']) {
 			return $tokens;
 		}
 
-		$tokens = array_map('trim', $tokens);
 		foreach ($tokens as $key => $token) {
 			if ($token === '') {
 				unset($tokens[$key]);
 			}
 		}
+
 		return $tokens;
 	}
 

+ 7 - 0
Test/Case/Lib/Utility/UtilityTest.php

@@ -36,6 +36,9 @@ class UtilityTest extends MyCakeTestCase {
 		$this->assertFalse($res);
 	}
 
+	/**
+	 * @return void
+	 */
 	public function testTokenize() {
 		$res = Utility::tokenize('');
 		$this->assertSame([], $res);
@@ -48,6 +51,10 @@ class UtilityTest extends MyCakeTestCase {
 
 		$res = Utility::tokenize(',some,,, ,, thing,');
 		$this->assertSame(['some', 'thing'], array_values($res));
+
+		$options = ['callback' => function ($token) { return ucfirst($token); }];
+		$res = Utility::tokenize('some, thing', ',', $options);
+		$this->assertSame(['Some', 'Thing'], $res);
 	}
 
 	/**