Pastakhov has uploaded a new change for review. https://gerrit.wikimedia.org/r/111199
Change subject: add some array functions ...................................................................... add some array functions Change-Id: I4781d4f0267365f12880bf121082e9cd3b984181 --- M PhpTagsFunctions.class.php M PhpTagsFunctions.init.php M PhpTagsFunctions.php M tests/phpunit/PhpTagsFunctions_Array_Test.php 4 files changed, 548 insertions(+), 64 deletions(-) git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/PhpTagsFunctions refs/changes/99/111199/1 diff --git a/PhpTagsFunctions.class.php b/PhpTagsFunctions.class.php index 01ec6de..3c308a0 100644 --- a/PhpTagsFunctions.class.php +++ b/PhpTagsFunctions.class.php @@ -1,4 +1,7 @@ <?php + +define( 'PHPTAGSFUNCTIONS_NAME', '&' ); + /** * * @@ -7,11 +10,15 @@ * @author Pavel Astakhov <pastak...@yandex.ru> * @licence GNU General Public Licence 2.0 or later */ - class PhpTagsFunctions extends PhpTags\BaseHooks { public static function getClassName() { return __CLASS__; + } + + public static function onFunctionHook( $name, $params, &$transit ) { + $transit[PHPTAGSFUNCTIONS_NAME] = $name; + return parent::onFunctionHook( $name, $params, $transit ); } protected static $functions_definition = array( @@ -19,7 +26,7 @@ 0 => array( PHPTAGS_TYPE_ARRAY, false ), 1 => array( PHPTAGS_TYPE_ARRAY, false ), 2 => array( PHPTAGS_TYPE_INT, false, CASE_LOWER ), - PHPTAGS_HOOK_INVOKE => array( 2 => 'phptags_array_change_key_case_2' ), + PHPTAGS_HOOK_INVOKE => array( 2 => 'call_php_native_function' ), PHPTAGS_HOOK_GROUP => array( PHPTAGS_GROUP_ARRAY ), ), 'array_chunk' => array( @@ -27,20 +34,20 @@ 1 => array( PHPTAGS_TYPE_ARRAY, false ), 2 => array( PHPTAGS_TYPE_INT, false ), 3 => array( PHPTAGS_TYPE_BOOL, false, false ), - PHPTAGS_HOOK_INVOKE => array( 3 => 'phptags_array_chunk_3' ), + PHPTAGS_HOOK_INVOKE => array( 3 => 'call_php_native_function' ), PHPTAGS_HOOK_GROUP => array( PHPTAGS_GROUP_ARRAY ), ), 'array_combine' => array( 0 => array( PHPTAGS_TYPE_ARRAY, false ), 1 => array( PHPTAGS_TYPE_ARRAY, false ), 2 => array( PHPTAGS_TYPE_ARRAY, false ), - PHPTAGS_HOOK_INVOKE => array( 2 => 'phptags_array_combine_2' ), + PHPTAGS_HOOK_INVOKE => array( 2 => 'call_php_native_function' ), PHPTAGS_HOOK_GROUP => array( PHPTAGS_GROUP_ARRAY ), ), 'array_count_values' => array( 0 => array( PHPTAGS_TYPE_ARRAY, null ), 1 => array( PHPTAGS_TYPE_ARRAY, false ), - PHPTAGS_HOOK_INVOKE => array( 1 => 'phptags_array_count_values_1' ), + PHPTAGS_HOOK_INVOKE => array( 1 => 'call_php_native_function' ), PHPTAGS_HOOK_GROUP => array( PHPTAGS_GROUP_ARRAY ), ), 'array_diff_assoc' => array( @@ -48,7 +55,7 @@ 1 => array( PHPTAGS_TYPE_ARRAY, false ), 2 => array( PHPTAGS_TYPE_ARRAY, false ), PHPTAGS_HOOK_VALUE_N => array( PHPTAGS_TYPE_ARRAY, false ), - PHPTAGS_HOOK_INVOKE => array( PHPTAGS_HOOK_VALUE_N => 'phptags_array_diff_assoc_n' ), + PHPTAGS_HOOK_INVOKE => array( PHPTAGS_HOOK_VALUE_N => 'call_php_native_function' ), PHPTAGS_HOOK_GROUP => array( PHPTAGS_GROUP_ARRAY ), ), 'array_diff_key' => array( @@ -56,7 +63,7 @@ 1 => array( PHPTAGS_TYPE_ARRAY, false ), 2 => array( PHPTAGS_TYPE_ARRAY, false ), PHPTAGS_HOOK_VALUE_N => array( PHPTAGS_TYPE_ARRAY, false ), - PHPTAGS_HOOK_INVOKE => array( PHPTAGS_HOOK_VALUE_N => 'phptags_array_diff_key_n' ), + PHPTAGS_HOOK_INVOKE => array( PHPTAGS_HOOK_VALUE_N => 'call_php_native_function' ), PHPTAGS_HOOK_GROUP => array( PHPTAGS_GROUP_ARRAY ), ), 'array_diff' => array( @@ -64,14 +71,14 @@ 1 => array( PHPTAGS_TYPE_ARRAY, false ), 2 => array( PHPTAGS_TYPE_ARRAY, false ), PHPTAGS_HOOK_VALUE_N => array( PHPTAGS_TYPE_ARRAY, false ), - PHPTAGS_HOOK_INVOKE => array( PHPTAGS_HOOK_VALUE_N => 'phptags_array_diff_n' ), + PHPTAGS_HOOK_INVOKE => array( PHPTAGS_HOOK_VALUE_N => 'call_php_native_function' ), PHPTAGS_HOOK_GROUP => array( PHPTAGS_GROUP_ARRAY ), ), 'array_fill_keys' => array( 0 => array( PHPTAGS_TYPE_ARRAY, null ), 1 => array( PHPTAGS_TYPE_ARRAY, false ), 2 => array( PHPTAGS_TYPE_MIXED, false ), - PHPTAGS_HOOK_INVOKE => array( 2 => 'phptags_array_fill_keys_2' ), + PHPTAGS_HOOK_INVOKE => array( 2 => 'call_php_native_function' ), PHPTAGS_HOOK_GROUP => array( PHPTAGS_GROUP_ARRAY ), ), 'array_fill' => array( @@ -79,8 +86,157 @@ 1 => array( PHPTAGS_TYPE_INT, false ), 2 => array( PHPTAGS_TYPE_INT, false ), 3 => array( PHPTAGS_TYPE_MIXED, false ), - PHPTAGS_HOOK_INVOKE => array( 3 => 'phptags_array_fill_3' ), - PHPTAGS_HOOK_GROUP => array( PHPTAGS_GROUP_ARRAY, PHPTAGS_GROUP_EXPENSIVE ), + PHPTAGS_HOOK_INVOKE => array( 3 => 'call_php_native_function' ), + PHPTAGS_HOOK_GROUP => array( PHPTAGS_GROUP_ARRAY, PHPTAGS_GROUP_UNLIMITED_MEMORY ), + ), + 'array_flip' => array( + 0 => array( PHPTAGS_TYPE_ARRAY, null ), + 1 => array( PHPTAGS_TYPE_ARRAY, false ), + PHPTAGS_HOOK_INVOKE => array( 1 => 'call_php_native_function' ), + PHPTAGS_HOOK_GROUP => array( PHPTAGS_GROUP_ARRAY ), + ), + 'array_intersect_assoc' => array( + 0 => array( PHPTAGS_TYPE_ARRAY, null ), + 1 => array( PHPTAGS_TYPE_ARRAY, false ), + 2 => array( PHPTAGS_TYPE_ARRAY, false ), + PHPTAGS_HOOK_VALUE_N => array( PHPTAGS_TYPE_ARRAY, false ), + PHPTAGS_HOOK_INVOKE => array( PHPTAGS_HOOK_VALUE_N => 'call_php_native_function' ), + PHPTAGS_HOOK_GROUP => array( PHPTAGS_GROUP_ARRAY ), + ), + 'array_intersect_key' => array( + 0 => array( PHPTAGS_TYPE_ARRAY, null ), + 1 => array( PHPTAGS_TYPE_ARRAY, false ), + 2 => array( PHPTAGS_TYPE_ARRAY, false ), + PHPTAGS_HOOK_VALUE_N => array( PHPTAGS_TYPE_ARRAY, false ), + PHPTAGS_HOOK_INVOKE => array( PHPTAGS_HOOK_VALUE_N => 'call_php_native_function' ), + PHPTAGS_HOOK_GROUP => array( PHPTAGS_GROUP_ARRAY ), + ), + 'array_intersect' => array( + 0 => array( PHPTAGS_TYPE_ARRAY, null ), + 1 => array( PHPTAGS_TYPE_ARRAY, false ), + 2 => array( PHPTAGS_TYPE_ARRAY, false ), + PHPTAGS_HOOK_VALUE_N => array( PHPTAGS_TYPE_ARRAY, false ), + PHPTAGS_HOOK_INVOKE => array( PHPTAGS_HOOK_VALUE_N => 'call_php_native_function' ), + PHPTAGS_HOOK_GROUP => array( PHPTAGS_GROUP_ARRAY ), + ), + 'array_key_exists' => array( + 0 => array( PHPTAGS_TYPE_BOOL, false ), + 1 => array( PHPTAGS_TYPE_MIXED, false ), + 2 => array( PHPTAGS_TYPE_ARRAY, false ), + PHPTAGS_HOOK_INVOKE => array( 2 => 'call_php_native_function' ), + PHPTAGS_HOOK_GROUP => array( PHPTAGS_GROUP_ARRAY ), + ), + 'array_keys' => array( + 0 => array( PHPTAGS_TYPE_ARRAY, false ), + 1 => array( PHPTAGS_TYPE_ARRAY, false ), + 2 => array( PHPTAGS_TYPE_MIXED, false, null ), + 3 => array( PHPTAGS_TYPE_BOOL, false, false ), + PHPTAGS_HOOK_INVOKE => array( 1 => 'call_php_native_function', 3 => 'call_php_native_function' ), + PHPTAGS_HOOK_GROUP => array( PHPTAGS_GROUP_ARRAY ), + ), + 'array_merge_recursive' => array( + 0 => array( PHPTAGS_TYPE_ARRAY, null ), + 1 => array( PHPTAGS_TYPE_ARRAY, false ), + PHPTAGS_HOOK_VALUE_N => array( PHPTAGS_TYPE_ARRAY, false ), + PHPTAGS_HOOK_INVOKE => array( PHPTAGS_HOOK_VALUE_N => 'call_php_native_function' ), + PHPTAGS_HOOK_GROUP => array( PHPTAGS_GROUP_ARRAY ), + ), + 'array_merge' => array( + 0 => array( PHPTAGS_TYPE_ARRAY, null ), + 1 => array( PHPTAGS_TYPE_ARRAY, false ), + PHPTAGS_HOOK_VALUE_N => array( PHPTAGS_TYPE_ARRAY, false ), + PHPTAGS_HOOK_INVOKE => array( PHPTAGS_HOOK_VALUE_N => 'call_php_native_function' ), + PHPTAGS_HOOK_GROUP => array( PHPTAGS_GROUP_ARRAY ), + ), + 'array_multisort' => array( + 0 => array( PHPTAGS_TYPE_BOOL, false ), + 1 => array( PHPTAGS_TYPE_ARRAY, true ), + 2 => array( PHPTAGS_TYPE_MIXED, 1, SORT_ASC ), + 3 => array( PHPTAGS_TYPE_MIXED, 1, SORT_REGULAR ), + PHPTAGS_HOOK_VALUE_N => array( PHPTAGS_TYPE_MIXED, 1 ), + PHPTAGS_HOOK_INVOKE => array( PHPTAGS_HOOK_VALUE_N => 'call_php_native_function' ), + PHPTAGS_HOOK_GROUP => array( PHPTAGS_GROUP_ARRAY ), + ), + 'array_pad' => array( + 0 => array( PHPTAGS_TYPE_ARRAY, null ), + 1 => array( PHPTAGS_TYPE_ARRAY, false ), + 2 => array( PHPTAGS_TYPE_INT, false ), + 3 => array( PHPTAGS_TYPE_MIXED, false ), + PHPTAGS_HOOK_INVOKE => array( 3 => 'call_php_native_function' ), + PHPTAGS_HOOK_GROUP => array( PHPTAGS_GROUP_ARRAY, PHPTAGS_GROUP_UNLIMITED_MEMORY ), + ), + 'array_pop' => array( + 0 => array( PHPTAGS_TYPE_MIXED, null ), + 1 => array( PHPTAGS_TYPE_ARRAY, true ), + PHPTAGS_HOOK_INVOKE => array( 1 => 'call_php_native_function' ), + PHPTAGS_HOOK_GROUP => array( PHPTAGS_GROUP_ARRAY ), + ), + 'array_product' => array( + 0 => array( PHPTAGS_TYPE_NUMBER, 0 ), + 1 => array( PHPTAGS_TYPE_ARRAY, false ), + PHPTAGS_HOOK_INVOKE => array( 1 => 'call_php_native_function' ), + PHPTAGS_HOOK_GROUP => array( PHPTAGS_GROUP_ARRAY ), + ), + 'array_push' => array( + 0 => array( PHPTAGS_TYPE_INT, null ), + 1 => array( PHPTAGS_TYPE_ARRAY, true ), + 2 => array( PHPTAGS_TYPE_MIXED, false ), + PHPTAGS_HOOK_VALUE_N => array( PHPTAGS_TYPE_MIXED, false ), + PHPTAGS_HOOK_INVOKE => array( PHPTAGS_HOOK_VALUE_N => 'call_php_native_function' ), + PHPTAGS_HOOK_GROUP => array( PHPTAGS_GROUP_ARRAY ), + ), + 'array_rand' => array( + 0 => array( PHPTAGS_TYPE_MIXED, null ), + 1 => array( PHPTAGS_TYPE_ARRAY, false ), + 2 => array( PHPTAGS_TYPE_INT, false, 1 ), + PHPTAGS_HOOK_INVOKE => array( 2 => 'call_php_native_function' ), + PHPTAGS_HOOK_GROUP => array( PHPTAGS_GROUP_ARRAY ), + ), + 'array_replace_recursive' => array( + 0 => array( PHPTAGS_TYPE_ARRAY, null ), + 1 => array( PHPTAGS_TYPE_ARRAY, false ), + 2 => array( PHPTAGS_TYPE_ARRAY, false ), + PHPTAGS_HOOK_VALUE_N => array( PHPTAGS_TYPE_ARRAY, false ), + PHPTAGS_HOOK_INVOKE => array( PHPTAGS_HOOK_VALUE_N => 'call_php_native_function' ), + PHPTAGS_HOOK_GROUP => array( PHPTAGS_GROUP_ARRAY ), + ), + 'array_replace' => array( + 0 => array( PHPTAGS_TYPE_ARRAY, null ), + 1 => array( PHPTAGS_TYPE_ARRAY, false ), + 2 => array( PHPTAGS_TYPE_ARRAY, false ), + PHPTAGS_HOOK_VALUE_N => array( PHPTAGS_TYPE_ARRAY, false ), + PHPTAGS_HOOK_INVOKE => array( PHPTAGS_HOOK_VALUE_N => 'call_php_native_function' ), + PHPTAGS_HOOK_GROUP => array( PHPTAGS_GROUP_ARRAY ), + ), + 'array_reverse' => array( + 0 => array( PHPTAGS_TYPE_ARRAY, null ), + 1 => array( PHPTAGS_TYPE_ARRAY, false ), + 2 => array( PHPTAGS_TYPE_BOOL, false, false ), + PHPTAGS_HOOK_INVOKE => array( 2 => 'call_php_native_function' ), + PHPTAGS_HOOK_GROUP => array( PHPTAGS_GROUP_ARRAY ), + ), + 'array_search' => array( + 0 => array( PHPTAGS_TYPE_MIXED, null ), + 1 => array( PHPTAGS_TYPE_MIXED, false ), + 2 => array( PHPTAGS_TYPE_ARRAY, false ), + 3 => array( PHPTAGS_TYPE_BOOL, false, false ), + PHPTAGS_HOOK_INVOKE => array( 3 => 'call_php_native_function' ), + PHPTAGS_HOOK_GROUP => array( PHPTAGS_GROUP_ARRAY ), + ), + 'array_shift' => array( + 0 => array( PHPTAGS_TYPE_MIXED, null ), + 1 => array( PHPTAGS_TYPE_ARRAY, true ), + PHPTAGS_HOOK_INVOKE => array( 1 => 'call_php_native_function' ), + PHPTAGS_HOOK_GROUP => array( PHPTAGS_GROUP_ARRAY ), + ), + 'array_slice' => array( + 0 => array( PHPTAGS_TYPE_ARRAY, null ), + 1 => array( PHPTAGS_TYPE_ARRAY, false ), + 2 => array( PHPTAGS_TYPE_INT, false ), + 3 => array( PHPTAGS_TYPE_INT, false, null ), + 4 => array( PHPTAGS_TYPE_BOOL, false, false ), + PHPTAGS_HOOK_INVOKE => array( 4 => 'call_php_native_function' ), + PHPTAGS_HOOK_GROUP => array( PHPTAGS_GROUP_ARRAY ), ), @@ -88,19 +244,19 @@ 0 => array( PHPTAGS_TYPE_INT, 0 ), 1 => array( PHPTAGS_TYPE_MIXED, false ), 2 => array( PHPTAGS_TYPE_INT, false, COUNT_NORMAL ), - PHPTAGS_HOOK_INVOKE => array( 2 => 'phptags_count_2' ), + PHPTAGS_HOOK_INVOKE => array( 2 => 'call_php_native_function' ), PHPTAGS_HOOK_GROUP => array( PHPTAGS_GROUP_ARRAY ), ), 'current' => array( 0 => array( PHPTAGS_TYPE_MIXED, false ), 1 => array( PHPTAGS_TYPE_ARRAY, true ), - PHPTAGS_HOOK_INVOKE => array( 1 => 'phptags_current_1' ), + PHPTAGS_HOOK_INVOKE => array( 1 => 'call_php_native_function' ), PHPTAGS_HOOK_GROUP => array( PHPTAGS_GROUP_ARRAY ), ), 'next' => array( 0 => array( PHPTAGS_TYPE_MIXED, false ), 1 => array( PHPTAGS_TYPE_ARRAY, true ), - PHPTAGS_HOOK_INVOKE => array( 1 => 'phptags_next_1' ), + PHPTAGS_HOOK_INVOKE => array( 1 => 'call_php_native_function' ), PHPTAGS_HOOK_GROUP => array( PHPTAGS_GROUP_ARRAY ), ), @@ -112,6 +268,13 @@ PHPTAGS_HOOK_INVOKE => array( 2 => 'phptags_print_r_2' ), PHPTAGS_HOOK_GROUP => array( PHPTAGS_GROUP_VARIABLE ), ), + 'var_dump' => array( + 0 => array( PHPTAGS_TYPE_VOID, null ), + 1 => array( PHPTAGS_TYPE_MIXED, false ), + PHPTAGS_HOOK_VALUE_N => array( PHPTAGS_TYPE_MIXED, false ), + PHPTAGS_HOOK_INVOKE => array( 2 => 'phptags_var_dump_n' ), + PHPTAGS_HOOK_GROUP => array( PHPTAGS_GROUP_VARIABLE ), + ), 'var_export' => array( 0 => array( PHPTAGS_TYPE_MIXED, null ), 1 => array( PHPTAGS_TYPE_MIXED, false ), @@ -121,54 +284,9 @@ ), ); - static function phptags_array_change_key_case_2( $args ) { - return array_change_key_case( $args[0], $args[1] ); - } - - static function phptags_array_chunk_3( $args ) { - return array_chunk( $args[0], $args[1], $args[2] ); - } - - static function phptags_array_combine_2( $args ) { - return array_combine( $args[0], $args[1] ); - } - - static function phptags_array_count_values_1( $args ) { - return @ array_count_values( $args[0] ); - } - - static function phptags_array_diff_assoc_n( $args ) { - return call_user_func_array('array_diff_assoc', $args); - } - - static function phptags_array_diff_key_n( $args ) { - return call_user_func_array('array_diff_key', $args); - } - - static function phptags_array_diff_n( $args ) { - return call_user_func_array('array_diff', $args); - } - - static function phptags_array_fill_keys_2( $args ) { - return array_fill_keys( $args[0], $args[1] ); - } - - static function phptags_array_fill_3( $args ) { - return array_fill( $args[0], $args[1], $args[2] ); - } - - - - static function phptags_count_2( $args ) { - return count( $args[0], $args[1] ); - } - - static function phptags_current_1( $args ) { - return current( $args[0] ); - } - - static function phptags_next_1( $args ) { - return next( $args[0] ); + protected static function call_php_native_function( $args, $transit ) { + $functionName = $transit[PHPTAGSFUNCTIONS_NAME]; + return call_user_func_array( $functionName, $args ); } static function phptags_print_r_2( $args ) { @@ -176,6 +294,12 @@ return $args[1] ? $ret : new PhpTags\outPrint( true, $ret ); } + static function phptags_var_dump_n( $args ) { + ob_start(); + call_user_func_array('var_dump', $args); + return new PhpTags\outPrint( null, ob_get_clean() ); + } + static function phptags_var_export_2( $args ) { $ret = var_export($args[0], true); return $args[1] ? $ret : new PhpTags\outPrint( null, $ret ); diff --git a/PhpTagsFunctions.init.php b/PhpTagsFunctions.init.php index 5267b90..9c2e258 100644 --- a/PhpTagsFunctions.init.php +++ b/PhpTagsFunctions.init.php @@ -183,12 +183,38 @@ 'array_diff', 'array_fill_keys', 'array_fill', + // 'array_filter', @todo callback + 'array_flip', + 'array_intersect_assoc', + 'array_intersect_key', + // 'array_intersect_uassoc', @todo callback + // 'array_intersect_ukey', @todo callback + 'array_intersect', + 'array_key_exists', + 'array_keys', + // 'array_map', @todo callback + 'array_merge_recursive', + 'array_merge', + 'array_multisort', + 'array_pad', + 'array_pop', + 'array_product', + 'array_push', + 'array_rand', + // 'array_reduce' @todo callback + 'array_replace_recursive', + 'array_replace', + 'array_reverse', + 'array_search', + 'array_shift', + 'array_slice', 'count', 'current', 'next', 'print_r', + 'var_dump', 'var_export', ); } diff --git a/PhpTagsFunctions.php b/PhpTagsFunctions.php index 7fa491b..b5e2d5f 100644 --- a/PhpTagsFunctions.php +++ b/PhpTagsFunctions.php @@ -19,14 +19,14 @@ die( 'ERROR: The <a href="https://www.mediawiki.org/wiki/Extension:PhpTags">extension PhpTags</a> must be installed for the extension PhpTags Functions to run!' ); } -if ( version_compare( PHPTAGS_VERSION, '1.0.2', '<' ) ) { +if ( version_compare( PHPTAGS_VERSION, '1.0.3', '<' ) ) { die( '<b>Error:</b> This version of extension PhpTags Functions needs <a href="https://www.mediawiki.org/wiki/Extension:PhpTags">PhpTags</a> 1.0.2 or later. You are currently using version ' . PHPTAGS_VERSION . '.<br />' ); } -define( 'PHPTAGS_FUNCTIONS_VERSION' , '1.0.1' ); +define( 'PHPTAGS_FUNCTIONS_VERSION' , '1.0.2' ); // Register this extension on Special:Version $wgExtensionCredits['phptags'][] = array( diff --git a/tests/phpunit/PhpTagsFunctions_Array_Test.php b/tests/phpunit/PhpTagsFunctions_Array_Test.php index 9ce9e91..74348df 100644 --- a/tests/phpunit/PhpTagsFunctions_Array_Test.php +++ b/tests/phpunit/PhpTagsFunctions_Array_Test.php @@ -120,6 +120,340 @@ ); } + public function testRun_array_flip_1() { + $return = Runtime::runSource(' +$trans = array("a" => 1, "b" => 1, "c" => 2); +$trans = array_flip($trans); +print_r($trans);'); + $this->assertEquals( + (string) new outPrint( null, print_r(array(1=>'b', 2=>'c'), true) ), + (string) $return[0] + ); + } + + public function testRun_array_intersect_assoc_1() { + $return = Runtime::runSource(' +$array1 = array("a" => "green", "b" => "brown", "c" => "blue", "red"); +$array2 = array("a" => "green", "b" => "yellow", "blue", "red"); +$result_array = array_intersect_assoc($array1, $array2); +print_r($result_array);'); + $this->assertEquals( + (string) new outPrint( null, print_r(array('a'=>'green'), true) ), + (string) $return[0] + ); + } + + public function testRun_array_intersect_key_1() { + $return = Runtime::runSource(' +$array1 = array("blue" => 1, "red" => 2, "green" => 3, "purple" => 4); +$array2 = array("green" => 5, "blue" => 6, "yellow" => 7, "cyan" => 8); +print_r(array_intersect_key($array1, $array2));'); + $this->assertEquals( + (string) new outPrint( null, print_r(array('blue'=>1,'green'=>3), true) ), + (string) $return[0] + ); + } + + public function testRun_array_intersect_1() { + $return = Runtime::runSource(' +$array1 = array("a" => "green", "red", "blue"); +$array2 = array("b" => "green", "yellow", "red"); +$result = array_intersect($array1, $array2); +print_r($result);'); + $this->assertEquals( + (string) new outPrint( null, print_r(array('a'=>'green', 0=>'red'), true) ), + (string) $return[0] + ); + } + + public function testRun_array_key_exists_1() { + $return = Runtime::runSource(' +$search_array = array("first" => 1, "second" => 4); +if (array_key_exists("first", $search_array)) { + echo "The \"first\" element is in the array"; +}'); + $this->assertEquals( + $return[0], + 'The "first" element is in the array' + ); + } + + public function testRun_array_keys_1() { + $return = Runtime::runSource(' +$array = array(0 => 100, "color" => "red"); +print_r(array_keys($array));'); + $this->assertEquals( + (string) new outPrint( null, print_r(array(0, 'color'), true) ), + (string) $return[0] + ); + } + public function testRun_array_keys_2() { + $return = Runtime::runSource(' +$array = array("blue", "red", "green", "blue", "blue"); +print_r(array_keys($array, "blue"));'); + $this->assertEquals( + (string) new outPrint( null, print_r(array(0, 3, 4), true) ), + (string) $return[0] + ); + } + public function testRun_array_keys_3() { + $return = Runtime::runSource(' +$array = array("color" => array("blue", "red", "green"), + "size" => array("small", "medium", "large")); +print_r(array_keys($array));'); + $this->assertEquals( + (string) new outPrint( null, print_r(array('color', 'size'), true) ), + (string) $return[0] + ); + } + + public function testRun_array_merge_recursive_1() { + $return = Runtime::runSource(' +$ar1 = array("color" => array("favorite" => "red"), 5); +$ar2 = array(10, "color" => array("favorite" => "green", "blue")); +$result = array_merge_recursive($ar1, $ar2); +print_r($result);'); + $this->assertEquals( + (string) new outPrint( null, print_r(array('color'=>array('favorite'=>array('red','green'), 0=>'blue'),0=>5,1=>10), true) ), + (string) $return[0] + ); + } + + public function testRun_array_merge_1() { + $return = Runtime::runSource(' +$beginning = "foo"; +$end = array(1 => "bar"); +$result = array_merge((array)$beginning, (array)$end); +print_r($result);'); + $this->assertEquals( + (string) new outPrint( null, print_r(array('foo', 'bar'), true) ), + (string) $return[0] + ); + } + public function testRun_array_merge_2() { + $return = Runtime::runSource(' +$array1 = array("color" => "red", 2, 4); +$array2 = array("a", "b", "color" => "green", "shape" => "trapezoid", 4); +$result = array_merge($array1, $array2); +print_r($result);'); + $this->assertEquals( + (string) new outPrint( null, print_r(array('color'=>'green', 0=>2, 1=>4, 2=>'a', 3=>'b', 'shape'=>'trapezoid', 4=>4,), true) ), + (string) $return[0] + ); + } + public function testRun_array_merge_3() { + $return = Runtime::runSource(' +$array1 = array(); +$array2 = array(1 => "data"); +$result = array_merge($array1, $array2); +print_r($result);'); + $this->assertEquals( + (string) new outPrint( null, print_r(array('data'), true) ), + (string) $return[0] + ); + } + public function testRun_array_merge_4() { + $return = Runtime::runSource(' +$array1 = array(0 => "zero_a", 2 => "two_a", 3 => "three_a"); +$array2 = array(1 => "one_b", 3 => "three_b", 4 => "four_b"); +$result = $array1 + $array2; +print_r($result);'); + $this->assertEquals( + (string) new outPrint( null, print_r(array(0=>'zero_a', 2=>'two_a', 3=>'three_a', 1=>'one_b', 4=>'four_b'), true) ), + (string) $return[0] + ); + } + + public function testRun_array_multisort_1() { + $return = Runtime::runSource(' +$ar1 = array(10, 100, 100, 0); +$ar2 = array(1, 3, 2, 4); +array_multisort($ar1, $ar2); + +print_r($ar1); +print_r($ar2);'); + $this->assertEquals( + (string) new outPrint( null, print_r(array(0,10,100,100), true) ), + (string) $return[0] + ); + $this->assertEquals( + (string) new outPrint( null, print_r(array(4,1,2,3), true) ), + (string) $return[1] + ); + } + public function testRun_array_multisort_2() { + $return = Runtime::runSource(' +$ar = array( + array("10", 11, 100, 100, "a"), + array( 1, 2, "2", 3, 1) + ); +array_multisort($ar[0], SORT_ASC, SORT_STRING, + $ar[1], SORT_NUMERIC, SORT_DESC); +print_r($ar);'); + $this->assertEquals( + (string) new outPrint( null, print_r(array( array('10',100,100,11,'a'), array(1,3,'2',2,1) ), true) ), + (string) $return[0] + ); + } + + public function testRun_array_pad_1() { + $return = Runtime::runSource(' +$input = array(12, 10, 9); +print_r( array_pad($input, 5, 0) );'); + $this->assertEquals( + (string) new outPrint( null, print_r(array(12, 10, 9, 0, 0), true) ), + (string) $return[0] + ); + } + public function testRun_array_pad_2() { + $return = Runtime::runSource('print_r( array_pad($input, -7, -1) );'); + $this->assertEquals( + (string) new outPrint( null, print_r(array(-1, -1, -1, -1, 12, 10, 9), true) ), + (string) $return[0] + ); + } + public function testRun_array_pad_3() { + $return = Runtime::runSource('print_r( array_pad($input, 2, "noop") );'); + $this->assertEquals( + (string) new outPrint( null, print_r(array(12, 10, 9), true) ), + (string) $return[0] + ); + } + + public function testRun_array_pop_1() { + $return = Runtime::runSource(' +$stack = array("orange", "banana", "apple", "raspberry"); +$fruit = array_pop($stack); +print_r($stack);'); + $this->assertEquals( + (string) new outPrint( null, print_r(array('orange', 'banana', 'apple'), true) ), + (string) $return[0] + ); + } + + public function testRun_array_product_1() { + $return = Runtime::runSource(' +$a = array(2, 4, 6, 8); +echo "product(a) = " . array_product($a); +echo "product(array()) = " . array_product(array());'); + $this->assertEquals( + $return, + array('product(a) = 384', 'product(array()) = 1') + ); + } + + public function testRun_array_push_1() { + $return = Runtime::runSource(' +$stack = array("orange", "banana"); +array_push($stack, "apple", "raspberry"); +print_r($stack);'); + $this->assertEquals( + (string) new outPrint( null, print_r(array('orange', 'banana', 'apple', 'raspberry'), true) ), + (string) $return[0] + ); + } + + public function testRun_array_rand_1() { + $return = Runtime::runSource(' +$input = array("Neo", "Morpheus", "Trinity", "Cypher", "Tank"); +$rand_keys = array_rand($input, 2); +echo $input[$rand_keys[0]] . "\n"; +echo $input[$rand_keys[1]] . "\n";'); + $this->assertCount(2 ,$return); + } + + public function testRun_array_replace_recursive_1() { + $return = Runtime::runSource(' +$base = array("citrus" => array( "orange") , "berries" => array("blackberry", "raspberry"), ); +$replacements = array("citrus" => array("pineapple"), "berries" => array("blueberry")); + +$basket = array_replace_recursive($base, $replacements); +print_r($basket);'); + $this->assertEquals( + (string) new outPrint( null, print_r(array('citrus'=>array('pineapple'),'berries'=>array('blueberry','raspberry')), true) ), + (string) $return[0] + ); + } + public function testRun_array_replace_recursive_2() { + $return = Runtime::runSource(' +$base = array("citrus" => array("orange") , "berries" => array("blackberry", "raspberry"), "others" => "banana" ); +$replacements = array("citrus" => "pineapple", "berries" => array("blueberry"), "others" => array("litchis")); +$replacements2 = array("citrus" => array("pineapple"), "berries" => array("blueberry"), "others" => "litchis"); +$basket = array_replace_recursive($base, $replacements, $replacements2); +print_r($basket);'); + $this->assertEquals( + (string) new outPrint( null, print_r(array('citrus'=>array('pineapple'),'berries'=>array('blueberry','raspberry'),'others'=>'litchis'), true) ), + (string) $return[0] + ); + } + + public function testRun_array_replace_1() { + $return = Runtime::runSource(' +$base = array("orange", "banana", "apple", "raspberry"); +$replacements = array(0 => "pineapple", 4 => "cherry"); +$replacements2 = array(0 => "grape"); +$basket = array_replace($base, $replacements, $replacements2); +print_r($basket);'); + $this->assertEquals( + (string) new outPrint( null, print_r(array('grape','banana','apple','raspberry','cherry'), true) ), + (string) $return[0] + ); + } + + public function testRun_array_reverse_1() { + $return = Runtime::runSource(' +$input = array("php", 4.0, array("green", "red")); +$reversed = array_reverse($input); +print_r($reversed);'); + $this->assertEquals( + (string) new outPrint( null, print_r(array(array('green','red'),'4','php'), true) ), + (string) $return[0] + ); + } + public function testRun_array_reverse_2() { + $return = Runtime::runSource(' +$input = array("php", 4.0, array("green", "red")); +$preserved = array_reverse($input, true); +print_r($preserved);'); + $this->assertEquals( + (string) new outPrint( null, print_r(array(2=>array('green','red'),1=>'4',0=>'php'), true) ), + (string) $return[0] + ); + } + + public function testRun_array_search_1() { + $return = Runtime::runSource(' +$array = array(0 => "blue", 1 => "red", 2 => "green", 3 => "red"); +echo array_search("green", $array); // $key = 2; +echo array_search("red", $array); // $key = 1;'); + $this->assertEquals( + $return, + array( 2, 1 ) + ); + } + + public function testRun_array_shift_1() { + $return = Runtime::runSource(' +$stack = array("orange", "banana", "apple", "raspberry"); +$fruit = array_shift($stack); +print_r($stack);'); + $this->assertEquals( + (string) new outPrint( null, print_r(array("banana", "apple", "raspberry"), true) ), + (string) $return[0] + ); + } + + public function testRun_array_slice_1() { + $return = Runtime::runSource(' +$input = array("a", "b", "c", "d", "e"); +print_r( array_slice($input, 2) );'); + $this->assertEquals( + (string) new outPrint( null, print_r(array("c", "d", "e"), true) ), + (string) $return[0] + ); + } + + public function testRun_count_1() { $this->assertEquals( Runtime::runSource('$transport = array("foot", "bike", "car", "plane"); echo count($transport);'), -- To view, visit https://gerrit.wikimedia.org/r/111199 To unsubscribe, visit https://gerrit.wikimedia.org/r/settings Gerrit-MessageType: newchange Gerrit-Change-Id: I4781d4f0267365f12880bf121082e9cd3b984181 Gerrit-PatchSet: 1 Gerrit-Project: mediawiki/extensions/PhpTagsFunctions Gerrit-Branch: master Gerrit-Owner: Pastakhov <pastak...@yandex.ru> _______________________________________________ MediaWiki-commits mailing list MediaWiki-commits@lists.wikimedia.org https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits