Changeset 1664

Show
Ignore:
Timestamp:
01/02/2008 04:32:55 PM (9 months ago)
Author:
gregmac
Message:

Add argument(), argument_array(), total_arguments() methods to URI

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • trunk/system/libraries/URI.php

    r1539 r1664  
    6969                return isset(self::$rsegments[$index]) ? self::$rsegments[$index] : $default; 
    7070        } 
     71         
     72        /** 
     73         * Method: argument 
     74         *  Retrieve a specific URI argument. This is the part of the segments that does not indicate controller 
     75         *  or method 
     76         * 
     77         * Parameters: 
     78         *  index   - argument number or label 
     79         *  default - default value returned if segment does not exist 
     80         * 
     81         * Returns: 
     82         *   Value of segment 
     83         */ 
     84        public function argument($index = 1, $default = FALSE) 
     85        { 
     86                if (is_string($index)) 
     87                { 
     88                        if (($key = array_search($index, self::$arguments)) === FALSE) 
     89                                return $default; 
     90 
     91                        $index = $key + 2; 
     92                } 
     93 
     94                $index = (int) $index - 1; 
     95 
     96                return isset(self::$arguments[$index]) ? self::$arguments[$index] : $default; 
     97        } 
    7198 
    7299        /** 
     
    129156                return $segment_array_assoc; 
    130157        } 
    131  
     158         
     159        /** 
     160         * Method: argument_array 
     161         *  Returns an array containing all the URI arguments. 
     162         * 
     163         * Parameters: 
     164         *  offset      - segment offset 
     165         *  associative - return an associative array 
     166         * 
     167         * Returns: 
     168         *   Array of URI segment arguments 
     169         */ 
     170        public function argument_array($offset = 0, $associative = FALSE) 
     171        { 
     172                $argument_array = self::$arguments; 
     173                array_unshift($argument_array, 0); 
     174                $argument_array = array_slice($argument_array, $offset + 1, $this->total_arguments(), TRUE); 
     175 
     176                if ( ! $associative) 
     177                        return $argument_array; 
     178 
     179                $argument_array_assoc = array(); 
     180 
     181                foreach (array_chunk($argument_array, 2) as $pair) 
     182                { 
     183                        $argument_array_assoc[$pair[0]] = isset($pair[1]) ? $pair[1] : ''; 
     184                } 
     185 
     186                return $argument_array_assoc; 
     187        } 
     188         
    132189        /** 
    133190         * Method: string 
     
    176233        { 
    177234                return count(self::$rsegments); 
     235        } 
     236         
     237        /** 
     238         * Method: total_arguments 
     239         *  Returns the total number of URI arguments. 
     240         * 
     241         * Returns: 
     242         *   Total number of URI arguments 
     243         */ 
     244        public function total_arguments() 
     245        { 
     246                return count(self::$arguments); 
    178247        } 
    179248