anewt-developers team mailing list archive
-
anewt-developers team
-
Mailing list archive
-
Message #00221
[Branch ~uws/anewt/anewt.uws] Rev 1759: [core] Cleanup array utility functions
------------------------------------------------------------
revno: 1759
committer: Wouter Bolsterlee <uws@xxxxxxxxx>
branch nick: anewt.uws
timestamp: Tue 2010-02-16 21:28:22 +0100
message:
[core] Cleanup array utility functions
Various code cleanups in the array utility functions to
match current coding style practice. Also updated the test
file to a proper unit test file.
Additionally, numeric_array_to_associative_array() has been
removed. It's useless to have this as a global function. If
you really need something like this it's really trivial and
much easier to understand if you write something like this
instead:
$out = array();
while (count($arr) > 2)
$out[array_shift($arr)] = array_shift[$arr];
modified:
core/array.lib.php
core/array.test.php
--
lp:anewt
https://code.launchpad.net/~uws/anewt/anewt.uws
Your team Anewt developers is subscribed to branch lp:anewt.
To unsubscribe from this branch go to https://code.launchpad.net/~uws/anewt/anewt.uws/+edit-subscription.
=== modified file 'core/array.lib.php'
--- core/array.lib.php 2009-04-12 23:16:19 +0000
+++ core/array.lib.php 2010-02-16 20:28:22 +0000
@@ -35,20 +35,23 @@
if (!is_array($arr))
return false;
- if (count($arr) == 0)
+ if (!$arr)
return true;
- if ($check_all)
- {
- foreach (array_keys($arr) as $key)
- if (!is_int($key))
- return false;
- } else
- {
+ if (!$check_all)
+ {
+ /* Simple checking */
reset($arr);
return is_int(key($arr));
}
+ /* Extensive checking */
+ foreach (array_keys($arr) as $key)
+ {
+ if (!is_int($key))
+ return false;
+ }
+
return true;
}
@@ -73,7 +76,7 @@
if (!is_array($arr))
return false;
- if (count($arr) == 0)
+ if (!$arr)
return true;
return !is_numeric_array($arr, $check_all);
@@ -208,6 +211,7 @@
*/
function array_get_bool($arr, $key, $default=null)
{
+ assert('is_null($default) || is_bool($default)');
$value = array_get_default($arr, $key, $default);
if (is_bool($value))
@@ -219,7 +223,6 @@
if (preg_match('/^(0|false|no|off)$/', $value))
return false;
- assert('is_null($default) || is_bool($default)');
return $default;
}
@@ -294,7 +297,7 @@
*
* \param $align_left
* An optional parameter specifying if the key names should be left-aligned or
- * right-aligned. The default is to use left-aligned key names..
+ * right-aligned. The default is to use left-aligned key names.
*
* \return
* Returns a multiline formatted string.
@@ -459,9 +462,9 @@
* The array containing the data. Example: (1, "foo", true)
*
* \param $typespec
- * A string with letters denoting variable types. These letters are supported:
- * a: array, b: boolean, f: float, i: integer, o: object and s: string.
- * Example: "isb".
+ * A string with characters denoting variable types. These characters are
+ * supported: a: array, b: boolean, f: float, i: integer, o: object and s:
+ * string. Example: "isb".
*
* \param $cast
* If this parameter is true, strings that look like integers are casted to
@@ -649,47 +652,12 @@
*
* \see array_trim_strings
*/
-function _array_trim_strings_cb(&$value, $key=null, $charlist=null)
+function _array_trim_strings_cb(&$value, $key=null, $charlist)
{
assert('is_string($value)');
-
- if ($charlist != null)
- $value = trim($value, $charlist);
- else
- $value = trim($value);
-}
-
-
-/**
- * Transforms a numeric array into an associative array pair-wise. The numeric
- * array is assumed to be a (key, value, key, value, ...) list. All keys must be
- * strings or integers, values can be anything. Make sure you provide an even
- * number of values in the input; if not, the last value will be silently
- * ignored.
- *
- * \param $arr
- * Multiple values or a single array to be converted
- *
- * \return
- * An associative array
- */
-function numeric_array_to_associative_array($arr)
-{
- $args = func_get_args();
- $num_args = func_num_args();
-
- if (($num_args == 1) && is_array($args[0]))
- $args = $args[0];
-
- $result = array();
- while (count($args) >= 2)
- {
- $name = array_shift($args);
- assert('is_int($name) || is_string($name)');
- $result[$name] = array_shift($args);
- }
- return $result;
-}
+ $value = trim($value, $charlist);
+}
+
/**
* Sort array keys using natural order. The passed array is modified in-place.
=== modified file 'core/array.test.php'
--- core/array.test.php 2008-11-13 14:05:27 +0000
+++ core/array.test.php 2010-02-16 20:28:22 +0000
@@ -1,188 +1,209 @@
<?php
-error_reporting(E_ALL | E_STRICT);
-require_once '../anewt.lib.php';
-
-
-/* test is_numeric_array and is_assoc_array */
-
-function dump_results($a) {
- print_r($a);
- is_numeric_array($a)
- ? printf("%s\n", 'This array is numeric')
- : printf("%s\n", 'This array is not numeric');
- is_assoc_array($a)
- ? printf("%s\n", 'This array is associative')
- : printf("%s\n", 'This array is not associative');
- print "\n\n";
+class AnewtArrayFunctionsTest extends PHPUnit_Framework_TestCase
+{
+ /**
+ * Test is_numeric_array and is_assoc_array
+ */
+ function test_is_functions()
+ {
+ $this->assertTrue(is_numeric_array(array()));
+ $this->assertTrue(is_assoc_array(array()));
+
+ $data = array('a', 'b', 'c');
+ $this->assertTrue(is_numeric_array($data));
+ $this->assertTrue(is_numeric_array($data, true));
+ $this->assertFalse(is_assoc_array($data));
+ $this->assertFalse(is_assoc_array($data, true));
+
+ $data = array(1, 2, 3);
+ $this->assertTrue(is_numeric_array($data));
+ $this->assertTrue(is_numeric_array($data, true));
+ $this->assertFalse(is_assoc_array($data));
+ $this->assertFalse(is_assoc_array($data, true));
+
+ $data = array('a' => 'aa', 'b' => 'bb', 'c' => 'cc');
+ $this->assertFalse(is_numeric_array($data));
+ $this->assertTrue(is_assoc_array($data));
+
+ $data = array('a', 'b' => 'bb', 'c' => 'cc', 'd' => 'dd');
+ $this->assertTrue(is_numeric_array($data));
+ $this->assertFalse(is_numeric_array($data, true));
+ $this->assertFalse(is_assoc_array($data));
+ $this->assertTrue(is_assoc_array($data, true));
+
+ $data = array('a' => 'aa', 'b', 'c');
+ $this->assertFalse(is_numeric_array($data));
+ $this->assertFalse(is_numeric_array($data, true));
+ $this->assertTrue(is_assoc_array($data));
+ $this->assertTrue(is_assoc_array($data, true));
+ }
+
+ /**
+ * Test array_has_key and array_has_value
+ */
+ function test_has_key_value()
+ {
+ $data = array(
+ 'first' => 'one',
+ 'second' => 'two',
+ 'third' => 'three'
+ );
+ $this->assertTrue(array_has_key($data, "first"));
+ $this->assertFalse(array_has_key($data, "foo"));
+ $this->assertFalse(array_has_key($data, "one"));
+ $this->assertTrue(array_has_value($data, "three"));
+ $this->assertFalse(array_has_value($data, "foo"));
+ $this->assertFalse(array_has_value($data, "first"));
+ }
+
+ /**
+ * Test array_unset_key
+ */
+ function test_array_unset_key()
+ {
+ $data = array(
+ 'first' => 'one',
+ 'second' => 'two',
+ 'third' => 'three'
+ );
+ array_unset_key($data, 'first');
+ $this->assertFalse(array_has_key($data, "first"));
+ $this->assertTrue(array_has_key($data, "third"));
+ array_unset_key($data, 'third');
+ $this->assertFalse(array_has_key($data, "third"));
+ array_unset_key($data, 'notpresent');
+ $this->assertFalse(array_has_key($data, "notpresent"));
+ }
+
+ /**
+ * Test array_unset_keys
+ */
+ function test_array_unset_keys()
+ {
+ $data = array(
+ 'first' => 'one',
+ 'second' => 'two',
+ 'third' => 'three'
+ );
+ $keys_to_remove = array ('first', 'second', 'notpresent');
+ array_unset_keys($data, $keys_to_remove);
+ $this->assertFalse(array_has_key($data, "first"));
+ $this->assertFalse(array_has_key($data, "second"));
+ $this->assertTrue(array_has_key($data, "third"));
+ $this->assertFalse(array_has_key($data, "notpresent"));
+ }
+
+ /**
+ * Test array_clear
+ */
+ function test_array_clear()
+ {
+ $data = array(
+ 'first' => 'one',
+ 'second' => 'two',
+ 'third' => 'three'
+ );
+ array_clear($data);
+ $this->assertEquals(0, count($data));
+ }
+
+ /**
+ * Test array_flip_string_keys
+ */
+ function test_array_flip_string_keys(){
+ $data = array(
+ 'first' => 'one',
+ 'second' => 'two',
+ 'third' => 'three',
+ 4 => 'four',
+ 5 => 'five',
+ 'six' => 6,
+ );
+ $data = array_flip_string_keys($data);
+ $this->assertTrue(array_has_key($data, "one"));
+ $this->assertTrue(array_has_key($data, "two"));
+ $this->assertFalse(array_has_key($data, "first"));
+ $this->assertTrue(array_has_key($data, 4));
+ $this->assertTrue(array_has_key($data, 6));
+ $this->assertFalse(array_has_key($data, "six"));
+ $data = array_flip_string_keys($data);
+ $this->assertTrue(array_has_key($data, 6));
+ $this->assertTrue(array_has_key($data, "first"));
+ }
+
+ /**
+ * Test array_check_types and require_args
+ */
+ function test_array_type_checking()
+ {
+ $data = array('foo', 1, '2', true, 'bar', array());
+ $this->assertFalse(array_check_types($data, "siibsa", false));
+ $this->assertTrue(array_check_types($data, "siibsa"));
+ $this->assertFalse(array_check_types($data, "a"));
+ $this->assertFalse(array_check_types($data, "abc"));
+
+ require_args($data, 'siibsa'); // should not throw an error
+ }
+
+ /**
+ * Test require_args
+ *
+ * @expectedException Exception
+ */
+ function test_require_args_error()
+ {
+ require_args($data, 'bsa');
+ }
+
+ /**
+ * Test array_trim_strings
+ */
+ function test_array_trim_strings()
+ {
+ $data = array('foo ', ' bar', '---foo---', 'bar--- ');
+
+ $expected = array('foo', 'bar', '---foo---', 'bar---');
+ $result = array_trim_strings($data);
+ $this->assertEquals($result, $expected);
+
+ $expected = array('foo ', ' bar', 'foo', 'bar--- ');
+ $result = array_trim_strings($data, '-');
+ $this->assertEquals($result, $expected);
+
+ $expected = array('foo', 'bar', 'foo', 'bar');
+ $result = array_trim_strings($data, ' -');
+ $this->assertEquals($result, $expected);
+ }
+
+ /**
+ * Test array_get_int
+ */
+ function test_array_get_int()
+ {
+ $data = array(
+ 'first' => 1,
+ 'second' => '2',
+ 'third' => 'drei',
+ 4 => 4);
+ $this->assertEquals(1, array_get_int($data, "first"));
+ $this->assertEquals(1, array_get_int($data, "first", 2));
+ $this->assertEquals(3, array_get_int($data, "notfound", 3));
+ $this->assertEquals(33, array_get_int($data, "third", 33));
+ $this->assertEquals(4, array_get_int($data, "4"));
+ $this->assertEquals(4, array_get_int($data, 4));
+ }
+
+ /**
+ * Test natksort
+ */
+ function test_natksort()
+ {
+ $data = array('a1'=>1, 'a20'=>2, 'a2'=>3);
+ $expected = array('a1'=>1, 'a2'=>3, 'a20'=>2);
+ natksort($data);
+ $this->assertEquals($data, $expected);
+ }
}
-$data = array('a', 'b', 'c');
-assert('true === is_numeric_array($data)');
-assert('true === is_numeric_array($data, true)');
-assert('false === is_assoc_array($data)');
-assert('false === is_assoc_array($data, true)');
-
-$data = array(1, 2, 3);
-assert('true === is_numeric_array($data)');
-assert('true === is_numeric_array($data, true)');
-assert('false === is_assoc_array($data)');
-assert('false === is_assoc_array($data, true)');
-
-$data = array('a' => 'aa', 'b' => 'bb', 'c' => 'cc');
-assert('false === is_numeric_array($data)');
-assert('true === is_assoc_array($data)');
-
-$data = array('a', 'b' => 'bb', 'c' => 'cc');
-$data['d'] = 'dd';
-assert('true === is_numeric_array($data)');
-assert('false === is_numeric_array($data, true)');
-assert('false === is_assoc_array($data)');
-assert('true === is_assoc_array($data, true)');
-
-$data = array('a' => 'aa', 'b', 'c');
-$data['d'] = 'dd';
-assert('false === is_numeric_array($data)');
-assert('false === is_numeric_array($data, true)');
-assert('true === is_assoc_array($data)');
-assert('true === is_assoc_array($data, true)');
-
-
-
-/* test array_has_key and array_has_value */
-
-$data = array(
- 'first' => 'one',
- 'second' => 'two',
- 'third' => 'three'
- );
-assert('true === array_has_key($data, "first")');
-assert('false === array_has_key($data, "foo")');
-assert('false === array_has_key($data, "one")');
-assert('true === array_has_value($data, "three")');
-assert('false === array_has_value($data, "foo")');
-assert('false === array_has_value($data, "first")');
-
-
-/* test array_unset_key */
-$data = array(
- 'first' => 'one',
- 'second' => 'two',
- 'third' => 'three'
- );
-array_unset_key($data, 'first');
-assert('false === array_has_key($data, "first")');
-assert('true === array_has_key($data, "third")');
-array_unset_key($data, 'third');
-assert('false === array_has_key($data, "third")');
-array_unset_key($data, 'notpresent');
-assert('false === array_has_key($data, "notpresent")');
-
-
-/* test array_unset_keys */
-
-$data = array(
- 'first' => 'one',
- 'second' => 'two',
- 'third' => 'three'
- );
-$keys_to_remove = array ('first', 'second', 'notpresent');
-array_unset_keys($data, $keys_to_remove);
-assert('false === array_has_key($data, "first")');
-assert('false === array_has_key($data, "second")');
-assert('true === array_has_key($data, "third")');
-assert('false === array_has_key($data, "notpresent")');
-
-
-/* test array_clear */
-
-$data = array(
- 'first' => 'one',
- 'second' => 'two',
- 'third' => 'three'
- );
-array_clear($data);
-assert('count($data) == 0');
-
-
-/* test array_flip_string_keys */
-$data = array(
- 'first' => 'one',
- 'second' => 'two',
- 'third' => 'three',
- 4 => 'four',
- 5 => 'five',
- 'six' => 6,
- );
-$data = array_flip_string_keys($data);
-assert('true === array_has_key($data, "one")');
-assert('true === array_has_key($data, "two")');
-assert('false === array_has_key($data, "first")');
-assert('true === array_has_key($data, 4)');
-assert('true === array_has_key($data, 6)');
-assert('false === array_has_key($data, "six")');
-$data = array_flip_string_keys($data);
-assert('true === array_has_key($data, 6)');
-assert('true === array_has_key($data, "first")');
-
-
-/* test array_check_types */
-$data = array('foo', 1, '2', true, 'bar', array());
-assert('!array_check_types($data, "siibsa", false)');
-assert('array_check_types($data, "siibsa")');
-assert('!array_check_types($data, "a")');
-assert('!array_check_types($data, "abc")');
-
-/* test require_args */
-require_args($data, 'siibsa'); // should not throw an error
-
-/* test array_trim_strings */
-
-$data = array('foo ', ' bar', '---foo---', 'bar--- ');
-
-$expected = array('foo', 'bar', '---foo---', 'bar---');
-$result = array_trim_strings($data);
-assert('$result === $expected');
-
-$expected = array('foo ', ' bar', 'foo', 'bar--- ');
-$result = array_trim_strings($data, '-');
-assert('$result === $expected');
-
-$expected = array('foo', 'bar', 'foo', 'bar');
-$result = array_trim_strings($data, ' -');
-assert('$result === $expected');
-
-/* test array_get_int */
-$data = array(
- 'first' => 1,
- 'second' => '2',
- 'third' => 'drei',
- 4 => 4);
-assert('array_get_int($data, "first") == 1');
-assert('array_get_int($data, "first", 2) == 1');
-assert('array_get_int($data, "notfound", 3) == 3');
-assert('array_get_int($data, "third", 33) == 33');
-assert('array_get_int($data, "4") == 4');
-assert('array_get_int($data, 4) == 4');
-
-/* test numeric_array_to_associative_array */
-$x = numeric_array_to_associative_array('one', 2, 3, 'data');
-$y = numeric_array_to_associative_array(array('one', 2, 3, 'data'));
-assert('count($x) == 2');
-assert('array_has_key($x, "one")');
-assert('array_has_key($x, 3)');
-assert('!array_has_key($x, 2)');
-assert('!array_has_key($x, "data")');
-assert('count($y) == 2');
-assert('array_has_key($y, "one")');
-assert('array_has_key($y, 3)');
-assert('!array_has_key($y, 2)');
-assert('!array_has_key($y, "data")');
-
-/* test natksort */
-$data = array('a1'=>1, 'a20'=>2, 'a2'=>3);
-$expected = array('a1'=>1, 'a2'=>3, 'a20'=>2);
-natksort($data);
-assert('$data === $expected');
-
?>