Changeset 358

Show
Ignore:
Timestamp:
03/10/07 00:13:56 (2 years ago)
Author:
mikey
Message:

added optional param $default to get() and getConfig()
removed tabs

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/src/main/php/net/stubbles/util/stubRegistry.php

    r152 r358  
    3434    private final function __construct() 
    3535    { 
    36        // nothing to do here 
     36        // nothing to do here 
    3737    } 
    3838 
     
    4343     * @param  mixed   $value  value to store 
    4444     */ 
    45    public static function set($key, $value) 
    46    
    47        self::$registry[$key] = $value; 
    48    
     45    public static function set($key, $value) 
     46   
     47        self::$registry[$key] = $value; 
     48   
    4949 
    5050    /** 
     
    5454     * @param  mixed   $value  value to store 
    5555     */ 
    56    public static function setConfig($key, $value) 
    57    
    58        self::$config[$key] = $value; 
    59    
     56    public static function setConfig($key, $value) 
     57   
     58        self::$config[$key] = $value; 
     59   
    6060 
    6161    /** 
     
    6565     * @return  bool    true if a value exists under the given key, else false 
    6666     */ 
    67    public static function has($key) 
    68    
    69        return isset(self::$registry[$key]); 
    70    
     67    public static function has($key) 
     68   
     69        return isset(self::$registry[$key]); 
     70   
    7171 
    7272    /** 
    7373     * return the value stored under the given key, if key does not exist it returns null 
    7474     * 
    75      * @param   string  $key  key where the value is stored under 
     75     * @param   string  $key      key where the value is stored under 
     76     * @param   mixed   $default  optional  default value to return if $key not set 
    7677     * @return  mixed 
    7778     */ 
    78    public static function get($key
    79    
    80        if (isset(self::$registry[$key]) == true) { 
    81            return self::$registry[$key]; 
    82        } 
    83         
    84        return null
    85    
    86      
     79    public static function get($key, $default = null
     80   
     81        if (isset(self::$registry[$key]) == true) { 
     82            return self::$registry[$key]; 
     83        } 
     84         
     85        return $default
     86   
     87 
    8788    /** 
    8889     * check if a value exists under the given key 
     
    9192     * @return  bool    true if a value exists under the given key for the given module, else false 
    9293     */ 
    93    public static function hasConfig($key) 
    94    
    95        if (isset(self::$config[$key]) == true) { 
    96            return true; 
    97        } 
    98         
    99        return false; 
    100    
     94    public static function hasConfig($key) 
     95   
     96        if (isset(self::$config[$key]) == true) { 
     97            return true; 
     98        } 
     99         
     100        return false; 
     101   
    101102 
    102103    /** 
    103104     * return the value stored under the given key, if key in module does not exist it returns null 
    104105     * 
    105      * @param   string  $key  key where the value is stored under 
     106     * @param   string  $key      key where the value is stored under 
     107     * @param   mixed   $default  optional  default value to return if $key not set 
    106108     * @return  mixed 
    107109     */ 
    108    public static function getConfig($key
    109    
    110        if (isset(self::$config[$key]) == true) { 
    111            return self::$config[$key]; 
    112        } 
    113  
    114        return null
    115    
     110    public static function getConfig($key, $default = null
     111   
     112        if (isset(self::$config[$key]) == true) { 
     113            return self::$config[$key]; 
     114        } 
     115         
     116        return $default
     117   
    116118} 
    117119?> 
  • trunk/src/test/php/net/stubbles/util/stubRegistryTestCase.php

    r152 r358  
    1919     * assure that values are returned as they are put into the registry 
    2020     */ 
    21     public function testNormal() 
    22     { 
    23         $this->assertNull(stubRegistry::get('test')); 
    24         $this->assertFalse(stubRegistry::has('test')); 
    25         stubRegistry::set('test', 'test'); 
    26         $this->assertTrue(stubRegistry::has('test')); 
    27         $this->assertEqual(stubRegistry::get('test'), 'test'); 
    28         $test = new stdClass(); 
    29         stubRegistry::set('test', $test); 
    30         $this->assertTrue(stubRegistry::has('test')); 
    31         $regTest = stubRegistry::get('test'); 
    32         $this->assertReference($regTest, $test); 
    33     } 
     21    public function testNormal() 
     22    { 
     23        $this->assertNull(stubRegistry::get('test')); 
     24        $this->assertEqual(stubRegistry::get('test', 'foo'), 'foo'); 
     25        $this->assertFalse(stubRegistry::has('test')); 
     26        stubRegistry::set('test', 'test'); 
     27        $this->assertTrue(stubRegistry::has('test')); 
     28        $this->assertEqual(stubRegistry::get('test'), 'test'); 
     29        $test = new stdClass(); 
     30        stubRegistry::set('test', $test); 
     31        $this->assertTrue(stubRegistry::has('test')); 
     32        $regTest = stubRegistry::get('test'); 
     33        $this->assertReference($regTest, $test); 
     34    } 
    3435 
    3536    /** 
    3637     * assure that values are returned as they are put into the config registry 
    3738     */ 
    38     public function testConfig() 
    39     { 
    40         $this->assertNull(stubRegistry::getConfig('test')); 
    41         $this->assertFalse(stubRegistry::hasConfig('test')); 
    42         stubRegistry::setConfig('test', 'test'); 
    43         $this->assertTrue(stubRegistry::hasConfig('test')); 
    44         $this->assertEqual(stubRegistry::getConfig('test'), 'test'); 
    45         $test = new stdClass(); 
    46         stubRegistry::setConfig('test', $test); 
    47         $this->assertTrue(stubRegistry::hasConfig('test')); 
    48         $regTest = stubRegistry::getConfig('test'); 
    49         $this->assertReference($regTest, $test); 
    50     } 
     39    public function testConfig() 
     40    { 
     41        $this->assertNull(stubRegistry::getConfig('test')); 
     42        $this->assertEqual(stubRegistry::getConfig('test', 'foo'), 'foo'); 
     43        $this->assertFalse(stubRegistry::hasConfig('test')); 
     44        stubRegistry::setConfig('test', 'test'); 
     45        $this->assertTrue(stubRegistry::hasConfig('test')); 
     46        $this->assertEqual(stubRegistry::getConfig('test'), 'test'); 
     47        $test = new stdClass(); 
     48        stubRegistry::setConfig('test', $test); 
     49        $this->assertTrue(stubRegistry::hasConfig('test')); 
     50        $regTest = stubRegistry::getConfig('test'); 
     51        $this->assertReference($regTest, $test); 
     52    } 
    5153} 
    5254?>