All pastes #838081 Raw Edit

Unnamed

public text v1 · immutable
#838081 ·published 2007-12-30 03:00 UTC
rendered paste body
<?php

class px_var {
	var $value;
	var $default_value;
	var $valid_values;

	function px_var( $value, $default_value = false ) {
		$this->value		= $value;
		$this->default_value	= $default_value;
		$this->valid_values	= false;
	}

	function integer() {
		$intval = $this->value;
		settype( $intval, 'integer' );

		if ( is_array( $this->valid_values ) ) {
			for ( $i = 0; $i < count( $this->valid_values ); $i++ ) {
				if ( $this->valid_values[ $i ] == $intval )
					break;
			}

			if ( $i == count( $this->valid_values ) )
				$intval = $this->default_value ? $this->default_value : $intval;
		}

		return $intval;
	}

	function string() {
		$strval = $this->value;
		settype( $intval, 'string' );

		if ( is_array( $this->valid_values ) ) {
			for ( $i = 0; $i < count( $this->valid_values ); $i++ ) {
				if ( $this->valid_values[ $i ] == $strval )
					break;
			}

			if ( $i == count( $this->valid_values ) )
				$strval = $this->default_value ? $this->default_value : $strval;
		}

		return $strval;
	}

	function valid_values( $values ) {
		if ( ! is_array( $values ) ) {
			die( 'valid_values: parameter is not an array!' );
		}

		$this->valid_values = $values;
	}
};


/*
Die Post Variablen sind nur zum testen
*/
$_POST[ 'user_id' ]	= '42d';
$_POST[ 'user_limit' ]	= '1';
$_POST[ 'user_game1' ]	= 'rtcw';

$user_id	= new px_var( $_POST[ 'user_id' ] );
$user_limit	= new px_var( $_POST[ 'user_limit' ], 20 );
$user_game1	= new px_var( $_POST[ 'user_game1' ], 'et' );

$user_limit->valid_values( Array( '10', '20', '30', '40', '50' ) );
$user_game1->valid_values( Array( 'et', 'rtcw', 'cod' ) );

echo $user_id->integer().'<br />';
echo $user_limit->integer().'<br />';
echo $user_game1->string().'<br />';

?>