Permalink
Browse files

ENHANCEMENT Added GridFieldConfig->getComponentsByType() and getCompo…

…nentByType()
  • Loading branch information...
chillu committed Jan 30, 2012
1 parent 2d898ca commit 5fe86be3115c627a21fd64aeeaa483ffc322f236
Showing with 104 additions and 3 deletions.
  1. +28 −3 forms/gridfield/GridFieldConfig.php
  2. +76 −0 tests/forms/gridfield/GridFieldConfigTest.php
@@ -23,7 +23,7 @@ public static function create(){
*
*/
public function __construct() {
- ;
+ $this->components = new ArrayList();
}
public function addComponent(GridFieldComponent $component) {
@@ -32,15 +32,40 @@ public function addComponent(GridFieldComponent $component) {
}
/**
- *
- * @return ArrayList
+ * @return ArrayList Of GridFieldComponent
*/
public function getComponents() {
if(!$this->components) {
$this->components = new ArrayList();
}
return $this->components;
}
+
+ /**
+ * Returns all components extending a certain class, or implementing a certain interface.
+ *
+ * @param String Class name or interface
+ * @return ArrayList Of GridFieldComponent
+ */
+ public function getComponentsByType($type) {
+ $components = new ArrayList();
+ foreach($this->components as $component) {
+ if($component instanceof $type) $components->push($component);
+ }
+ return $components;
+ }
+
+ /**
+ * Returns the first available component with the given class or interface.
+ *
+ * @param String ClassName
+ * @return GridFieldComponent
+ */
+ public function getComponentByType($type) {
+ foreach($this->components as $component) {
+ if($component instanceof $type) return $component;
+ }
+ }
}
class GridFieldConfig_Base extends GridFieldConfig {
@@ -0,0 +1,76 @@
+<?php
+/**
+ * @package sapphire
+ * @subpackage tests
+ */
+
+class GridFieldConfigTest extends SapphireTest {
+
+ function testGetComponents() {
+ $config = GridFieldConfig::create();
+ $this->assertType('ArrayList', $config->getComponents());
+ $this->assertEquals($config->getComponents()->Count(), 0);
+
+ $config
+ ->addComponent($c1 = new GridFieldConfigTest_MyComponent())
+ ->addComponent($c2 = new GridFieldConfigTest_MyOtherComponent())
+ ->addComponent($c3 = new GridFieldConfigTest_MyOtherComponent());
+
+ $this->assertEquals(
+ new ArrayList(array($c1, $c2, $c3)),
+ $config->getComponents()
+ );
+ }
+
+ function testGetComponentsByType() {
+ $config = GridFieldConfig::create()
+ ->addComponent($c1 = new GridFieldConfigTest_MyComponent())
+ ->addComponent($c2 = new GridFieldConfigTest_MyOtherComponent())
+ ->addComponent($c3 = new GridFieldConfigTest_MyOtherComponent());
+
+ $this->assertEquals(
+ new ArrayList(array($c1)),
+ $config->getComponentsByType('GridFieldConfigTest_MyComponent')
+ );
+ $this->assertEquals(
+ new ArrayList(array($c2, $c3)),
+ $config->getComponentsByType('GridFieldConfigTest_MyOtherComponent')
+ );
+ $this->assertEquals(
+ new ArrayList(array($c1, $c2, $c3)),
+ $config->getComponentsByType('GridField_URLHandler')
+ );
+ $this->assertEquals(
+ new ArrayList(),
+ $config->getComponentsByType('GridFieldConfigTest_UnknownComponent')
+ );
+ }
+
+ function testGetComponentByType() {
+ $config = GridFieldConfig::create()
+ ->addComponent($c1 = new GridFieldConfigTest_MyComponent())
+ ->addComponent($c2 = new GridFieldConfigTest_MyOtherComponent())
+ ->addComponent($c3 = new GridFieldConfigTest_MyOtherComponent());
+
+ $this->assertEquals(
+ $c1,
+ $config->getComponentByType('GridFieldConfigTest_MyComponent')
+ );
+ $this->assertEquals(
+ $c2,
+ $config->getComponentByType('GridFieldConfigTest_MyOtherComponent')
+ );
+ $this->assertNull(
+ $config->getComponentByType('GridFieldConfigTest_UnknownComponent')
+ );
+ }
+
+}
+
+class GridFieldConfigTest_MyComponent implements GridField_URLHandler, TestOnly {
+ function getURLHandlers($gridField) {return array();}
+}
+
+class GridFieldConfigTest_MyOtherComponent implements GridField_URLHandler, TestOnly {
+ function getURLHandlers($gridField) {return array();}
+}

0 comments on commit 5fe86be

Please sign in to comment.