/** * 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); }
/** * Adds a mapping to the list of url mappings. * * \param $command_name * The name of the command to invoke when this url mapping matches. This * should be a simple string, eg. "archive". This string will be prefixed * with "command_" and called if the url matches, eg. the method * command_archive() will be called. This callback method needs to handle * the url. * * \param $url * The url pattern to match. Variable url parts should have a colon * prefix. Example: /news/:year/:month/:slug/comments. The year, month and * slug variables will be passed in an array to the method handling the * request if the url matches. * * \param $requirements * Optional parameter with regular expressions to match against the * variables in the url. Only variables matching the regular expression * will be handled by this mapping. This way you can be sure your method * will always be called with valid parameters (so you don't need to * repeat the input checking in your handler methods). Example: * array('year' => '/^\\d{4}$/', 'month' => '/^\\d{2}$/') * */ function add_urlmap($command_name, $url, $requirements = null) { /* Requirements are optional */ if (is_null($requirements)) { $requirements = array(); } /* Sanity checks */ assert('is_string($command_name) && strlen($command_name)'); assert('is_assoc_array($requirements)'); /* Split the url into smaller pieces. We split on the forward slash * character and put the parts in a list after stripping of leading and * trailing slashes. Eg. /foo/bar/baz/ is split in (foo, bar, baz). */ if (is_string($url)) { $url = str_strip_prefix($url, '/'); $url = str_strip_suffix($url, '/'); /* Special case for top level urls */ if (strlen($url) == 0) { $parts = array(); // no parts } else { $parts = split('/', $url); } } else { assert('is_array($url)'); $parts = array_trim_strings($url, '/'); } /* Parse the pieces and parameters */ $map = array(); foreach ($parts as $part) { /* Handle variables */ if (str_has_prefix($part, ':')) { $part = substr($part, 1); $requirement = array_get_default($requirements, $part, null); $map[] = array($part, $requirement); /* No variable */ } else { $map[] = $part; } } /* Add the url map to the list of registered url maps */ $urlmap = array($command_name, $map); $this->urlmaps[] = $urlmap; }
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)');