Selaa lähdekoodia

Added basis of Array and SQl plugin.

Taddes Kroes 14 vuotta sitten
vanhempi
sitoutus
06bcb1db3b
5 muutettua tiedostoa jossa 255 lisäystä ja 139 poistoa
  1. 0 1
      .gitignore
  2. 38 0
      array.php
  3. 190 0
      base.php
  4. 0 138
      pquery.php
  5. 27 0
      sql.php

+ 0 - 1
.gitignore

@@ -1,3 +1,2 @@
 docs
 index.php
-sql.php

+ 38 - 0
array.php

@@ -0,0 +1,38 @@
+<?php
+
+/**
+ * @todo Documentation
+ */
+class pQueryArray extends pQuery {
+	function get($index) {
+		return isset($this->variable[$index]) ? $this->variable[$index] : null;
+	}
+	
+	function count() {
+		return count($this->variable);
+	}
+	
+	function is_empty() {
+		return !$this->count();
+	}
+	
+	function reverse() {
+		$this->variable = array_reverse($this->variable);
+		
+		return $this;
+	}
+	
+	function __call($method, $args) {
+		$function = 'array_'.$method;
+		
+		if( function_exists($function) ) {
+			array_unshift($args, &$this->variable);
+			
+			return call_user_func_array($function, $args);
+		}
+		
+		return self::error('Plugin "%s" has no method "%s".', __CLASS__, $method);
+	}
+}
+
+?>

+ 190 - 0
base.php

@@ -0,0 +1,190 @@
+<?php
+/**
+ * Base for pQuery PHP utility framework.
+ * 
+ * @package pQuery
+ */
+
+/**
+ * Indicates whether the framework is in debug mode.
+ * 
+ * @var bool
+ */
+defined('DEBUG') || define('DEBUG', true);
+
+/**
+ * The root location of the pQuery framework folder.
+ * 
+ * @var string
+ */
+define('PQUERY_ROOT', 'D:/xampp/htdocs/pquery/');
+
+/**
+ * Common utility class.
+ */
+class pQuery {
+	/**
+	 * The minimum php version required to use the framework.
+	 * 
+	 * @var string
+	 */
+	static $REQUIRED_PHP_VERSION = '5.3';
+	
+	/**
+	 * A list of all plugins currently included.
+	 * 
+	 * @var array
+	 */
+	static $plugins = array();
+	
+	/**
+	 * The variable types accepted by the parser.
+	 * 
+	 * @var array
+	 * @see set_variable()
+	 */
+	static $accepts = array('boolean', 'integer', 'double', 'string', 'array', 'object', 'NULL');
+	
+	/**
+	 * The current variable.
+	 * 
+	 * @var mixed
+	 */
+	var $variable;
+	
+	/**
+	 * Extend pQuery with a plugin.
+	 * 
+	 * @param string $class_name The name of the plugin's base class.
+	 * @param string $alias The alias to save for the plugin (defaults to $class_name).
+	 * @see $plugins
+	 */
+	static function extend($class_name, $alias=null) {
+		// Assert plugin existance
+		if( !class_exists($class_name) )
+			return self::error('Plugin "%s" does not exist.', $class_name);
+		
+		// Assert that the plugin extend the base clas properly
+		if( !in_array('pQueryExtension', class_implements($class_name)) )
+			return self::error('Plugin "%s" does not implement pQueryExtension.', $class_name);
+		
+		// Assert that the required PHP version is installed
+		if( isset($class_name::$REQUIRED_PHP_VERSION)
+				&& version_compare(PHP_VERSION, $class_name::$REQUIRED_PHP_VERSION, '<') ) {
+			return self::error('Plugin "%s" requires PHP version %s.',
+				$class_name, $class_name::$REQUIRED_PHP_VERSION);
+		}
+		
+		self::$plugins[$alias === null ? $class_name : $alias] = $class_name;
+	}
+	
+	/**
+	 * Display an error message if in {@link DEBUG} mode.
+	 * 
+	 * The optional arguments are passed to {@link printf}, along with $error.
+	 * 
+	 * @param string $error The error message to display.
+	 */
+	static function error($error/*, $arg1, $arg2...*/) {
+		$args = func_get_args();
+		
+		if( DEBUG ) {
+			call_user_func_array('printf', $args);
+			//echo debug_backtrace();
+		}
+	}
+	
+	/**
+	 * Constructor.
+	 * 
+	 * @param mixed $variable The variable to use an utility on.
+	 */
+	function __construct($variable) {
+		$this->set_variable($variable);
+	}
+	
+	/**
+	 * Parse the type of the given variable, and convert it if needed.
+	 * 
+	 * @param mixed $variable The variable to parse.
+	 * @param bool $force Whether not to check the variables type against the accepted types.
+	 */
+	function set_variable($variable, $force=false) {
+		if( !$force ) {
+			$type = gettype($variable);
+			$class_name = get_class($this);
+			$accepts = $class_name::$accepts;
+			
+			if( isset($accepts[$type]) ) {
+				$convert_method = $accepts[$type];
+				
+				if( !method_exists($this, $convert_method) )
+					return self::error('Plugin "%s" has no conversion method "%s".', $class_name, $convert_method);
+				
+				$result = $this->$convert_method($variable);
+				$result === null || $variable = $result;
+			} else if( !in_array($type, $accepts) ) {
+				return self::error('Variable type "%s" is not accepted by class "%s".', $type, $class_name);
+			}
+		}
+		
+		$this->variable = $variable;
+	}
+	
+	/**
+	 * Load the file containing the utility class for a specific variable type.
+	 * 
+	 * @param mixed $typoe the variable type of the class to load.
+	 */
+	static function load_type_class($type) {
+		$file = PQUERY_ROOT.$type.'.php';
+		
+		if( !file_exists($file) )
+			return false;
+		
+		include_once $file;
+		
+		return true;
+	}
+}
+
+/**
+ * Interface used for extending the jQuery class.
+ */
+interface pQueryExtension {
+	/**
+	 * Constructor.
+	 * 
+	 * @param mixed $variable The variable to use an utility on.
+	 */
+	function __construct($variable);
+}
+
+/**
+ * Shortcut constructor for {@link pQuery}.
+ * 
+ * @param mixed $variable The variable to use an utility on.
+ * @param string $plugin The name of an utility plugin to use (optional).
+ * @returns pQuery A new pQuery (or descendant) instance.
+ */
+function _p($variable, $plugin=null) {
+	$class_name = 'pQuery';
+	
+	if( $plugin === null ) {
+		// Use custom class for this variable type
+		$type = gettype($variable);
+		
+		if( pQuery::load_type_class($type) )
+			$class_name .= ucfirst($type);
+	} else {
+		// Use custom plugin class
+		if( isset(pQuery::$plugins[$plugin]) )
+			$class_name = pQuery::$plugins[$plugin];
+		else if( DEBUG )
+			pQuery::error('Plugin "%s" does not exist.', $plugin);
+	}
+	
+	return new $class_name($variable);
+}
+ 
+?>

+ 0 - 138
pquery.php

@@ -1,138 +0,0 @@
-<?php
-/**
- * Base for pQuery package.
- * 
- * @package pQuery
- */
-
-namespace pQuery;
-
-/**
- * Shortcut constructor for {@link pQuery}.
- * 
- * @returns pQuery A new pQuery instance.
- */
-function _() {
-	$args = func_get_args();
-	
-	return call_user_func_array('pQuery::__construct', $args);
-}
-
-/**
- * Indicates whether the framework is in debug mode.
- * 
- * @var bool
- */
-defined('DEBUG') || define('DEBUG', true);
-
-/**
- * Common utility class.
- */
-class pQuery {
-	/**
-	 * @see pQueryExtension::REQUIRED_PHP_VERSION
-	 */
-	const REQUIRED_PHP_VERSION = '5.3';
-	
-	/**
-	 * A list of all plugins currently included.
-	 * 
-	 * @var array
-	 */
-	static $plugins = array();
-	
-	/**
-	 * The current variable.
-	 * 
-	 * @var mixed
-	 */
-	var $variable;
-	
-	/**
-	 * Extend pQuery with a plugin.
-	 * 
-	 * @param mixed $variable The variable to parse.
-	 * @see $plugins
-	 */
-	static function extend($class_name, $alias=null) {
-		if( !class_exists($class_name) )
-			return self::error('Class "%s" does not exist.', $class_name);
-		
-		if( !($class_name instanceof pQueryExtension) )
-			return self::error('Class "%s" does not implement pQueryExtension.', $class_name);
-		
-		if( $class_name )
-			return self::error('Class "%s" does not implement pQueryExtension.', $class_name);
-		
-		self::$plugins[$alias === null ? $class_name : $alias ] = $class_name;
-	}
-	
-	/**
-	 * Display an error message if in {@link DEBUG} mode.
-	 * 
-	 * The optional arguments are passed to {@link printf}, along with $error.
-	 * 
-	 * @param string $error The error message to display.
-	 */
-	static function error($error/*, $arg1, $arg2...*/) {
-		$args = func_get_args();
-		
-		if( DEBUG ) {
-			call_user_func_array('printf', $args);
-			echo debug_backtrace();
-		}
-	}
-	
-	/**
-	 * Constructor.
-	 * 
-	 * @param mixed $variable The variable to use an utility on.
-	 * @param string $plugin The name of an utility plugin to use (optional).
-	 */
-	function __construct($variable, $plugin=null) {
-		if( $plugin !== null ) {
-			if( isset($plugins[$plugin]) ) {
-				$class_name = $plugins[$plugin];
-				
-				return new $class_name($variable);
-			} else if( DEBUG ) {
-				self::error('Plugin "%s" does not exist.', $plugin);
-			}
-		}
-		
-		$this->parse_variable($variable);
-	}
-	
-	/**
-	 * Parse the type of the given variable, and convert it if needed.
-	 * 
-	 * @param mixed $variable The variable to parse.
-	 * @todo Type and conversion
-	 */
-	function parse_variable($variable) {
-		
-		
-		$this->variable = $variable;
-	}
-}
-
-/**
- * Interface used for extending the jQuery class.
- */
-interface pQueryExtension {
-	/**
-	 * The minimum php version required to use the package.
-	 * 
-	 * @var string
-	 */
-	const REQUIRED_PHP_VERSION;
-	
-	/**
-	 * Constructor.
-	 * 
-	 * @param mixed $variable The variable to use an utility on.
-	 */
-	function __construct($variable);
-}
- 
-?>

+ 27 - 0
sql.php

@@ -0,0 +1,27 @@
+<?php
+
+/**
+ * @todo Documentation
+ */
+class pQuerySql extends pQuery implements pQueryExtension {
+	static $accepts = array('string' => 'parse_query', 'resource');
+	
+	function parse_query($query) {
+		$this->query = $query;
+	}
+}
+
+/**
+ * Shortcut constructor for {@link pQuerySql}.
+ * 
+ * @returns pQuerySql A new pQuerySql instance.
+ * @see pQuerySql::__construct
+ */
+function _s($query) {
+	return _p($query, 'sql');
+}
+
+pQuerySql::extend('pQuerySql', 'sql');
+debug(pQuery::$plugins);
+
+?>