From db0c030557218d8693750f7499c2647a8aa02ad5 Mon Sep 17 00:00:00 2001 From: mark_story Date: Sun, 15 Nov 2009 18:16:12 -0500 Subject: [PATCH] Adding pluginSplit and test case. pluginSplit consolidates a number of repeated code blocks. --- cake/basics.php | 16 ++++++++++++++++ cake/tests/cases/basics.test.php | 13 +++++++++++++ 2 files changed, 29 insertions(+) diff --git a/cake/basics.php b/cake/basics.php index bf4df7afa..b18e415a7 100644 --- a/cake/basics.php +++ b/cake/basics.php @@ -212,6 +212,22 @@ if (!function_exists('array_combine')) { 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. * diff --git a/cake/tests/cases/basics.test.php b/cake/tests/cases/basics.test.php index eb87b7b00..01860d2dc 100644 --- a/cake/tests/cases/basics.test.php +++ b/cake/tests/cases/basics.test.php @@ -764,5 +764,18 @@ class BasicsTest extends CakeTestCase { $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')); + } } ?> \ No newline at end of file