     1: <?php
     2:
     3: class RESTian_Var {
     4:   /**
     5:    * @var string Actual name of URL query argument.  Defaults to same value as $this->name.
     6:    */
     7:   var $name;
     8:   /**
     9:    * @var string if specified, indicates what this param is used for. Basically it's a description.
    10:    */
    11:   var $requires = false;
    12:   /**
    13:    * @var bool|array List of other parameter names that make this parameter invalid when they appear.
    14:    * If in "name=value" format then only invalid when the parameter appears with the specified value.
    15:    */
    16:   var $not_vars = false;
    17:   /**
    18:    * @var bool|array List of options that are valid for this parameter.
    19:    */
    20:   var $options = false;
    21:   /**
    22:    * @var bool|string Specifies how this param is used; as part of the URL 'path', in the URL 'query', or 'both'.
    23:    */
    24:   var $usage = 'query';
    25:   /**
    26:    * @var bool|string Specifies the data type expected. Currently just 'string' or 'number'.
    27:    */
    28:   var $type = 'string';
    29:   /**
    30:    * @var bool|array List of Transforms that can be applied to value, i.e. fill[/] replaces whitespace with '/'
    31:    */
    32:   var $transforms = array();
    33:
    34:   /**
    35:    * @param string $var_name
    36:    * @param array $args
    37:    * @throws Exception
    38:    */
    39:   function __construct( $var_name, $args = array() ) {
    40:     /**
    41:      * If a string, could be 'foo' or 'var=foo' maybe
    42:      */
    43:     if ( is_string( $args ) ) {
    44:       /**
    45:        * If just 'foo' set $var = 'foo'
    46:        */
    47:       if ( true === strpos( $args, '=' ) ) {
    48:         $args = array( 'var' => $args );
    49:       } else {
    50:         /**
    51:          * If 'var=foo' turn into array( 'var'=>'foo' )
    52:          */
    53:         parse_str( $args, $args );
    54:       }
    55:     }
    56:
    57:     if ( ! is_object( $args ) && ! is_array( $args ) ) {
    58:       throw new Exception( 'Must pass a string, array or object for $args when creating a new ' . __CLASS__ . '.' );
    59:     }
    60:
    61:     $this->name = $var_name;
    62:     /**
    63:      * Copy properties in from $args, if they exist.
    64:      */
    65:     foreach( $args as $property => $value )
    66:       if ( property_exists(  $this, $property ) )
    67:         $this->$property = $value;
    68:
    69:     /*
    70:      * Convert strings to arrays.
    71:      */
    72:     if ( is_string( $this->options ) )
    73:       $this->options = RESTian::parse_string( $this->options );
    74:
    75:     if ( is_string( $this->not_vars ) )
    76:       $this->not_vars = RESTian::parse_string( $this->not_vars );
    77:
    78:     if ( is_string( $this->transforms ) )
    79:       $this->transforms = RESTian::parse_transforms( $this->transforms );
    80:
    81:   }
    82:   function apply_transforms( $value ) {
    83:     $new_value = $value;
    84:     foreach( $this->transforms as $transform => $data ) {
    85:       switch( $transform ) {
    86:         case 'fill':
    87:           $new_value = preg_replace( '#\s+#', $data, $new_value );
    88:           break;
    89:       }
    90:     }
    91:     return $new_value;
    92:   }
    93: }
    94: