From 18769636f0b1bcfc414ad0bab64bacf22ce7d652 Mon Sep 17 00:00:00 2001 From: Joe Stump Date: Tue, 28 Apr 2009 17:09:56 -0400 Subject: [PATCH] Updated Common and Bitly to conform to PHPCS. --- Services/ShortURL.php | 2 +- Services/ShortURL/Bitly.php | 32 +++++++++++++++++++-- Services/ShortURL/Common.php | 55 ++++++++++++++++++++++++++++++++++-- 3 files changed, 82 insertions(+), 7 deletions(-) diff --git a/Services/ShortURL.php b/Services/ShortURL.php index be460be..70ac54e 100644 --- a/Services/ShortURL.php +++ b/Services/ShortURL.php @@ -5,7 +5,7 @@ * * PHP version 5.2.0+ * - * LICENSE: This source file is subject to the New BSD license that is + * LICENSE: This source file is subject to the New BSD license that is * available through the world-wide-web at the following URI: * http://www.opensource.org/licenses/bsd-license.php. If you did not receive * a copy of the New BSD License and are unable to obtain it through the web, diff --git a/Services/ShortURL/Bitly.php b/Services/ShortURL/Bitly.php index dcaa50e..ccc6847 100644 --- a/Services/ShortURL/Bitly.php +++ b/Services/ShortURL/Bitly.php @@ -47,8 +47,17 @@ class Services_ShortURL_Bitly * * @var string $api The URL for the API */ - private $api = 'http://api.bit.ly'; + protected $api = 'http://api.bit.ly'; + /** + * Constructor + * + * @param array $options The service options array + * @param object $req The request object + * + * @throws {@link Services_ShortURL_Exception_InvalidOptions} + * @return void + */ public function __construct(array $options = array(), HTTP_Request2 $req = null) { @@ -67,6 +76,15 @@ public function __construct(array $options = array(), } } + /** + * Shorten a URL using {@link http://bit.ly} + * + * @param string $url The URL to shorten + * + * @throws {@link Services_ShortURL_Exception_CouldNotShorten} + * @return string The shortened URL + * @see Services_ShortURL_Bitly::sendRequest() + */ public function shorten($url) { $params = array( @@ -87,14 +105,22 @@ public function shorten($url) return (string)$xml->results->nodeKeyVal->shortUrl; } - private function sendRequest($url) + /** + * Send a request to {@link http://bit.ly} + * + * @param string $url The URL to send the request to + * + * @throws {@link Services_ShortURL_Exception_CouldNotShorten} + * @return object Instance of SimpleXMLElement + */ + protected function sendRequest($url) { $this->req->setUrl($url); $this->req->setMethod('GET'); $result = $this->req->send(); if ($result->getStatus() != 200) { - throw new Services_ShortURL_Exception_CouldNotExpand( + throw new Services_ShortURL_Exception_CouldNotShorten( 'Non-300 code returned', $result->getStatus() ); } diff --git a/Services/ShortURL/Common.php b/Services/ShortURL/Common.php index 3a10a26..97c7a60 100644 --- a/Services/ShortURL/Common.php +++ b/Services/ShortURL/Common.php @@ -35,9 +35,35 @@ */ abstract class Services_ShortURL_Common { + /** + * Service options + * + * Some services require an API key, username/password, or other + * non-standard information. Those options are set on a per-service + * basis and passed to the constructor. + * + * @var array $options Service options + * @see Services_ShortURL::$options + * @see Services_ShortURL::setServiceOptions() + */ protected $options = array(); + + /** + * Instance of {@link HTTP_Request2} + * + * @var object $req Instance of {@link HTTP_Request2} + * @see Services_ShortURL_Common::__construct() + */ protected $req = null; + /** + * Constructor + * + * @param array $options Service options + * @param object $req Provide your own {@link HTTP_Request2} instance + * + * @return void + */ public function __construct(array $options = array(), HTTP_Request2 $req = null) { @@ -46,14 +72,24 @@ public function __construct(array $options = array(), } else { $this->req = new HTTP_Request2(); $this->req->setAdapter('Curl'); - $this->req->setHeader( - 'User-Agent', get_class($this) . ' @version@' - ); + $this->req->setHeader('User-Agent', get_class($this) . ' @version@'); } $this->options = $options; } + /** + * Acceptor pattern + * + * By default, {@link Services_ShortURL} will create a cURL version of + * {@link HTTP_Request2}. If you need to override this you can use the + * accept method or pass an instance into the constructor. + * + * @param object $object Instance of {@link HTTP_Request2} + * + * @throws InvalidArgumentException on invalid object + * @return void + */ public function accept($object) { if (!is_object($object)) { @@ -69,6 +105,19 @@ public function accept($object) } } + /** + * Default expand method + * + * All of the URL shortening services, for the most part, do a 301 redirect + * using the Location header. Rather than implement this over and over we + * provide it here and assume others who need non-normal expansion will + * override this method. + * + * @param string $url The URL to expand + * + * @throws {@link Services_ShortURL_Exception_CouldNotExpand} on non-300's. + * @return string $url The expanded URL + */ public function expand($url) { $this->req->setUrl($url);