Permalink
Browse files

Merge pull request #496 from chdemko/jform

Adding features to the JForm library + correcting some errors
  • Loading branch information...
2 parents 30f5a23 + 431e71b commit 5288422d724dd2d64cd334534d8f6ffeef7b6bc6 @eddieajau eddieajau committed Nov 24, 2011
@@ -196,6 +196,20 @@ public function __construct($form = null)
$this->form = $form;
$this->formControl = $form->getFormControl();
}
+
+ // Detect the field type if not set
+ if (!isset($this->type))
+ {
+ $parts = JString::splitCamelCase(get_called_class());
+ if ($parts[0] == 'J')
+ {
+ $this->type = JString::ucfirst($parts[count($parts) - 1], '_');
+ }
+ else
+ {
+ $this->type = JString::ucfirst($parts[0], '_') . JString::ucfirst($parts[count($parts) - 1], '_');
+ }
+ }
}
/**
@@ -169,7 +169,17 @@ public static function loadRuleClass($type)
*/
protected static function loadClass($entity, $type)
{
- $class = 'JForm' . ucfirst($entity) . ucfirst($type);
+ if (strpos($type, '.'))
+ {
+ list($prefix, $type) = explode('.', $type);
+ }
+ else
+ {
+ $prefix = 'J';
+ }
+
+ $class = JString::ucfirst($prefix, '_') . 'Form' . JString::ucfirst($entity, '_') . JString::ucfirst($type, '_');
+
if (class_exists($class))
{
return $class;
@@ -191,7 +201,7 @@ protected static function loadClass($entity, $type)
// If the path does not exist, add it.
if (!in_array($path, $paths))
{
- array_unshift($paths, $path);
+ $paths[] = $path;
}
}
// Break off the end of the complex type.
@@ -65,6 +65,28 @@
);
/**
+ * Split a string in camel case format
+ *
+ * "FooBarABCDef" becomes array("Foo", "Bar", "ABC", "Def");
+ * "JFooBar" becomes array("J", "Foo", "Bar");
+ * "J001FooBar002" becomes array("J001", "Foo", "Bar002");
+ * "abcDef" becomes array("abc", "Def");
+ * "abc_defGhi_Jkl" becomes array("abc_def", "Ghi_Jkl");
+ * "ThisIsA_NASAAstronaut" becomes array("This", "Is", "A_NASA", "Astronaut")),
+ * "JohnFitzgerald_Kennedy" becomes array("John", "Fitzgerald_Kennedy")),
+ *
+ * @param string $string The source string.
+ *
+ * @return array The splitted string.
+ *
+ * @since 11.3
+ */
+ public static function splitCamelCase($string)
+ {
+ return preg_split('/(?<=[^A-Z_])(?=[A-Z])|(?<=[A-Z])(?=[A-Z][^A-Z_])/x', $string);
+ }
+
+ /**
* Increments a trailing number in a string.
*
* Used to easily create distinct labels when copying objects. The method has the following styles:
@@ -638,19 +660,34 @@ public static function trim($str, $charlist = false)
/**
* UTF-8 aware alternative to ucfirst
- * Make a string's first character uppercase
+ * Make a string's first character uppercase or all words' first character uppercase
*
- * @param string $str String to be processed
+ * @param string $str String to be processed
+ * @param string $delimiter The words delimiter (null means do not split the string)
+ * @param string $newDelimiter The new words delimiter (null means equal to $delimiter)
*
- * @return string String with first character as upper case (if applicable)
+ * @return string If $delimiter is null, return the string with first character as upper case (if applicable)
+ * else consider the string of words separated by the delimiter, apply the ucfirst to each words
+ * and return the string with the new delimiter
*
* @see http://www.php.net/ucfirst
* @since 11.1
*/
- public static function ucfirst($str)
+ public static function ucfirst($str, $delimiter = null, $newDelimiter = null)
{
jimport('phputf8.ucfirst');
- return utf8_ucfirst($str);
+ if ($delimiter === null)
+ {
+ return utf8_ucfirst($str);
+ }
+ else
+ {
+ if ($newDelimiter === null)
+ {
+ $newDelimiter = $delimiter;
+ }
+ return implode($newDelimiter, array_map('utf8_ucfirst', explode($delimiter, $str)));
+ }
}
/**
@@ -67,6 +67,33 @@ public function testConstruct()
$this->identicalTo($form),
'Line:'.__LINE__.' The internal form should be identical to the variable passed in the contructor.'
);
+
+ // Add custom path.
+ JForm::addFieldPath(__DIR__ . '/_testfields');
+
+ JFormHelper::loadFieldType('foo.bar');
+ $field = new FooFormFieldBar($form);
+ $this->assertEquals(
+ $field->type,
+ 'FooBar',
+ 'Line:'.__LINE__.' The field type should have been guessed by the constructor.'
+ );
+
+ JFormHelper::loadFieldType('foo');
+ $field = new JFormFieldFoo($form);
+ $this->assertEquals(
+ $field->type,
+ 'Foo',
+ 'Line:'.__LINE__.' The field type should have been guessed by the constructor.'
+ );
+
+ JFormHelper::loadFieldType('modal_foo');
+ $field = new JFormFieldModal_Foo($form);
+ $this->assertEquals(
+ $field->type,
+ 'Modal_Foo',
+ 'Line:'.__LINE__.' The field type should have been guessed by the constructor.'
+ );
}
/**
@@ -1398,6 +1398,24 @@ public function testLoadFieldType()
$this->isTrue(),
'Line:'.__LINE__.' loadFieldType should return the correct custom class.'
);
+
+ $this->assertThat(
+ (JFormInspector::loadFieldType('foo.bar') instanceof FooFormFieldBar),
+ $this->isTrue(),
+ 'Line:'.__LINE__.' loadFieldType should return the correct custom class.'
+ );
+
+ $this->assertThat(
+ (JFormInspector::loadFieldType('modal_foo') instanceof JFormFieldModal_Foo),
+ $this->isTrue(),
+ 'Line:'.__LINE__.' loadFieldType should return the correct custom class.'
+ );
+
+ $this->assertThat(
+ (JFormInspector::loadFieldType('foo.modal_bar') instanceof FooFormFieldModal_Bar),
+ $this->isTrue(),
+ 'Line:'.__LINE__.' loadFieldType should return the correct custom class.'
+ );
}
/**
@@ -0,0 +1,30 @@
+<?php
+/**
+ * @package Joomla.UnitTest
+ * @subpackage Form
+ *
+ * @copyright Copyright (C) 2005 - 2011 Open Source Matters, Inc. All rights reserved.
+ * @license GNU General Public License version 2 or later; see LICENSE
+ */
+
+jimport('joomla.form.formfield');
+
+/**
+ * Form Field class for the Joomla Framework.
+ *
+ * @package Joomla.Platform
+ * @subpackage Form
+ * @since 11.1
+ */
+class FooFormFieldBar extends JFormField
+{
+ /**
+ * Method to get the field input.
+ *
+ * @return string The field input.
+ */
+ protected function getInput()
+ {
+ return 'Bar';
+ }
+}
@@ -0,0 +1,30 @@
+<?php
+/**
+ * @package Joomla.UnitTest
+ * @subpackage Form
+ *
+ * @copyright Copyright (C) 2005 - 2011 Open Source Matters, Inc. All rights reserved.
+ * @license GNU General Public License version 2 or later; see LICENSE
+ */
+
+jimport('joomla.form.formfield');
+
+/**
+ * Form Field class for the Joomla Framework.
+ *
+ * @package Joomla.Platform
+ * @subpackage Form
+ * @since 11.1
+ */
+class JFormFieldFoo extends JFormField
+{
+ /**
+ * Method to get the field input.
+ *
+ * @return string The field input.
+ */
+ protected function getInput()
+ {
+ return 'Foo';
+ }
+}
@@ -0,0 +1,30 @@
+<?php
+/**
+ * @package Joomla.UnitTest
+ * @subpackage Form
+ *
+ * @copyright Copyright (C) 2005 - 2011 Open Source Matters, Inc. All rights reserved.
+ * @license GNU General Public License version 2 or later; see LICENSE
+ */
+
+jimport('joomla.form.formfield');
+
+/**
+ * Form Field class for the Joomla Framework.
+ *
+ * @package Joomla.Platform
+ * @subpackage Form
+ * @since 11.1
+ */
+class FooFormFieldModal_Bar extends JFormField
+{
+ /**
+ * Method to get the field input.
+ *
+ * @return string The field input.
+ */
+ protected function getInput()
+ {
+ return 'Modal';
+ }
+}
@@ -0,0 +1,30 @@
+<?php
+/**
+ * @package Joomla.UnitTest
+ * @subpackage Form
+ *
+ * @copyright Copyright (C) 2005 - 2011 Open Source Matters, Inc. All rights reserved.
+ * @license GNU General Public License version 2 or later; see LICENSE
+ */
+
+jimport('joomla.form.formfield');
+
+/**
+ * Form Field class for the Joomla Framework.
+ *
+ * @package Joomla.Platform
+ * @subpackage Form
+ * @since 11.1
+ */
+class JFormFieldModal_Foo extends JFormField
+{
+ /**
+ * Method to get the field input.
+ *
+ * @return string The field input.
+ */
+ protected function getInput()
+ {
+ return 'Modal';
+ }
+}
@@ -25,6 +25,16 @@ class JStringTest extends PHPUnit_Framework_TestCase
/**
* @return array
*
+ * @since 11.3
+ */
+ public function getSplitCamelCaseData()
+ {
+ return JStringTest_DataSet::$splitCamelCase;
+ }
+
+ /**
+ * @return array
+ *
* @since 11.2
*/
public function getIncrementData()
@@ -255,6 +265,20 @@ public function getValidData()
/**
* @return void
*
+ * @dataProvider getSplitCamelCaseData
+ * @since 11.3
+ */
+ public function testSplitCamelCase($string, $expected)
+ {
+ $this->assertThat(
+ JString::splitCamelCase($string),
+ $this->equalTo($expected)
+ );
+ }
+
+ /**
+ * @return void
+ *
* @dataProvider getIncrementData
* @since 11.2
*/
@@ -531,9 +555,9 @@ public function testTrim($string, $charlist, $expect)
* @dataProvider getUcfirstData
* @since 11.2
*/
- public function testUcfirst($string, $expect)
+ public function testUcfirst($string, $delimiter, $newDelimiter, $expect)
{
- $actual = JString::ucfirst($string);
+ $actual = JString::ucfirst($string, $delimiter, $newDelimiter);
$this->assertEquals($expect, $actual);
}
@@ -10,6 +10,25 @@
class JStringTest_DataSet
{
/**
+ * Tests for JString::splitCamelCase.
+ *
+ * Each element contains $string, $expect
+ *
+ * @var array
+ * @since 11.3
+ */
+ static public $splitCamelCase = array(
+ // string, expected
+ array('FooBarABCDef', array('Foo', 'Bar', 'ABC', 'Def')),
+ array('JFooBar', array('J', 'Foo', 'Bar')),
+ array('J001FooBar002', array('J001', 'Foo', 'Bar002')),
+ array('abcDef', array('abc', 'Def')),
+ array('abc_defGhi_Jkl', array('abc_def', 'Ghi_Jkl')),
+ array('ThisIsA_NASAAstronaut', array('This', 'Is', 'A_NASA', 'Astronaut')),
+ array('JohnFitzgerald_Kennedy', array('John', 'Fitzgerald_Kennedy')),
+ );
+
+ /**
* Tests for JString::increment.
*
* Each element contains $haystack, $needle, $offset, $expect,
@@ -265,9 +284,12 @@ class JStringTest_DataSet
* @since 11.2
*/
static public $ucfirstTests = array (
- array('george', 'George'),
- array('мога', 'Мога'),
- array('ψυχοφθόρα', 'Ψυχοφθόρα')
+ array('george', null, null, 'George'),
+ array('мога', null, null, 'Мога'),
+ array('ψυχοφθόρα', null, null, 'Ψυχοφθόρα'),
+ array('dr jekill and mister hyde', ' ', null, 'Dr Jekill And Mister Hyde'),
+ array('dr jekill and mister hyde', ' ', '_', 'Dr_Jekill_And_Mister_Hyde'),
+ array('dr jekill and mister hyde', ' ', '', 'DrJekillAndMisterHyde'),
);
/**

0 comments on commit 5288422

Please sign in to comment.