#native_company# #native_desc#
#native_cta#

GetString

By Pingveno
on July 22, 2005

Version: 1.0 beta 1

Type: Class

Category: HTTP

License: Other

Description: Assembles a URL or URI based on a target script and variables to be sent to via the “get” method

Licensed under Creative Commons Attribution-ShareAlike 2.5

<?php
<**
 * Assembles a URL or URI based on a target script and variables to be sent to via
 * the "get" method 
 * @author Aaron DeVore <[email protected]>
 * @license http://creativecommons.org/licenses/by-sa/2.5/ Creative Commons Attribution-ShareAlike 2.5
 */
/**
 * Creates a URL complete with get variables from a target page and variables
 *
 * A target script is set on initialization of the GetString object, then variables
 * are added via the addVar method. The final URL is returned using the getURL
 * method. An absolute URL is returned by getAbsoluteURL.
 * 
 * <code>
 * $geturl = new GetString ('foo.php')
 * $geturl->addVar('why?','because');
 * print '<a href="' . $geturl->getURL() . '" />';
 * </code>
 *  
 * 
 */
class GetString {
	var $getvars = array();
	var $RELATIVE = 'relative';
	var $ABSOLUTE = 'absolute';
	/**
	 * Constructor; sets target 
	 * 
	 * @param string URL of target script
	 * @param string type of URL (relative, absolute)
	 */
	function GetString ($target,$type='relative') {
		$result = $this->setTarget($target, $type);
		if (!$result) {
			return False;
		}
	}
	/**
	 * Add a variable to be included in the URL
	 *
	 * Values will be URL encoded, so don't worry about illegal characters
	 *
	 * @param string name of the variable
	 * @param string value of the variable
	 */
	function addVar($name, $value) {
		$this->getvars[$name] = $value;
	}
	/**
	 * Add all name => value variables in an array to the URL
	 *
	 * @param array Variables to be in the URL
	 */
	function addVars ($vars) {
		$this->getvars = array_merge($this->getvars, $vars);
	}
	function setTarget($target,$type='relative') {
		if ($type == 'relative') {
			$this->type = 'relative';
		} elseif ($type == 'absolute') {
			$this->type = 'relative';
		} else {
			return False;
		}
		$this->target = $target;
	}
	/**
	 * Get an absolute version of the fully constructed URL
	 *
	 * @return string URL
	 */
	function getAbsoluteURL() {
		if ($this->type == 'absolute') { // if target is already an absolute URL
			$url = $this->target;
		} else {
			$schema = $_SERVER['SERVER_PORT'] == '443' ? 'https' : 'http';
			$host = strlen($_SERVER['HTTP_HOST']) ? $_SERVER['HTTP_HOST']:$_SERVER['SERVER_NAME'];
			$patharray = explode("/",$_SERVER['PHP_SELF']);
			array_pop($patharray);
			$path = implode ("/",$patharray);
			$url = "$schema://{$host}{$path}/{$this->target}";
		}
		if (sizeof($this->getvars) != 0) {
			$url .= "?";
		}
		$beginning = True;
		foreach ($this->getvars as $name => $var) {
			if (!$beginning) {
				$url .= "&";
			}
			$url .= urlencode($name) . "=" . urlencode($var);
			$beginning = FALSE;
		}
		return $url;
	}
	/**
	 * Get the fully constructed URL
	 *
	 * @return string URL
	 */
	function getURL() {
		$url = $this->target;
		if (sizeof($this->getvars) != 0) {
			$url .= "?";
		}
		$beginning = TRUE;
		foreach ($this->getvars as $name => $var) {
			if (!$beginning) {
				$url .= "&";
			}
			$var = str_replace('+',' ',$var);
			$url .= urlencode($name) . "=" . urlencode($var);
			$beginning = FALSE;
		}
		return $url;
	}
	/**
	 * Returns a clone of the object
	 * 
	 * Clones are used when multiple URLs must be created, but with some common
	 * variables.
	 * <code>
	 * $me = new GetString('target.php');
	 * $me->addVar('foo','bar');
	 * $firstname = $me->getClone();
	 * $firstname->addVar('somename', 'somevalue');
	 * print $firstname->getURL(); // target.php?foo=bar&somename=somevalue
	 * $lastname = $me->getClone();
	 * $lastname->addVar('aname','avalue');
	 * print $lastname->getURL(); // target.php?foo=bar&aname=avalue
	 * </code>
	 * 
	 * @return GetString copy of current object
	 */
	function getClone() {
		$clone = new GetString($this->target, $this->type);
		$clone->addVars($this->getvars);
		return $clone;
	}
}