Exemple #1
0
/**
 * Recursively searches the sitemap for an item's corresponding link.
 *
 * Parameters:
 *  $item - the item name to search for
 *  $sitemap - the array to search in (defaults to the global sitemap)
 *
 * @return The item's corresponding url
 */
function find_in_sitemap($item, $sitemap = array())
{
    $sitemap = use_default($sitemap, get_sitemap());
    $link = '';
    foreach ($sitemap as $key => $value) {
        if ($item === $key) {
            if (is_array($value)) {
                $link = get_first_unnested_item($value);
            } else {
                $link = $value;
            }
            break;
        } elseif (is_array($value)) {
            $link = find_in_sitemap($item, $value);
            if (exists($link)) {
                break;
            }
        }
    }
    return $link;
}
Exemple #2
0
 function test_find_in_sitemap()
 {
     $sitemap = array('foo' => 'foo.php', 'bar' => array('baz' => 'baz.php'), 'chewie' => array('han' => 'han.php'), 'anakin' => 'vader.php', 'html' => array('div' => array('p' => array('span' => 'em'))), 'yoda' => array('obi' => array('luke' => 'luke.php')));
     $this->assertIdentical(find_in_sitemap('foo', $sitemap), 'foo.php');
     $this->assertIdentical(find_in_sitemap('bar', $sitemap), 'baz.php');
     $this->assertIdentical(find_in_sitemap('baz', $sitemap), 'baz.php');
     ##
     $this->assertIdentical(find_in_sitemap('yoda', $sitemap), 'luke.php');
     $this->assertIdentical(find_in_sitemap('obi', $sitemap), 'luke.php');
     $this->assertIdentical(find_in_sitemap('luke', $sitemap), 'luke.php');
     $this->assertIdentical(find_in_sitemap('chewie', $sitemap), 'han.php');
     $this->assertIdentical(find_in_sitemap('han', $sitemap), 'han.php');
     ##
     $this->assertIdentical(find_in_sitemap('anakin', $sitemap), 'vader.php');
     $this->assertIdentical(find_in_sitemap('html', $sitemap), 'em');
     $this->assertIdentical(find_in_sitemap('div', $sitemap), 'em');
     $this->assertIdentical(find_in_sitemap('p', $sitemap), 'em');
     $this->assertIdentical(find_in_sitemap('span', $sitemap), 'em');
 }