Browse Source

better return result

euromark 12 years ago
parent
commit
fd9a9d3d83
2 changed files with 16 additions and 14 deletions
  1. 12 10
      Lib/Utility/Utility.php
  2. 4 4
      Test/Case/Lib/Utility/UtilityTest.php

+ 12 - 10
Lib/Utility/Utility.php

@@ -13,7 +13,9 @@ App::uses('Router', 'Routing');
 class Utility {
 
 	/**
-	 * in_array itself has some PHP flaws regarding cross-type comparison
+	 * Clean implementation of inArray to avoid false positives.
+	 *
+	 * in_array itself has some PHP flaws regarding cross-type comparison:
 	 * - in_array('50x', array(40, 50, 60)) would be true!
 	 * - in_array(50, array('40x', '50x', '60x')) would be true!
 	 *
@@ -23,11 +25,11 @@ class Utility {
 	 */
 	public static function inArray($needle, $haystack) {
 		$strict = !is_numeric($needle);
-		return in_array((string )$needle, $haystack, $strict);
+		return in_array((string)$needle, $haystack, $strict);
 	}
 
 	/**
-	 * Multibyte analogue of preg_match_all() function.
+	 * Multibyte analogue of preg_match_all() function. Only that this returns the result.
 	 * By default this works properly with UTF8 strings.
 	 *
 	 * Note that you still need to add the u modifier (for UTF8) to your pattern yourself.
@@ -36,18 +38,18 @@ class Utility {
 	 *
 	 * @param string $pattern The pattern to use.
 	 * @param string $subject The string to match.
-	 * @param array $matches Array of all matches in multi-dimensional array ordered according to flags.
 	 * @param int $flags
 	 * @param int $offset
 	 * @return array Result
 	 */
-	public static function pregMatchAll($pattern, $subject, $matches, $flags = null, $offset = null) {
+	public static function pregMatchAll($pattern, $subject, $flags = PREG_SET_ORDER, $offset = null) {
 		$pattern = substr($pattern, 0, 1) . '(*UTF8)' . substr($pattern, 1);
-		return preg_match_all($pattern, $subject, $matches, $flags, $offset);
+		preg_match_all($pattern, $subject, $matches, $flags, $offset);
+		return $matches;
 	}
 
 	/**
-	 * Multibyte analogue of preg_match() function.
+	 * Multibyte analogue of preg_match() function. Only that this returns the result.
 	 * By default this works properly with UTF8 strings.
 	 *
 	 * Note that you still need to add the u modifier (for UTF8) to your pattern yourself.
@@ -56,14 +58,14 @@ class Utility {
 	 *
 	 * @param string $pattern The pattern to use.
 	 * @param string $subject The string to match.
-	 * @param array $matches Array of all matches in multi-dimensional array ordered according to flags.
 	 * @param int $flags
 	 * @param int $offset
 	 * @return array Result
 	 */
-	public static function pregMatch($pattern, $subject, $matches, $flags = null, $offset = null) {
+	public static function pregMatch($pattern, $subject, $flags = null, $offset = null) {
 		$pattern = substr($pattern, 0, 1) . '(*UTF8)' . substr($pattern, 1);
-		return preg_match($pattern, $subject, $matches, $flags, $offset);
+		preg_match($pattern, $subject, $matches, $flags, $offset);
+		return $matches;
 	}
 
 	/**

+ 4 - 4
Test/Case/Lib/Utility/UtilityTest.php

@@ -35,14 +35,14 @@ class UtilityTest extends MyCakeTestCase {
 		preg_match('/\<(\w+)\>/', $string, $matches);
 		$this->assertSame(array($string, 'abc'), $matches);
 
-		Utility::pregMatch('/\<(\w+)\>/', $string, $matches);
+		$matches = Utility::pregMatch('/\<(\w+)\>/', $string);
 		$this->assertSame(array($string, 'abc'), $matches);
 
 		$string = '<äöü>';
 		preg_match('/\<(.+)\>/', $string, $matches);
 		$this->assertSame(array($string, 'äöü'), $matches);
 
-		Utility::pregMatch('/\<(.+)\>/', $string, $matches);
+		$matches = Utility::pregMatch('/\<(.+)\>/', $string);
 		$this->assertSame(array($string, 'äöü'), $matches);
 
 		$string = 'D-81245 München';
@@ -56,7 +56,7 @@ class UtilityTest extends MyCakeTestCase {
 		$this->assertSame($expected, $matches);
 
 		// we dont need the utf8 hack:
-		Utility::pregMatch('/([\w+])-([a-z0-9]+)\s+\b([\w\s]+)\b/iu', $string, $matches);
+		$matches = Utility::pregMatch('/([\w+])-([a-z0-9]+)\s+\b([\w\s]+)\b/iu', $string);
 		$this->assertSame($expected, $matches);
 	}
 
@@ -74,7 +74,7 @@ class UtilityTest extends MyCakeTestCase {
 		$this->assertSame($expected, $matches);
 
 		// we dont need the utf8 hack:
-		Utility::pregMatchAll('/([\w+])-([a-z0-9]+)\s+\b([\w\s]+)\b/iu', $string, $matches);
+		$matches = Utility::pregMatchAll('/([\w+])-([a-z0-9]+)\s+\b([\w\s]+)\b/iu', $string);
 		$this->assertSame($expected, $matches);
 	}