Просмотр исходного кода

ZF-8263
added the possibility to change the attributes' name treatment in the Zend_Ldap_Collection_Iterator_Default.

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

sgehrig 16 лет назад
Родитель
Сommit
ba9e900381

+ 77 - 1
library/Zend/Ldap/Collection/Iterator/Default.php

@@ -30,6 +30,10 @@
  */
 class Zend_Ldap_Collection_Iterator_Default implements Iterator, Countable
 {
+    const ATTRIBUTE_TO_LOWER  = 1;
+    const ATTRIBUTE_TO_UPPER  = 2;
+    const ATTRIBUTE_NATIVE    = 3;
+
     /**
      * LDAP Connection
      *
@@ -59,6 +63,13 @@ class Zend_Ldap_Collection_Iterator_Default implements Iterator, Countable
     protected $_itemCount = -1;
 
     /**
+     * The method that will be applied to the attribute's names.
+     *
+     * @var  integer|callback
+     */
+    protected $_attributeNameTreatment = self::ATTRIBUTE_TO_LOWER;
+
+    /**
      * Constructor.
      *
      * @param  Zend_Ldap $ldap
@@ -111,6 +122,56 @@ class Zend_Ldap_Collection_Iterator_Default implements Iterator, Countable
     }
 
     /**
+     * Sets the attribute name treatment.
+     *
+     * Can either be one of the following constants
+     * - Zend_Ldap_Collection_Iterator_Default::ATTRIBUTE_TO_LOWER
+     * - Zend_Ldap_Collection_Iterator_Default::ATTRIBUTE_TO_UPPER
+     * - Zend_Ldap_Collection_Iterator_Default::ATTRIBUTE_NATIVE
+     * or a valid callback accepting the attribute's name as it's only
+     * argument and returning the new attribute's name.
+     *
+     * @param  integer|callback $attributeNameTreatment
+     * @return Zend_Ldap_Collection_Iterator_Default Provides a fluent interface
+     */
+    public function setAttributeNameTreatment($attributeNameTreatment)
+    {
+        if (is_callable($attributeNameTreatment)) {
+            if (is_string($attributeNameTreatment) && !function_exists($attributeNameTreatment)) {
+                $this->_attributeNameTreatment = self::ATTRIBUTE_TO_LOWER;
+            } else if (is_array($attributeNameTreatment) &&
+                    !method_exists($attributeNameTreatment[0], $attributeNameTreatment[1])) {
+                $this->_attributeNameTreatment = self::ATTRIBUTE_TO_LOWER;
+            } else {
+                $this->_attributeNameTreatment = $attributeNameTreatment;
+            }
+        } else {
+            $attributeNameTreatment = (int)$attributeNameTreatment;
+            switch ($attributeNameTreatment) {
+                case self::ATTRIBUTE_TO_LOWER:
+                case self::ATTRIBUTE_TO_UPPER:
+                case self::ATTRIBUTE_NATIVE:
+                    $this->_attributeNameTreatment = $attributeNameTreatment;
+                    break;
+                default:
+                    $this->_attributeNameTreatment = self::ATTRIBUTE_TO_LOWER;
+                    break;
+            }
+        }
+        return $this;
+    }
+
+    /**
+     * Returns the currently set attribute name treatment
+     *
+     * @return integer|callback
+     */
+    public function getAttributeNameTreatment()
+    {
+        return $this->_attributeNameTreatment;
+    }
+
+    /**
      * Returns the number of items in current result
      * Implements Countable
      *
@@ -144,7 +205,22 @@ class Zend_Ldap_Collection_Iterator_Default implements Iterator, Countable
         while ($name) {
             $data = @ldap_get_values_len($this->_ldap->getResource(), $this->_current, $name);
             unset($data['count']);
-            $entry[strtolower($name)] = $data;
+
+            switch($this->_attributeNameTreatment) {
+                case self::ATTRIBUTE_TO_LOWER:
+                    $attrName = strtolower($name);
+                    break;
+                case self::ATTRIBUTE_TO_UPPER:
+                    $attrName = strtoupper($name);
+                    break;
+                case self::ATTRIBUTE_NATIVE:
+                    $attrName = $name;
+                    break;
+                default:
+                    $attrName = call_user_func($this->_attributeNameTreatment, $name);
+                    break;
+            }
+            $entry[$attrName] = $data;
             $name = @ldap_next_attribute($this->_ldap->getResource(), $this->_current,
                 $ber_identifier);
         }

+ 1 - 1
tests/Zend/Ldap/OnlineTestCase.php

@@ -110,7 +110,7 @@ abstract class Zend_Ldap_OnlineTestCase extends Zend_Ldap_TestCase
     {
         $this->_nodes=array(
             $this->_createDn('ou=Node,') =>
-                array("objectClass" => "organizationalUnit", "ou" => "Node"),
+                array("objectClass" => "organizationalUnit", "ou" => "Node", "postalCode" => "1234"),
             $this->_createDn('ou=Test1,ou=Node,') =>
                 array("objectClass" => "organizationalUnit", "ou" => "Test1"),
             $this->_createDn('ou=Test2,ou=Node,') =>

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

@@ -520,6 +520,91 @@ class Zend_Ldap_SearchTest extends Zend_Ldap_OnlineTestCase
         foreach ($items as $item) { $j++; }
         $this->assertEquals($i, $j);
     }
+
+    /**
+     * @group ZF-8263
+     */
+    public function testAttributeNameTreatmentToLower()
+    {
+        $dn = $this->_createDn('ou=Node,');
+        $list = $this->_getLdap()->search('objectClass=*', $dn, Zend_Ldap::SEARCH_SCOPE_BASE);
+        $list->getInnerIterator()->setAttributeNameTreatment(Zend_Ldap_Collection_Iterator_Default::ATTRIBUTE_TO_LOWER);
+        $this->assertArrayHasKey('postalcode', $list->current());
+    }
+
+    /**
+     * @group ZF-8263
+     */
+    public function testAttributeNameTreatmentToUpper()
+    {
+        $dn = $this->_createDn('ou=Node,');
+        $list = $this->_getLdap()->search('objectClass=*', $dn, Zend_Ldap::SEARCH_SCOPE_BASE);
+        $list->getInnerIterator()->setAttributeNameTreatment(Zend_Ldap_Collection_Iterator_Default::ATTRIBUTE_TO_UPPER);
+        $this->assertArrayHasKey('POSTALCODE', $list->current());
+    }
+
+    /**
+     * @group ZF-8263
+     */
+    public function testAttributeNameTreatmentNative()
+    {
+        $dn = $this->_createDn('ou=Node,');
+        $list = $this->_getLdap()->search('objectClass=*', $dn, Zend_Ldap::SEARCH_SCOPE_BASE);
+        $list->getInnerIterator()->setAttributeNameTreatment(Zend_Ldap_Collection_Iterator_Default::ATTRIBUTE_NATIVE);
+        $this->assertArrayHasKey('postalCode', $list->current());
+    }
+
+    /**
+     * @group ZF-8263
+     */
+    public function testAttributeNameTreatmentCustomFunction()
+    {
+        $dn = $this->_createDn('ou=Node,');
+        $list = $this->_getLdap()->search('objectClass=*', $dn, Zend_Ldap::SEARCH_SCOPE_BASE);
+        $list->getInnerIterator()->setAttributeNameTreatment('Zend_Ldap_SearchTest_customNaming');
+        $this->assertArrayHasKey('EDOCLATSOP', $list->current());
+    }
+
+    /**
+     * @group ZF-8263
+     */
+    public function testAttributeNameTreatmentCustomStaticMethod()
+    {
+        $dn = $this->_createDn('ou=Node,');
+        $list = $this->_getLdap()->search('objectClass=*', $dn, Zend_Ldap::SEARCH_SCOPE_BASE);
+        $list->getInnerIterator()->setAttributeNameTreatment(array('Zend_Ldap_SearchTest_CustomNaming', 'name1'));
+        $this->assertArrayHasKey('edoclatsop', $list->current());
+    }
+
+    /**
+     * @group ZF-8263
+     */
+    public function testAttributeNameTreatmentCustomInstanceMethod()
+    {
+        $dn = $this->_createDn('ou=Node,');
+        $list = $this->_getLdap()->search('objectClass=*', $dn, Zend_Ldap::SEARCH_SCOPE_BASE);
+        $namer = new Zend_Ldap_SearchTest_CustomNaming();
+        $list->getInnerIterator()->setAttributeNameTreatment(array($namer, 'name2'));
+        $this->assertArrayHasKey('edoClatsop', $list->current());
+    }
+}
+
+function Zend_Ldap_SearchTest_customNaming($attrib)
+{
+    return strtoupper(strrev($attrib));
+}
+
+class Zend_Ldap_SearchTest_CustomNaming
+{
+    public static function name1($attrib)
+    {
+        return strtolower(strrev($attrib));
+    }
+
+    public function name2($attrib)
+    {
+        return strrev($attrib);
+    }
 }
 
 class Zend_Ldap_SearchTest_CollectionClassNotSubclassingZendLdapCollection