Browse Source

Merge pull request #6706 from cakephp/master-tree

3.0: Extract formatTreeList() from findTreeList()
José Lorenzo Rodríguez 10 years ago
parent
commit
5da52da74d
2 changed files with 70 additions and 17 deletions
  1. 40 17
      src/ORM/Behavior/TreeBehavior.php
  2. 30 0
      tests/TestCase/ORM/Behavior/TreeBehaviorTest.php

+ 40 - 17
src/ORM/Behavior/TreeBehavior.php

@@ -64,7 +64,8 @@ class TreeBehavior extends Behavior
             'moveDown' => 'moveDown',
             'recover' => 'recover',
             'removeFromTree' => 'removeFromTree',
-            'getLevel' => 'getLevel'
+            'getLevel' => 'getLevel',
+            'formatTreeList' => 'formatTreeList'
         ],
         'parent' => 'parent_id',
         'left' => 'lft',
@@ -441,35 +442,57 @@ class TreeBehavior extends Behavior
      * the primary key for the table and the values are the display field for the table.
      * Values are prefixed to visually indicate relative depth in the tree.
      *
-     * Available options are:
+     * ### Options
      *
      * - keyPath: A dot separated path to fetch the field to use for the array key, or a closure to
-     *  return the key out of the provided row.
+     *   return the key out of the provided row.
      * - valuePath: A dot separated path to fetch the field to use for the array value, or a closure to
-     *  return the value out of the provided row.
+     *   return the value out of the provided row.
      * - spacer: A string to be used as prefix for denoting the depth in the tree for each item
      *
      * @param \Cake\ORM\Query $query Query.
-     * @param array $options Array of options as described above
+     * @param array $options Array of options as described above.
      * @return \Cake\ORM\Query
      */
     public function findTreeList(Query $query, array $options)
     {
-        return $this->_scope($query)
+        $results = $this->_scope($query)
             ->find('threaded', [
                 'parentField' => $this->config('parent'),
                 'order' => [$this->config('left') => 'ASC']
-            ])
-            ->formatResults(function ($results) use ($options) {
-                $options += [
-                    'keyPath' => $this->_getPrimaryKey(),
-                    'valuePath' => $this->_table->displayField(),
-                    'spacer' => '_'
-                ];
-                return $results
-                    ->listNested()
-                    ->printer($options['valuePath'], $options['keyPath'], $options['spacer']);
-            });
+            ]);
+        return $this->formatTreeList($results, $options);
+    }
+
+    /**
+     * Formats query as a flat list where the keys are the primary key for the table
+     * and the values are the display field for the table. Values are prefixed to visually
+     * indicate relative depth in the tree.
+     *
+     * ### Options
+     *
+     * - keyPath: A dot separated path to the field that will be the result array key, or a closure to
+     *   return the key from the provided row.
+     * - valuePath: A dot separated path to the field that is the array's value, or a closure to
+     *   return the value from the provided row.
+     * - spacer: A string to be used as prefix for denoting the depth in the tree for each item.
+     *
+     * @param \Cake\ORM\Query $query The query object to format.
+     * @param array $options Array of options as described above.
+     * @return \Cake\ORM\Query Augmented query.
+     */
+    public function formatTreeList(Query $query, array $options = [])
+    {
+        return $query->formatResults(function ($results) use ($options) {
+            $options += [
+                'keyPath' => $this->_getPrimaryKey(),
+                'valuePath' => $this->_table->displayField(),
+                'spacer' => '_'
+            ];
+            return $results
+                ->listNested()
+                ->printer($options['valuePath'], $options['keyPath'], $options['spacer']);
+        });
     }
 
     /**

+ 30 - 0
tests/TestCase/ORM/Behavior/TreeBehaviorTest.php

@@ -327,6 +327,36 @@ class TreeBehaviorTest extends TestCase
     }
 
     /**
+     * Tests the testFormatTreeListCustom() method.
+     *
+     * @return void
+     */
+    public function testFormatTreeListCustom()
+    {
+        $table = TableRegistry::get('MenuLinkTrees');
+        $table->addBehavior('Tree');
+
+        $query = $table
+            ->find('threaded')
+            ->where(['menu' => 'main-menu']);
+
+        $options = ['keyPath' => 'url', 'valuePath' => 'id', 'spacer' => ' '];
+        $result = $table->formatTreeList($query, $options)->toArray();
+
+        $expected = [
+            '/link1.html' => '1',
+            'http://example.com' => ' 2',
+            '/what/even-more-links.html' => ' 3',
+            '/lorem/ipsum.html' => '  4',
+            '/what/the.html' => '   5',
+            '/yeah/another-link.html' => '6',
+            'http://cakephp.org' => ' 7',
+            '/page/who-we-are.html' => '8'
+        ];
+        $this->assertEquals($expected, $result);
+    }
+
+    /**
      * Tests the moveUp() method
      *
      * @return void