Skip to content

Commit

Permalink
Adding pluginSplit and test case. pluginSplit consolidates a number o…
Browse files Browse the repository at this point in the history
…f repeated code blocks.
  • Loading branch information
markstory committed Nov 15, 2009
1 parent 28d3488 commit db0c030
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 0 deletions.
16 changes: 16 additions & 0 deletions cake/basics.php
Expand Up @@ -212,6 +212,22 @@ function h($text, $charset = null) {
return htmlspecialchars($text, ENT_QUOTES, $charset);
}

/**
* Splits a dot syntax plugin name into its plugin and classname.
* If $name does not have a dot, then index 0 will be null.
*
* Commonly used like `list($plugin, $name) = pluginSplit($name);
*
* @param string $name The name you want to plugin split.
* @return array Array with 2 indexes. 0 => plugin name, 1 => classname
*/
function pluginSplit($name) {
if (strpos($name, '.') !== false) {
return explode('.', $name, 2);
}
return array(null, $name);
}

/**
* Returns an array of all the given parameters.
*
Expand Down
13 changes: 13 additions & 0 deletions cake/tests/cases/basics.test.php
Expand Up @@ -764,5 +764,18 @@ function testIfe() {
$this->assertEqual(ife(0, 'a', 'b'), 'b');
$this->assertEqual(ife(array(), 'a', 'b'), 'b');
}

/**
* test pluginSplit
*
* @return void
*/
function testPluginSplit() {
$result = pluginSplit('Something.else');
$this->assertEqual($result, array('Something', 'else'));

$result = pluginSplit('Somethingelse');
$this->assertEqual($result, array(null, 'Somethingelse'));
}
}
?>

0 comments on commit db0c030

Please sign in to comment.