Forráskód Böngészése

ZF-6929: Made it possible pass in the username and password as an array so it's easy to pull from a Zend_Config and such.

git-svn-id: http://framework.zend.com/svn/framework/standard/trunk@18101 44c647ce-9c0f-0410-b52a-842ac1e357ba
sidhighwind 16 éve
szülő
commit
f34a751056

+ 10 - 0
documentation/manual/en/module_specs/Zend_Service_Twitter.xml

@@ -89,6 +89,16 @@ $twitter = new Zend_Service_Twitter('myusername', 'mysecretpassword');
 // verify your credentials with twitter
 $response = $twitter->account->verifyCredentials();
 ]]></programlisting>
+
+			<para>
+                You can also pass in an array as the first variable that contains the username and password.
+            </para>
+			            <programlisting language="php"><![CDATA[
+						$userInfo = array('username' => 'foo', 'password' => 'bar');
+$twitter = new Zend_Service_Twitter(userInfo);
+// verify your credentials with twitter
+$response = $twitter->account->verifyCredentials();
+]]></programlisting>
         </example>
     </sect2>
 

+ 16 - 8
library/Zend/Service/Twitter.php

@@ -77,10 +77,20 @@ class Zend_Service_Twitter extends Zend_Rest_Client
      * @param  string $password
      * @return void
      */
-    public function __construct ($username, $password)
-    {
-        $this->setUsername($username);
-        $this->setPassword($password);
+    public function __construct ($username, $password = null)
+    {
+        if (is_array($username) && is_null($password)) {
+            if (isset($username['username']) && isset($username['password'])) {
+                $this->setUsername($username['username']);
+                $this->setPassword($username['password']);
+            } elseif (isset($username[0]) && isset($username[1])) {
+                $this->setUsername($username[0]);
+                $this->setPassword($username[1]);
+            }
+        } else {
+            $this->setUsername($username);
+            $this->setPassword($password);
+        }
         $this->setUri('http://twitter.com');
         $client = self::getHttpClient();
         $client->setHeaders('Accept-Charset', 'ISO-8859-1,utf-8');
@@ -698,12 +708,10 @@ class Zend_Service_Twitter extends Zend_Rest_Client
      * @return integer
      */
     protected function _validInteger ($int)
-    {
-        if(preg_match("/(\d+)/",$int))
-        {
+    {
+        if (preg_match("/(\d+)/", $int)) {
             return $int;
         }
-
         return 0;
     }
 }

+ 12 - 0
tests/Zend/Service/TwitterTest.php

@@ -91,6 +91,18 @@ class Zend_Service_TwitterTest extends PHPUnit_Framework_TestCase
         $this->assertEquals(TESTS_ZEND_SERVICE_TWITTER_PASS, $this->twitter->getPassword());
     }
 
+	/**
+     * @return void
+     */
+    public function testConstructorShouldAllowUsernamePasswordAsArray()
+    {
+        $userInfo = array('username' => 'foo', 'password' => 'bar');
+
+        $twit = new Zend_Service_Twitter($userInfo);
+        $this->assertEquals('foo', $twit->getUsername());
+        $this->assertEquals('bar', $twit->getPassword());
+    }
+
     /**
      * @return void
      */