arguments = $args; $obj->set_variable($variable); return $obj; } /** * 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) { $this->variable = $variable; if( $force ) return; $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 || $this->variable = $result; } else if( !in_array($type, $accepts) ) { return self::error('Variable type "%s" is not accepted by class "%s".', $type, $class_name); } } /** * Try to load the file containing the utility class for a specific variable type. * * @param mixed $type the variable type of the class to load. */ static function load_plugin($type) { $file = PQUERY_ROOT.$type.'.php'; if( !file_exists($file) ) return false; include_once $file; return true; } /** * Include the nescessary files for the given plugins. */ static function require_plugins(/* $plugin1 [ , $plugin2, ... ] */) { $plugins = func_get_args(); foreach( $plugins as $plugin ) { $path = PQUERY_ROOT.$plugin.'.php'; if( !file_exists($path) ) { return self::error('Required plugin "%s" could not be located (looked in "%s").', $plugin, $path); } include_once $path; } } } /** * Interface used for extending the jQuery class. */ interface pQueryExtension { /** * Constructor. * * @param mixed $variable The variable to use an utility on. */ //function __construct(); } /** * 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_plugin($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); } ?>