<?php
/*
 * Copyright (c) 2012, webvariants GbR, http://www.webvariants.de
 *
 * This file is released under the terms of the MIT license. You can find the
 * complete text in the attached LICENSE file or online at:
 *
 * http://www.opensource.org/licenses/mit-license.php
 */

/**
 * Instanz einer URL
 *
 * @since  v4.0
 * @author Christoph
 */
class _WV24_URL {
	protected $namespace;
	protected $ident;
	protected $path;
	protected $url;
	protected $articleID;
	protected $clangID;
	protected $created;
	protected $params;

	public function __construct(array $params) {
		$this->namespace = $params['namespace'];
		$this->ident     = $params['ident'];
		$this->path      = $params['path'];
		$this->url       = $params['url'];
		$this->articleID = (int) $params['article_id'];
		$this->clangID   = (int) $params['clang'];
		$this->created   = isset($params['unix']) ? (int) $params['unix'] : strtotime($params['created']);
		$this->params    = empty($params['params']) ? array() : (is_array($params['params']) ? $params['params'] : json_decode($params['params'], true));
	}

	/**
	 * URL ermitteln
	 *
	 * Diese Methode gibt die in dieser Route enthaltene URL zurück. Auf Wunsch
	 * können GET-Parameter hinzugefügt werden.
	 *
	 * @param  mixed   $params       die GET-Parameter entweder als Array oder bereits als Query-String
	 * @param  string  $divider      der Divider für den Query-String (nur relevant, wenn $params ein array ist)
	 * @param  boolean $forceDomain  wenn true, wird immer die Domain eingefügt (ansonsten nur wenn nötig)
	 * @return string                die URL
	 */
	public function getURL($params = array(), $divider = '&amp;', $forceDomain = false) {
		$url    = $this->url;
		$params = WV24_Util::buildQuery($params, $divider);

		// cut off domain if possible
		$url = WV24_Util::shortenUrl($url, $forceDomain);

		return rtrim(rtrim($url.$params, '&'), '?');
	}

	public function getNamespace() { return $this->namespace; }
	public function getIdent()     { return $this->ident;     }
	public function getPath()      { return $this->path;      }
	public function getArticleID() { return $this->articleID; }
	public function getClang()     { return $this->clangID;   }
	public function getCreated()   { return $this->created;   }
	public function getParams()    { return $this->params;    }
	public function getRawURL()    { return $this->url;       }
}
