Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Adding features to the jform class + correcting some errors

  • Loading branch information...
commit b61a91d88df390687e57fd9b6d2dbdc6a5e3cf1d 1 parent 9d6c8b7
@chdemko authored
View
14 libraries/joomla/form/formfield.php
@@ -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::ucfirstEach($parts[count($parts) - 1], '_');
+ }
+ else
+ {
+ $this->type = JString::ucfirstEach($parts[0], '_') . JString::ucfirstEach($parts[count($parts) - 1], '_');
+ }
+ }
}
/**
View
14 libraries/joomla/form/helper.php
@@ -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::ucfirstEach($prefix, '_') . 'Form' . JString::ucfirstEach($entity, '_') . JString::ucfirstEach($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);
+ array_push($paths, $path);
}
}
// Break off the end of the complex type.
View
40 libraries/joomla/string/string.php
@@ -65,6 +65,46 @@
);
/**
+ * Split a string in camel case format
+ *
+ * "FooBarABCDef" becomes array("Foo", "Bar", "ABC", "Def");
+ * "JFooBar" becomes array("J", "Foo", "Bar");
+ * "J001FooBar002" becomes array("J004", "Foo", "Bar002");
+ * "abcDef" becomes array("abc", "Def");
+ * "abc_defGhi_Jkl" becomes array("abc_def", "Ghi_Jkl");
+ *
+ * @param string $string The source string.
+ *
+ * @return array The splitted string.
+ *
+ * @since 11.3
+ */
+ public function splitCamelCase($string)
+ {
+ return preg_split('/(?<=[^A-Z_])(?=[A-Z])|(?<=[A-Z])(?=[A-Z][^A-Z_])/x', $string);
+ }
+
+ /**
+ * ucfirst each words
+ *
+ * @param string $string The source string.
+ * @param string $delimiter The words delimiter
+ * @param string $new_delimiter The new words delimiter (null means equal to $delimiter)
+ *
+ * @return string The string with all words ucfirst.
+ *
+ * @since 11.3
+ */
+ public function ucfirstEach($string, $delimiter = ' ', $new_delimiter = null)
+ {
+ if ($new_delimiter === null)
+ {
+ $new_delimiter = $delimiter;
+ }
+ return implode($new_delimiter, array_map('ucfirst', explode($delimiter, $string)));
+ }
+
+ /**
* Increments a trailing number in a string.
*
* Used to easily create distinct labels when copying objects. The method has the following styles:
View
27 tests/suite/joomla/form/JFormFieldTest.php
@@ -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.'
+ );
}
/**
View
6 tests/suite/joomla/form/JFormTest.php
@@ -1398,6 +1398,12 @@ 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.'
+ );
}
/**
View
48 tests/suite/joomla/string/JStringTest.php
@@ -25,6 +25,26 @@ class JStringTest extends PHPUnit_Framework_TestCase
/**
* @return array
*
+ * @since 11.3
+ */
+ public function getSplitCamelCaseData()
+ {
+ return JStringTest_DataSet::$splitCamelCase;
+ }
+
+ /**
+ * @return array
+ *
+ * @since 11.3
+ */
+ public function getUcfirstEachData()
+ {
+ return JStringTest_DataSet::$ucfirstEach;
+ }
+
+ /**
+ * @return array
+ *
* @since 11.2
*/
public function getIncrementData()
@@ -255,6 +275,34 @@ 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 getUcfirstEachData
+ * @since 11.3
+ */
+ public function testUcfirstEach($string, $delimiter, $new_delimiter, $expected)
+ {
+ $this->assertThat(
+ JString::ucfirstEach($string, $delimiter, $new_delimiter),
+ $this->equalTo($expected)
+ );
+ }
+
+ /**
+ * @return void
+ *
* @dataProvider getIncrementData
* @since 11.2
*/
View
34 tests/suite/joomla/string/TestHelpers/JString-helper-dataset.php
@@ -10,6 +10,40 @@
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::ucfirstEach.
+ *
+ * Each element contains $string, $delimiter, $new_delimiter, $expect
+ *
+ * @var array
+ * @since 11.3
+ */
+ static public $ucfirstEach = array(
+ // string, expected
+ 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'),
+ );
+
+ /**
* Tests for JString::increment.
*
* Each element contains $haystack, $needle, $offset, $expect,
Please sign in to comment.
Something went wrong with that request. Please try again.