Browse Source

Add keys method to mappers (#3830)

Add keys method to DatagridMapper, ShowMapper, FormMapper and ListMapper.

This will permit to get all the configured keys.

For example, this could be used to do an advanced key ordering on BaseMapper::reorder method.
Sullivan SENECHAL 9 years ago
parent
commit
125d24ad01

+ 1 - 0
CHANGELOG.md

@@ -5,6 +5,7 @@ This project adheres to [Semantic Versioning](http://semver.org/).
 ## [3.x]
 ## [3.x]
 ### Added
 ### Added
 - Added AbstractAdmin, replacing Admin
 - Added AbstractAdmin, replacing Admin
+- Added `BaseMapper::keys` method
 
 
 ### Changed
 ### Changed
 - Updated AdminLTE theme to version 2.3.3
 - Updated AdminLTE theme to version 2.3.3

+ 8 - 0
Datagrid/DatagridMapper.php

@@ -105,6 +105,14 @@ class DatagridMapper extends BaseMapper
         return $this->datagrid->hasFilter($key);
         return $this->datagrid->hasFilter($key);
     }
     }
 
 
+    /**
+     * {@inheritdoc}
+     */
+    final public function keys()
+    {
+        return array_keys($this->datagrid->getFilters());
+    }
+
     /**
     /**
      * {@inheritdoc}
      * {@inheritdoc}
      */
      */

+ 8 - 0
Datagrid/ListMapper.php

@@ -145,6 +145,14 @@ class ListMapper extends BaseMapper
         return $this;
         return $this;
     }
     }
 
 
+    /**
+     * {@inheritdoc}
+     */
+    final public function keys()
+    {
+        return array_keys($this->list->getElements());
+    }
+
     /**
     /**
      * {@inheritdoc}
      * {@inheritdoc}
      */
      */

+ 8 - 0
Form/FormMapper.php

@@ -160,6 +160,14 @@ class FormMapper extends BaseGroupedMapper
         return $this->formBuilder->has($key);
         return $this->formBuilder->has($key);
     }
     }
 
 
+    /**
+     * {@inheritdoc}
+     */
+    final public function keys()
+    {
+        return array_keys($this->formBuilder->all());
+    }
+
     /**
     /**
      * {@inheritdoc}
      * {@inheritdoc}
      */
      */

+ 8 - 0
Mapper/BaseMapper.php

@@ -71,6 +71,14 @@ abstract class BaseMapper
      */
      */
     abstract public function remove($key);
     abstract public function remove($key);
 
 
+    // To be uncommented on 4.0.
+    /**
+     * Returns configured keys.
+     *
+     * @return string[]
+     */
+    //abstract public function keys();
+
     /**
     /**
      * @param array $keys field names
      * @param array $keys field names
      *
      *

+ 8 - 0
Show/ShowMapper.php

@@ -113,6 +113,14 @@ class ShowMapper extends BaseGroupedMapper
         return $this;
         return $this;
     }
     }
 
 
+    /**
+     * {@inheritdoc}
+     */
+    final public function keys()
+    {
+        return array_keys($this->list->getElements());
+    }
+
     /**
     /**
      * {@inheritdoc}
      * {@inheritdoc}
      */
      */

+ 11 - 0
Tests/Datagrid/DatagridMapperTest.php

@@ -226,6 +226,17 @@ class DatagridMapperTest extends \PHPUnit_Framework_TestCase
         $this->fail('Failed asserting that exception of type "\RuntimeException" is thrown.');
         $this->fail('Failed asserting that exception of type "\RuntimeException" is thrown.');
     }
     }
 
 
+    public function testKeys()
+    {
+        $fieldDescription1 = $this->getFieldDescriptionMock('fooName1', 'fooLabel1');
+        $fieldDescription2 = $this->getFieldDescriptionMock('fooName2', 'fooLabel2');
+
+        $this->datagridMapper->add($fieldDescription1, null, array('field_name' => 'fooFilterName1'));
+        $this->datagridMapper->add($fieldDescription2, null, array('field_name' => 'fooFilterName2'));
+
+        $this->assertSame(array('fooName1', 'fooName2'), $this->datagridMapper->keys());
+    }
+
     public function testReorder()
     public function testReorder()
     {
     {
         $fieldDescription1 = $this->getFieldDescriptionMock('fooName1', 'fooLabel1');
         $fieldDescription1 = $this->getFieldDescriptionMock('fooName1', 'fooLabel1');

+ 11 - 0
Tests/Datagrid/ListMapperTest.php

@@ -215,6 +215,17 @@ class ListMapperTest extends \PHPUnit_Framework_TestCase
         }
         }
     }
     }
 
 
+    public function testKeys()
+    {
+        $fieldDescription1 = $this->getFieldDescriptionMock('fooName1', 'fooLabel1');
+        $fieldDescription2 = $this->getFieldDescriptionMock('fooName2', 'fooLabel2');
+
+        $this->listMapper->add($fieldDescription1);
+        $this->listMapper->add($fieldDescription2);
+
+        $this->assertSame(array('fooName1', 'fooName2'), $this->listMapper->keys());
+    }
+
     public function testReorder()
     public function testReorder()
     {
     {
         $fieldDescription1 = $this->getFieldDescriptionMock('fooName1', 'fooLabel1');
         $fieldDescription1 = $this->getFieldDescriptionMock('fooName1', 'fooLabel1');

+ 14 - 0
Tests/Form/FormMapperTest.php

@@ -380,6 +380,20 @@ class FormMapperTest extends \PHPUnit_Framework_TestCase
         $this->assertSame(array(), $this->admin->getFormTabs());
         $this->assertSame(array(), $this->admin->getFormTabs());
     }
     }
 
 
+    public function testKeys()
+    {
+        $this->contractor->expects($this->any())
+            ->method('getDefaultOptions')
+            ->will($this->returnValue(array()));
+
+        $this->formMapper
+            ->add('foo', 'bar')
+            ->add('baz', 'foobaz')
+        ;
+
+        $this->assertSame(array('foo', 'baz'), $this->formMapper->keys());
+    }
+
     private function getFieldDescriptionMock($name = null, $label = null, $translationDomain = null)
     private function getFieldDescriptionMock($name = null, $label = null, $translationDomain = null)
     {
     {
         $fieldDescription = $this->getMockForAbstractClass('Sonata\AdminBundle\Admin\BaseFieldDescription');
         $fieldDescription = $this->getMockForAbstractClass('Sonata\AdminBundle\Admin\BaseFieldDescription');

+ 11 - 0
Tests/Show/ShowMapperTest.php

@@ -353,6 +353,17 @@ class ShowMapperTest extends \PHPUnit_Framework_TestCase
         $this->fail('Failed asserting that duplicate field name exception of type "\RuntimeException" is thrown.');
         $this->fail('Failed asserting that duplicate field name exception of type "\RuntimeException" is thrown.');
     }
     }
 
 
+    public function testKeys()
+    {
+        $fieldDescription1 = $this->getFieldDescriptionMock('fooName1', 'fooLabel1');
+        $fieldDescription2 = $this->getFieldDescriptionMock('fooName2', 'fooLabel2');
+
+        $this->showMapper->add($fieldDescription1);
+        $this->showMapper->add($fieldDescription2);
+
+        $this->assertSame(array('fooName1', 'fooName2'), $this->showMapper->keys());
+    }
+
     public function testReorder()
     public function testReorder()
     {
     {
         $this->assertSame(array(), $this->admin->getShowGroups());
         $this->assertSame(array(), $this->admin->getShowGroups());