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); } ?>