Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Initial commit

  • Loading branch information...
commit 107e91a15b742919b9f8e8d2b05dbcf409025875 0 parents
David Coallier authored
72 Services/URL.php
@@ -0,0 +1,72 @@
+<?php
+require_once 'Services/URL/Exception.php';
+
+require_once 'Services/URL/Drivers/Common.php';
+require_once 'Services/URL/Drivers/Interface.php';
+/**
+ * Services_URL Class
+ *
+ * This class is used in order to retrieve instances
+ * and should not be instantiated by itself.
+ *
+ * @category Services
+ * @author David Coallier <david@echolibre.com>
+ * @package Services_URL
+ * @copyright echolibre ltd. 2008
+ * @license http://www.opensource.org/licenses/bsd-license.php New BSD License
+ */
+abstract class Services_URL
+{
+ const OUTPUT_XML = 'xml';
+ const OUTPUT_JSON = 'json';
+
+ /**
+ * Instances
+ *
+ * This variables stores the instances
+ * that are already instantiated of Services_URL_*
+ * objects.
+ */
+ protected static $instances = array();
+
+ /**
+ * Factory method
+ *
+ * This method will be used mostly to generate
+ * or retrieve existing objects of the requested
+ * URL Shortening service's API.
+ *
+ * @param string $method The method to load (Driver)
+ * @return mixed self::$instances[$method] An instance of the requested
+ * driver.
+ */
+ public static function factory($method)
+ {
+ $method = ucfirst(strtolower($method));
+
+ if (isset(self::$instances[$method])) {
+ return self::$instances[$method];
+ }
+
+ $file = 'Services/URL/Drivers/' . $method . '.php';
+ if (!file_exists($file)) {
+ throw new Services_URL_Exception(
+ Services_URL_Exception::DRIVER_DOES_NOT_EXIST
+ );
+ }
+ include_once $file;
+
+ $class = 'Services_URL_Driver_' . $method;
+ if (!class_exists($class)) {
+ throw new Services_URL_Exception(
+ Services_URL_Exception::CLASS_DOES_NOT_EXIST
+ );
+ }
+
+ self::$instances[$method] = new $class();
+ return self::$instances[$method];
+ }
+}
+
+/* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4 foldmethod=marker: */
+?>
80 Services/URL/Drivers/Common.php
@@ -0,0 +1,80 @@
+<?php
+require_once 'HTTP/Request2.php';
+/**
+ * Services URL Driver Common
+ *
+ * This is the class that places the request and either
+ * returns the result as clear text, or as a json or
+ * an xml object.
+ *
+ * @category Services
+ * @package Services_URL
+ * @author David Coallier <david@echolibre.com>
+ * @copyright echolibre ltd. 2008
+ * @uses HTTP_Request2
+ */
+abstract class Services_URL_Driver_Common
+{
+ /**
+ * Get the request
+ *
+ * Place the request using the
+ * decided url.
+ *
+ * This is using HTT_Request2 to make the
+ * request and returns the body of the
+ * HTTP_Request2_Response.
+ *
+ * @param string $url The url to place the query on.
+ * @use HTTP_Request2
+ *
+ * @return mixed Either the body of the request
+ * or a false response.
+ */
+ protected function _getRequest($url)
+ {
+ $req = new HTTP_Request2($url);
+ $response = $req->send();
+
+ return $response->getBody();
+ }
+
+ /**
+ * Get JSON
+ *
+ * Place the request on the server and
+ * get the request in form of a json object
+ *
+ * @param string $url The url to place the request on.
+ * @return JsonObj The json_decoded object or false
+ */
+ protected function _getJSON($url)
+ {
+ $jsonResult = $this->_getRequest($url);
+ if ($jsonResult != '') {
+ return json_decode($jsonResult);
+ }
+
+ return false;
+ }
+
+ /**
+ * Get XML
+ *
+ * This method will return the SimpleXML
+ * element object.
+ *
+ * @param string $url The url to place the request on.
+ * @return SimpleXML Either a simplexml element object or
+ * boolean false value
+ */
+ protected function _getXML()
+ {
+ $xmlResult = $this->_getRequest($url);
+ if ($xmlResult != '') {
+ return simplexml_load_string($xmlResult);
+ }
+
+ return false;
+ }
+}
39 Services/URL/Drivers/Interface.php
@@ -0,0 +1,39 @@
+<?php
+/**
+ * Services URL Interface
+ *
+ * Anyone in the services_url family must be
+ * implementing this in order to be compliant
+ * to the driver standard.
+ *
+ * @author David Coallier <david@echolibre.com>
+ * @package Services_URL
+ * @category Services
+ * @copyright echolibre ltd. 2008
+ */
+interface Services_URL_Driver_Interface
+{
+ /**
+ * Authorize
+ *
+ * This method is either used to authorize
+ * or set the values for authentication
+ * at the API level. Every drivers should have
+ * this method.
+ *
+ * @param array $params The array of values
+ * for the authorization
+ */
+ public function authorize(array $params);
+
+ /**
+ * Shorten the url
+ *
+ * This is the method that all drivers
+ * will have in order to shorten the values
+ *
+ * @param array $params The parameters associated
+ * to the url shortening service.
+ */
+ public function shorten(array $params);
+}
104 Services/URL/Drivers/Shortie.php
@@ -0,0 +1,104 @@
+<?php
+/**
+ * Services URL Driver Shortie
+ *
+ * This is the class/driver used to make requests
+ * on the short.ie api.
+ *
+ * @category Services
+ * @package Services_URL
+ * @author David Coallier <david@echolibre.com>
+ * @copyright echolibre ltd. 2008
+ */
+class Services_URL_Driver_Shortie
+ extends Services_URL_Driver_Common
+ implements Services_URL_Driver_Interface
+{
+ /**
+ * The URL
+ *
+ * @global The url to place the request on.
+ */
+ const URL = 'http://short.ie/api?';
+
+ /**
+ * The short.ie email key
+ *
+ * In order to authorize, the short.ie
+ * api uses your email address.
+ *
+ * @var string $email The email to login with (Default null)
+ */
+ private $email = null;
+
+ /**
+ * The short.ie secret key
+ *
+ * In order to authorize, the short.ie
+ * api uses a secret key that you previously
+ * generated on the site.
+ */
+ private $secretKey = null;
+
+ public function __construct() {}
+
+ /**
+ * Authorize
+ *
+ * It is possible to use the short.ie api
+ * anonymously, however if you want to track
+ * your clicks, you will need to pass your
+ * email and secretKey as array keys here.
+ *
+ * @param array $params The authentication parameters
+ * @see $this->email
+ * @see $this->secretKey
+ * @return boolean true
+ */
+ public function authorize(array $params)
+ {
+ if (isset($params['email'])) {
+ $this->email = $params['email'];
+ }
+
+ if (isset($params['secretKey'])) {
+ $this->secretKey = $params['secretKey'];
+ }
+
+ return true;
+ }
+
+ /**
+ * The most important part. Shorten the URL
+ *
+ * Shorten the URL. This is the most important
+ * part of this is definitely the urlParts passed
+ * to the method.
+ *
+ * The urlParts parameters is constructed of 1 required key
+ * and 2 optional parameters.
+ * <ul>
+ * <li>(Required)url: The original url</li>
+ * <li>(Optional)custom: The custom url ot use</li>
+ * <li>(Optional)private: Is the url private? (Show or not in the public links)</li>
+ * </ul>
+ *
+ * @param array $urlParts The url parts
+ * @see Services_URL_Drvier_Common::_getJSON()
+ * @return mixed Either a result object or false
+ */
+ public function shorten(array $urlParts)
+ {
+ if (!isset($urlParts['url'])) {
+ return false;
+ }
+
+ $urlParts['email'] = $this->email;
+ $urlParts['secretKey'] = $this->secretKey;
+
+ $urlParts = http_build_query($urlParts);
+
+ $res = $this->_getJSON(self::URL . $urlParts);
+ return $res;
+ }
+}
18 Services/URL/Exception.php
@@ -0,0 +1,18 @@
+<?php
+/**
+ * Exception class
+ *
+ * This is the Services_URL_Exception class that is
+ * used thorough the system. You can edit and change
+ * The exception messages
+ *
+ * @author David Coallier <david@echolibre.com>
+ * @package Services_URL
+ * @abstract The Services_URL_Exception class
+ * @uses Exception
+ */
+class Services_URL_Exception extends Exception
+{
+ const DRIVER_DOES_NOT_EXIST = 'Driver does not exist.';
+ const CLASS_DOES_NOT_EXIST = 'Class does not exist.';
+}

0 comments on commit 107e91a

Please sign in to comment.
Something went wrong with that request. Please try again.