OSDN Git Service

array_shrink_leaves()
authorhenoheno <henoheno>
Sat, 5 May 2007 08:49:10 +0000 (17:49 +0900)
committerhenoheno <henoheno>
Sat, 5 May 2007 08:49:10 +0000 (17:49 +0900)
spam/SpamTest.php
spam/spam.php

index ec3b4e7..c45d74d 100644 (file)
@@ -1,5 +1,5 @@
 <?php
-// $Id: SpamTest.php,v 1.9 2007/05/05 04:59:31 henoheno Exp $
+// $Id: SpamTest.php,v 1.10 2007/05/05 08:49:10 henoheno Exp $
 // Copyright (C) 2007 heno
 //
 // Design test case for spam.php (called from runner.php)
@@ -181,6 +181,17 @@ class SpamTest extends PHPUnit_TestCase
                $this->assertEquals($result, array_merge_leaves($array1, $array2));
        }
 
+       function testFunc_array_shrink_leaves()
+       {
+               $array  = array('key' => array('key'));
+               $result = array('key');
+               $this->assertEquals($result, array_shrink_leaves($array));
+
+               $array  = array('key' => array('key' => array('key' => array('key'))));
+               $result = array('key');
+               $this->assertEquals($result, array_shrink_leaves($array));
+       }
+
        function testFunc_uri_pickup()
        {
                // 1st argument: Null
index 5493740..7e3d963 100644 (file)
@@ -1,5 +1,5 @@
 <?php
-// $Id: spam.php,v 1.153 2007/05/05 08:02:39 henoheno Exp $
+// $Id: spam.php,v 1.154 2007/05/05 08:49:10 henoheno Exp $
 // Copyright (C) 2006-2007 PukiWiki Developers Team
 // License: GPL v2 or (at your option) any later version
 //
@@ -37,6 +37,26 @@ function preg_grep_invert($pattern = '//', $input = array())
        }
 }
 
+// ----
+
+// Very roughly, shrink the lines of var_export()
+// NOTE: If the same data exists, it must be corrupted.
+function var_export_shrink($expression, $return = FALSE)
+{
+       $result =preg_replace(
+               // Remove a newline and spaces
+               '# => \n *array \(#', ' => array (',
+               var_export($expression, TRUE)
+       );
+
+       if ($return) {
+               return $result;
+       } else {
+               echo   $result;
+               return NULL;
+       }
+}
+
 // Remove redundant values from array()
 function array_unique_recursive($array = array())
 {
@@ -115,6 +135,16 @@ function strings($binary = '', $min_len = 4, $ignore_space = FALSE)
        return $binary;
 }
 
+// Reverse $string with specified delimiter
+function delimiter_reverse($string = 'foo.bar.example.com', $from_delim = '.', $to_delim = '.')
+{
+       if (! is_string($string) || ! is_string($from_delim) || ! is_string($to_delim))
+               return $string;
+
+       // com.example.bar.foo
+       return implode($to_delim, array_reverse(explode($from_delim, $string)));
+}
+
 
 // ---------------------
 // URI pickup
@@ -1356,6 +1386,30 @@ function array_merge_leaves(& $array1, & $array2, $unique_values = TRUE, $renumb
        return $array;
 }
 
+// Shrink array('key' => array('key')) to array('key')
+function array_shrink_leaves(& $array)
+{
+       if (! is_array($array)) return $array;
+
+       foreach($array as $key => $value){
+               // Recurse. Removing more leaves beforehand
+               if (is_array($value)) array_shrink_leaves($array[$key]);
+       }
+
+       $tmp = array();
+       foreach($array as $key => $value){
+               if (is_array($value)) {
+                       $count = count($value);
+                       if ($count == 1 && current($value) == $key) {
+                               unset($array[$key]);
+                               $array[] = $key;
+                       }
+               }
+       }
+
+       return $array;
+}
+
 
 // ---------------------
 // Reporting
@@ -1381,39 +1435,11 @@ function summarize_spam_progress($progress = array(), $blockedonly = FALSE)
        return implode(', ', $tmp);
 }
 
-// Very roughly, shrink the lines of var_export()
-// NOTE: If the same data exists, it must be corrupted.
-function var_export_shrink($expression, $return = FALSE)
-{
-       $result =preg_replace(
-               // Remove a newline and spaces
-               '# => \n *array \(#', ' => array (',
-               var_export($expression, TRUE)
-       );
-
-       if ($return) {
-               return $result;
-       } else {
-               echo   $result;
-               return NULL;
-       }
-}
-
 function summarize_detail_badhost($progress = array())
 {
        if (! isset($progress['is_spam']['badhost'])) return '';
 
-       return var_export_shrink($progress['blocked'], TRUE);
-}
-
-// Reverse $string with specified delimiter
-function delimiter_reverse($string = 'foo.bar.example.com', $from_delim = '.', $to_delim = '.')
-{
-       if (! is_string($string) || ! is_string($from_delim) || ! is_string($to_delim))
-               return $string;
-
-       // com.example.bar.foo
-       return implode($to_delim, array_reverse(explode($from_delim, $string)));
+       return var_export_shrink(array_shrink_leaves($progress['blocked']), TRUE);
 }
 
 function summarize_detail_newtral($progress = array())