arguments = $args; $obj->set_variable(array_shift($args)); return $obj; } /** * Try to load an utility file. * * @param string $basename */ static function load_util($basename) { $path = PQUERY_ROOT.pQueryConfig::UTILS_FOLDER.$basename.'.php'; if( !file_exists($path) ) { return self::error('Utility "%s" could not be loaded (looked in "%s").', $basename, $path); } include_once $path; } /** * Try to load the file containing the utility class for a specific variable type. * * @param string $type the variable type of the class to load. */ static function load_plugin($type) { $path = PQUERY_ROOT.sprintf(self::PLUGIN_FILENAME_PATTERN, $type); if( !file_exists($path) ) return false; include_once $path; 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.sprintf(self::PLUGIN_FILENAME_PATTERN, $plugin); if( !file_exists($path) ) { return self::error('Required plugin "%s" could not be located (looked in "%s").', $plugin, $path); } include_once $path; } } /** * 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); } } /** * Getter for {@link variable}. * * @see variable_alias */ function __get($name) { $class_name = get_class($this); if( in_array($name, (array)$class_name::$variable_alias) ) return $this->variable; } /** * Setter for {@link variable}. * * @see variable_alias */ function __set($name, $value) { $class_name = get_class($this); if( in_array($name, (array)$class_name::$variable_alias) ) $this->variable = $value; } } /** * 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); } /** * Set an alias for the bas class consistent with plugin aliases. */ class_alias('pQuery', '__p'); ?>