Skip to content

Commit db0c030

Browse files
committed
Adding pluginSplit and test case. pluginSplit consolidates a number of repeated code blocks.
1 parent 28d3488 commit db0c030

File tree

2 files changed

+29
-0
lines changed

2 files changed

+29
-0
lines changed

cake/basics.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,22 @@ function h($text, $charset = null) {
212212
return htmlspecialchars($text, ENT_QUOTES, $charset);
213213
}
214214

215+
/**
216+
* Splits a dot syntax plugin name into its plugin and classname.
217+
* If $name does not have a dot, then index 0 will be null.
218+
*
219+
* Commonly used like `list($plugin, $name) = pluginSplit($name);
220+
*
221+
* @param string $name The name you want to plugin split.
222+
* @return array Array with 2 indexes. 0 => plugin name, 1 => classname
223+
*/
224+
function pluginSplit($name) {
225+
if (strpos($name, '.') !== false) {
226+
return explode('.', $name, 2);
227+
}
228+
return array(null, $name);
229+
}
230+
215231
/**
216232
* Returns an array of all the given parameters.
217233
*

cake/tests/cases/basics.test.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -764,5 +764,18 @@ function testIfe() {
764764
$this->assertEqual(ife(0, 'a', 'b'), 'b');
765765
$this->assertEqual(ife(array(), 'a', 'b'), 'b');
766766
}
767+
768+
/**
769+
* test pluginSplit
770+
*
771+
* @return void
772+
*/
773+
function testPluginSplit() {
774+
$result = pluginSplit('Something.else');
775+
$this->assertEqual($result, array('Something', 'else'));
776+
777+
$result = pluginSplit('Somethingelse');
778+
$this->assertEqual($result, array(null, 'Somethingelse'));
779+
}
767780
}
768781
?>

0 commit comments

Comments
 (0)