ソースを参照

ZF-8233
added $reverseSort argument to Zend_Ldap::searchEntries()
added possibility to pass parameters to Zend_Ldap::searchEntries() and Zend_Ldap::search() in a single array argument

git-svn-id: http://framework.zend.com/svn/framework/standard/trunk@18883 44c647ce-9c0f-0410-b52a-842ac1e357ba

sgehrig 16 年 前
コミット
731f364a2f
2 ファイル変更125 行追加13 行削除
  1. 68 13
      library/Zend/Ldap.php
  2. 57 0
      tests/Zend/Ldap/SearchTest.php

+ 68 - 13
library/Zend/Ldap.php

@@ -872,18 +872,49 @@ class Zend_Ldap
     /**
      * A global LDAP search routine for finding information.
      *
-     * @param  string|Zend_Ldap_Filter_Abstract $filter
-     * @param  string|Zend_Ldap_Dn|null         $basedn
-     * @param  integer                          $scope
-     * @param  array                            $attributes
-     * @param  string|null                      $sort
-     * @param  string|null                      $collectionClass
+     * Options can be either passed as single parameters according to the
+     * method signature or as an array with one or more of the following keys
+     * - filter
+     * - baseDn
+     * - scope
+     * - attributes
+     * - sort
+     * - collectionClass
+     *
+     * @param  string|Zend_Ldap_Filter_Abstract|array $filter
+     * @param  string|Zend_Ldap_Dn|null               $basedn
+     * @param  integer                                $scope
+     * @param  array                                  $attributes
+     * @param  string|null                            $sort
+     * @param  string|null                            $collectionClass
      * @return Zend_Ldap_Collection
      * @throws Zend_Ldap_Exception
      */
     public function search($filter, $basedn = null, $scope = self::SEARCH_SCOPE_SUB,
         array $attributes = array(), $sort = null, $collectionClass = null)
     {
+        if (is_array($filter)) {
+            $options = array_change_key_case($filter, CASE_LOWER);
+            foreach ($options as $key => $value) {
+                switch ($key) {
+                    case 'filter':
+                    case 'basedn':
+                    case 'scope':
+                    case 'sort':
+                        $$key = $value;
+                        break;
+                    case 'attributes':
+                        if (is_array($value)) {
+                            $attributes = $value;
+                        }
+                        break;
+                    case 'collectionclass':
+                        $collectionClass = $value;
+                        break;
+                }
+            }
+        }
+
         if ($basedn === null) {
             $basedn = $this->getBaseDn();
         }
@@ -1006,19 +1037,43 @@ class Zend_Ldap
     /**
      * Search LDAP registry for entries matching filter and optional attributes
      *
-     * @param  string|Zend_Ldap_Filter_Abstract $filter
-     * @param  string|Zend_Ldap_Dn|null         $basedn
-     * @param  integer                          $scope
-     * @param  array                            $attributes
-     * @param  string|null                      $sort
+     * Options can be either passed as single parameters according to the
+     * method signature or as an array with one or more of the following keys
+     * - filter
+     * - baseDn
+     * - scope
+     * - attributes
+     * - sort
+     * - reverseSort
+     *
+     * @param  string|Zend_Ldap_Filter_Abstract|array $filter
+     * @param  string|Zend_Ldap_Dn|null               $basedn
+     * @param  integer                                $scope
+     * @param  array                                  $attributes
+     * @param  string|null                            $sort
+     * @param  boolean                                $reverseSort
      * @return array
      * @throws Zend_Ldap_Exception
      */
     public function searchEntries($filter, $basedn = null, $scope = self::SEARCH_SCOPE_SUB,
-        array $attributes = array(), $sort = null)
+        array $attributes = array(), $sort = null, $reverseSort = false)
     {
+        if (is_array($filter)) {
+            $filter = array_change_key_case($filter, CASE_LOWER);
+            if (isset($filter['collectionclass'])) {
+                unset($filter['collectionclass']);
+            }
+            if (isset($filter['reversesort'])) {
+                $reverseSort = $filter['reversesort'];
+                unset($filter['reversesort']);
+            }
+        }
         $result = $this->search($filter, $basedn, $scope, $attributes, $sort);
-        return $result->toArray();
+        $items = $result->toArray();
+        if ((bool)$reverseSort === true) {
+            $items = array_reverse($items, false);
+        }
+        return $items;
     }
 
     /**

+ 57 - 0
tests/Zend/Ldap/SearchTest.php

@@ -328,6 +328,63 @@ class Zend_Ldap_SearchTest extends Zend_Ldap_OnlineTestCase
                 $zle->getMessage());
         }
     }
+
+    /**
+     * @group ZF-8233
+     */
+    public function testSearchWithOptionsArray()
+    {
+        $items=$this->_getLdap()->search(array(
+            'filter' => '(objectClass=organizationalUnit)',
+            'baseDn' => TESTS_ZEND_LDAP_WRITEABLE_SUBTREE,
+            'scope'  => Zend_Ldap::SEARCH_SCOPE_SUB
+        ));
+        $this->assertEquals(9, $items->count());
+    }
+
+    /**
+     * @group ZF-8233
+     */
+    public function testSearchEntriesShortcutWithOptionsArray()
+    {
+        $items=$this->_getLdap()->searchEntries(array(
+            'filter' => '(objectClass=organizationalUnit)',
+            'baseDn' => TESTS_ZEND_LDAP_WRITEABLE_SUBTREE,
+            'scope'  => Zend_Ldap::SEARCH_SCOPE_SUB
+        ));
+        $this->assertEquals(9, count($items));
+    }
+
+    /**
+     * @group ZF-8233
+     */
+    public function testReverseSortingWithSearchEntriesShortcut()
+    {
+        $lSorted = array('e', 'd', 'c', 'b', 'a');
+        $items = $this->_getLdap()->searchEntries('(l=*)', TESTS_ZEND_LDAP_WRITEABLE_SUBTREE,
+            Zend_Ldap::SEARCH_SCOPE_SUB, array(), 'l', true);
+        foreach ($items as $key => $item) {
+            $this->assertEquals($lSorted[$key], $item['l'][0]);
+        }
+    }
+
+    /**
+     * @group ZF-8233
+     */
+    public function testReverseSortingWithSearchEntriesShortcutWithOptionsArray()
+    {
+        $lSorted = array('e', 'd', 'c', 'b', 'a');
+        $items = $this->_getLdap()->searchEntries(array(
+            'filter'      => '(l=*)',
+            'baseDn'      => TESTS_ZEND_LDAP_WRITEABLE_SUBTREE,
+            'scope'       => Zend_Ldap::SEARCH_SCOPE_SUB,
+            'sort'        => 'l',
+            'reverseSort' => true
+        ));
+        foreach ($items as $key => $item) {
+            $this->assertEquals($lSorted[$key], $item['l'][0]);
+        }
+    }
 }
 
 class Zend_Ldap_SearchTest_CollectionClassNotSubclassingZendLdapCollection