Changeset 753

Show
Ignore:
Timestamp:
07/02/07 22:59:00 (1 year ago)
Author:
mikey
Message:

added stubRequest::addValueError(stubRequestValueError $valueError, $valueName, $source = self::SOURCE_PARAM) (closes ticket #70)

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/src/main/php/net/stubbles/ipo/request/stubAbstractRequest.php

    r427 r753  
    6363     */ 
    6464    protected $isCancelled     = false; 
    65      
     65 
    6666    /** 
    6767     * constructor 
     
    7171        $this->doConstuct(); 
    7272    } 
    73      
     73 
    7474    /** 
    7575     * template method for child classes to do the real construction 
     
    8686        throw new stubException('Cloning of request is not allowed!'); 
    8787    } 
    88      
     88 
    8989    /** 
    9090     * checks whether a request value is set or not 
     
    9999        return isset($data[$valueName]); 
    100100    } 
    101      
     101 
     102    /** 
     103     * add a value error for a request value 
     104     * 
     105     * @param  stubRequestValueError  $valueError 
     106     * @param  string                 $valueName 
     107     * @param  int                    $source 
     108     */ 
     109    public function addValueError(stubRequestValueError $valueError, $valueName, $source = self::SOURCE_PARAM) 
     110    { 
     111        $error =& $this->getErrors($source); 
     112        if (isset($error[$valueName]) == false) { 
     113            $error[$valueName] = array($valueError); 
     114        } else { 
     115            $ids = array(); 
     116            foreach ($error[$valueName] as $errorValue) { 
     117                $ids[] = $errorValue->getId(); 
     118            } 
     119             
     120            if (in_array($valueError->getId(), $ids) == false) { 
     121                $error[$valueName][] = $valueError; 
     122            } 
     123        } 
     124    } 
     125 
    102126    /** 
    103127     * checks whether a request value has an error after a filter was applied 
     
    112136        return isset($error[$valueName]); 
    113137    } 
    114      
     138 
    115139    /** 
    116140     * returns a list of errors for given request value 
     
    129153        return array(); 
    130154    } 
    131      
     155 
    132156    /** 
    133157     * checks whether there are any value errors 
     
    140164        return (count($this->getErrors($source)) > 0); 
    141165    } 
    142      
     166 
    143167    /** 
    144168     * returns a list of all request value names with their errors 
     
    151175        return $this->getErrors($source); 
    152176    } 
    153      
     177 
    154178    /** 
    155179     * returns the array with errors from requested source 
     
    174198        } 
    175199    } 
    176      
     200 
    177201    /** 
    178202     * cancels the request, e.g. if it was detected that it is invalid 
     
    197221        $dispatcher->triggerEvent($event, true); 
    198222    } 
    199      
     223 
    200224    /** 
    201225     * checks whether the request has been cancelled or not 
     
    207231        return $this->isCancelled; 
    208232    } 
    209      
     233 
    210234    /** 
    211235     * checks whether raw data is valid or not 
     
    218242        return $validator->validate($this->getRawData()); 
    219243    } 
    220      
     244 
    221245    /** 
    222246     * returns the validated raw data 
     
    236260        return null; 
    237261    } 
    238      
     262 
    239263    /** 
    240264     * returns the raw data filtered 
     
    257281        return $filter->execute($rawData); 
    258282    } 
    259      
     283 
    260284    /** 
    261285     * returns the raw data 
     
    264288     */ 
    265289    protected abstract function getRawData(); 
    266      
     290 
    267291    /** 
    268292     * checks whether a request value is valid or not 
     
    282306        return false; 
    283307    } 
    284      
     308 
    285309    /** 
    286310     * returns the validated request value 
     
    326350            $value = $filter->execute($data[$valueName]); 
    327351        } catch (stubFilterException $fe) { 
    328             $error =& $this->getErrors($source); 
    329             if (isset($error[$valueName]) == false) { 
    330                 $error[$valueName] = array($fe->getError()); 
    331             } else { 
    332                 $ids = array(); 
    333                 foreach ($error[$valueName] as $errorValue) { 
    334                     $ids[] = $errorValue->getId(); 
    335                 } 
    336                  
    337                 if (in_array($fe->getError()->getId(), $ids) == false) { 
    338                     $error[$valueName][] = $fe->getError(); 
    339                 } 
    340             } 
    341              
     352            $this->addValueError($fe->getError(), $valueName, $source); 
    342353            return null; 
    343354        } 
     
    345356        return $value; 
    346357    } 
    347      
     358 
    348359    /** 
    349360     * return an array of all keys registered in this request 
     
    355366        return array_keys($this->getValues($source)); 
    356367    } 
    357      
     368 
    358369    /** 
    359370     * returns the array with data from requested source 
  • trunk/src/main/php/net/stubbles/ipo/request/stubRequest.php

    r425 r753  
    99stubClassLoader::load('net.stubbles.events.stubEventDispatcher', 
    1010                      'net.stubbles.util.validators.stubValidator', 
    11                       'net.stubbles.ipo.request.filters.stubFilter' 
     11                      'net.stubbles.ipo.request.filters.stubFilter', 
     12                      'net.stubbles.ipo.request.stubRequestValueError' 
    1213); 
    1314/** 
     
    4041     */ 
    4142    const SOURCE_PARAM    = 4; 
    42      
     43 
    4344    /** 
    4445     * checks whether a request value is set or not 
     
    4950     */ 
    5051    public function hasValue($valueName, $source = self::SOURCE_PARAM); 
    51      
     52 
     53    /** 
     54     * add a value error for a request value 
     55     * 
     56     * @param  stubRequestValueError  $valueError 
     57     * @param  string                 $valueName 
     58     * @param  int                    $source 
     59     */ 
     60    public function addValueError(stubRequestValueError $valueError, $valueName, $source = self::SOURCE_PARAM); 
     61 
    5262    /** 
    5363     * checks whether a request value has an error after a filter was applied 
     
    5868     */ 
    5969    public function hasValueError($valueName, $source = self::SOURCE_PARAM); 
    60      
     70 
    6171    /** 
    6272     * returns a list of errors for given request value 
     
    6777     */ 
    6878    public function getValueError($valueName, $source = self::SOURCE_PARAM); 
    69      
     79 
    7080    /** 
    7181     * checks whether there are any value errors 
     
    7585     */ 
    7686    public function hasValueErrors($source = self::SOURCE_PARAM); 
    77      
     87 
    7888    /** 
    7989     * returns a list of all request value names with their errors 
     
    8393     */ 
    8494    public function getValueErrors($source = self::SOURCE_PARAM); 
    85      
     95 
    8696    /** 
    8797     * cancels the request, e.g. if it was detected that it is invalid 
     
    92102     */ 
    93103    public function cancel(stubEventDispatcher $dispatcher = null); 
    94      
     104 
    95105    /** 
    96106     * checks whether the request has been cancelled or not 
     
    99109     */ 
    100110    public function isCancelled(); 
    101      
     111 
    102112    /** 
    103113     * returns the request method 
     
    106116     */ 
    107117    public function getMethod(); 
    108      
     118 
    109119    /** 
    110120     * returns the uri of the request 
     
    113123     */ 
    114124    public function getURI(); 
    115      
     125 
    116126    /** 
    117127     * checks whether raw data is valid or not 
     
    121131     */ 
    122132    public function validateRawData(stubValidator $validator); 
    123      
     133 
    124134    /** 
    125135     * returns the validated raw data 
     
    131141     */ 
    132142    public function getValidatedRawData(stubValidator $validator); 
    133      
     143 
    134144    /** 
    135145     * returns the raw data filtered 
     
    140150     */ 
    141151    public function getFilteredRawData(stubFilter $filter); 
    142      
     152 
    143153    /** 
    144154     * checks whether a request value is valid or nor 
     
    150160     */ 
    151161    public function validateValue(stubValidator $validator, $valueName, $source = self::SOURCE_PARAM); 
    152      
     162 
    153163    /** 
    154164     * returns the validated request value 
     
    173183     */ 
    174184    public function getFilteredValue(stubFilter $filter, $valueName, $source = self::SOURCE_PARAM); 
    175      
     185 
    176186    /** 
    177187     * return an array of all keys registered in this request 
  • trunk/src/main/php/net/stubbles/ipo/request/stubRequestPrefixDecorator.php

    r425 r753  
    4444     */ 
    4545    protected $sources; 
    46      
     46 
    4747    /** 
    4848     * constructor 
     
    5858        $this->sources = $sources; 
    5959    } 
    60      
     60 
    6161    /** 
    6262     * sets the prefix to another value 
     
    6868        $this->prefix = $prefix; 
    6969    } 
    70      
     70 
    7171    /** 
    7272     * checks whether a request value is set or not 
     
    8484        return $this->request->hasValue($valueName, $source); 
    8585    } 
    86      
     86 
     87    /** 
     88     * add a value error for a request value 
     89     * 
     90     * @param  stubRequestValueError  $valueError 
     91     * @param  string                 $valueName 
     92     * @param  int                    $source 
     93     */ 
     94    public function addValueError(stubRequestValueError $valueError, $valueName, $source = self::SOURCE_PARAM) 
     95    { 
     96        if ($this->applyPrefix($source) == true) { 
     97            $valueName = $this->prefix . '_' . $valueName; 
     98        } 
     99         
     100        $this->request->addValueError($valueError, $valueName, $source); 
     101    } 
     102 
    87103    /** 
    88104     * checks whether a request value has an error after a filter was applied 
     
    163179        $this->request->cancel($dispatcher); 
    164180    } 
    165      
     181 
    166182    /** 
    167183     * checks whether the request has been cancelled or not 
     
    173189        return $this->request->isCancelled(); 
    174190    } 
    175      
     191 
    176192    /** 
    177193     * returns the request method 
     
    183199        return $this->request->getMethod(); 
    184200    } 
    185      
     201 
    186202    /** 
    187203     * returns the uri of the request 
     
    193209        return $this->request->getURI(); 
    194210    } 
    195      
     211 
    196212    /** 
    197213     * checks whether raw data is valid or not 
     
    204220        return $this->request->validateRawData($validator); 
    205221    } 
    206      
     222 
    207223    /** 
    208224     * returns the validated raw data 
     
    217233        return $this->request->getValidatedRawData($validator); 
    218234    } 
    219      
     235 
    220236    /** 
    221237     * returns the raw data filtered 
     
    229245        return $this->request->getFilteredRawData($filter); 
    230246    } 
    231      
     247 
    232248    /** 
    233249     * checks whether a request value is valid or nor 
     
    283299        return $this->request->getFilteredValue($filter, $valueName, $source); 
    284300    } 
    285      
     301 
    286302    /** 
    287303     * return an array of all keys registered in this request 
     
    306322        return $returnedValueKeys; 
    307323    } 
    308      
     324 
    309325    /** 
    310326     * check whether the prefix has to be applied for requested source 
  • trunk/src/test/php/net/stubbles/ipo/request/stubAbstractRequestTestCase.php

    r425 r753  
    11<?php 
    22/** 
    3  * Tests for ipo.request.stubAbstractRequest 
     3 * Tests for net.stubbles.ipo.request.stubAbstractRequest 
    44 * 
    55 * @author      Frank Kleine <mikey@stubbles.net> 
    66 * @package     stubbles 
    7  * @subpackage  ipo_test 
     7 * @subpackage  ipo_request_test 
    88 */ 
    99stubClassLoader::load('net.stubbles.ipo.request.stubAbstractRequest', 
     
    5050        return $this->_rawData; 
    5151    } 
    52      
    53     public function setValueError($valueName, stubRequestValueError $error) 
    54     { 
    55         $this->paramErrors[$valueName] = array($error); 
    56     } 
    57      
    58     public function setHeaderError($headerName, stubRequestValueError $error) 
    59     { 
    60         $this->headerErrors[$headerName] = array($error); 
    61     } 
    62      
    63     public function setCookieError($cookieName, stubRequestValueError $error) 
    64     { 
    65         $this->cookieErrors[$cookieName] = array($error); 
    66     } 
    6752} 
    6853/** 
    69  * Tests for ipo.request.stubAbstractRequest 
     54 * Tests for net.stubbles.ipo.request.stubAbstractRequest 
    7055 * 
    7156 * @package     stubbles 
    72  * @subpackage  ipo_test 
     57 * @subpackage  ipo_request_test 
    7358 */ 
    7459class stubAbstractRequestTestCase extends UnitTestCase 
     
    8065     */ 
    8166    protected $request; 
    82      
     67 
    8368    /** 
    8469     * set up test environment 
     
    164149        $this->assertEqual($this->request->getFilteredValue($mockFilter, 'foo', 'dummy'), 'bam'); 
    165150    } 
    166      
     151 
    167152    /** 
    168153     * test that headers are handles as expected 
     
    198183        $this->assertEqual($this->request->getFilteredValue($mockFilter, 'bar', stubRequest::SOURCE_HEADER), 'bam'); 
    199184    } 
    200      
     185 
    201186    /** 
    202187     * test that headers are handles as expected 
     
    232217        $this->assertEqual($this->request->getFilteredValue($mockFilter, 'baz', stubRequest::SOURCE_COOKIE), 'bam'); 
    233218    } 
    234      
     219 
    235220    /** 
    236221     * assure that cancelling the request works as expected 
     
    248233        stubEventDispatcher::destroyInstance('__test'); 
    249234    } 
    250      
     235 
    251236    /** 
    252237     * assure that value error handling works correct 
     
    268253         
    269254        $errorValue = new stubRequestValueError('bar', array()); 
    270         $this->request->setValueError('foo', $errorValue); 
     255        $this->request->addValueError($errorValue, 'foo'); 
    271256        $this->assertTrue($this->request->hasValueError('foo')); 
    272257        $this->assertEqual($this->request->getValueError('foo'), array($errorValue)); 
     
    284269        $this->assertEqual($this->request->getValueErrors('foo', 'dummy'), array('foo' => array($errorValue))); 
    285270    } 
    286      
     271 
    287272    /** 
    288273     * assure that value error handling works correct 
     
    296281         
    297282        $errorValue = new stubRequestValueError('bar', array()); 
    298         $this->request->setHeaderError('foo', $errorValue); 
     283        $this->request->addValueError($errorValue, 'foo', stubRequest::SOURCE_HEADER); 
    299284        $this->assertTrue($this->request->hasValueError('foo', stubRequest::SOURCE_HEADER)); 
    300285        $this->assertEqual($this->request->getValueError('foo', stubRequest::SOURCE_HEADER), array($errorValue)); 
     
    302287        $this->assertEqual($this->request->getValueErrors(stubRequest::SOURCE_HEADER), array('foo' => array($errorValue))); 
    303288    } 
    304      
     289 
    305290    /** 
    306291     * assure that value error handling works correct 
     
    314299         
    315300        $errorValue = new stubRequestValueError('bar', array()); 
    316         $this->request->setCookieError('foo', $errorValue); 
     301        $this->request->addValueError($errorValue, 'foo', stubRequest::SOURCE_COOKIE); 
    317302        $this->assertTrue($this->request->hasValueError('foo', stubRequest::SOURCE_COOKIE)); 
    318303        $this->assertEqual($this->request->getValueError('foo', stubRequest::SOURCE_COOKIE), array($errorValue)); 
     
    320305        $this->assertEqual($this->request->getValueErrors(stubRequest::SOURCE_COOKIE), array('foo' => array($errorValue))); 
    321306    } 
    322      
     307 
    323308    /** 
    324309     * assure that the same error occurs only once in list of errors for a value 
     
    327312    { 
    328313        $errorValue = new stubRequestValueError('foo', array()); 
    329         $this->request->setValueError('foo', $errorValue); 
     314        $this->request->addValueError($errorValue, 'foo'); 
    330315        $this->assertEqual($this->request->getValueError('foo'), array($errorValue)); 
    331316        $this->request->getFilteredValue(new stubTestExceptionFilter(), 'foo'); 
    332317        $this->assertEqual($this->request->getValueError('foo'), array($errorValue)); 
    333318    } 
    334      
     319 
    335320    /** 
    336321     * assure that value keys are delivered correct 
     
    343328        $this->assertEqual($this->request->getValueKeys(stubRequest::SOURCE_COOKIE), array('baz')); 
    344329    } 
    345      
     330 
    346331    /** 
    347332     * assure that raw data is handles correct 
     
    355340        $this->assertFalse($this->request->validateRawData($mockValidator)); 
    356341    } 
    357      
     342 
    358343    /** 
    359344     * assure that raw data is handles correct 
     
    367352        $this->assertNull($this->request->getValidatedRawData($mockValidator)); 
    368353    } 
    369      
     354 
    370355    /** 
    371356     * assure that raw data is handles correct 
  • trunk/src/test/php/net/stubbles/ipo/request/stubRequestPrefixDecoratorTestCase.php

    r425 r753  
    3131     */ 
    3232    protected $mockRequest; 
    33      
     33 
    3434    /** 
    3535     * set up test environment 
     
    4040        $this->request     = new stubRequestPrefixDecorator($this->mockRequest, 'test'); 
    4141    } 
    42      
     42 
    4343    /** 
    4444     * test that values are prefixed as expected 
     
    6060        $this->request->getFilteredValue($mockFilter, 'foo'); 
    6161    } 
    62      
     62 
    6363    /** 
    6464     * test that values are prefixed as expected 
     
    8181        $this->request->getFilteredValue($mockFilter, 'foo'); 
    8282    } 
    83      
     83 
    8484    /** 
    8585     * test that headers are handles as expected 
     
    101101        $this->request->getFilteredValue($mockFilter, 'foo', stubRequest::SOURCE_HEADER); 
    102102    } 
    103      
     103 
    104104    /** 
    105105     * test that headers are handles as expected 
     
    121121        $this->request->getFilteredValue($mockFilter, 'foo', stubRequest::SOURCE_COOKIE); 
    122122    } 
    123      
     123 
    124124    /** 
    125125     * assure that value error handling works correct 
     
    127127    public function testValueError() 
    128128    { 
     129        $errorValue = new stubRequestValueError('bar', array()); 
     130        $this->mockRequest->expect('addValueError', array($errorValue, 'test_foo', stubRequest::SOURCE_PARAM)); 
     131        $this->request->addValueError($errorValue, 'foo'); 
     132         
    129133        $this->mockRequest->expect('hasValueError', array('test_foo', stubRequest::SOURCE_PARAM)); 
    130134        $this->request->hasValueError('foo'); 
     
    139143        $this->assertEqual($this->request->getValueErrors(), array('foo' => array())); 
    140144    } 
    141      
     145 
    142146    /** 
    143147     * assure that value error handling works correct 
     
    145149    public function testHeaderError() 
    146150    { 
    147         $this->mockRequest->expect('getValueError', array('foo', stubRequest::SOURCE_HEADER)); 
     151        $errorValue = new stubRequestValueError('bar', array()); 
     152        $this->mockRequest->expect('addValueError', array($errorValue, 'foo', stubRequest::SOURCE_HEADER)); 
     153        $this->request->addValueError($errorValue, 'foo', stubRequest::SOURCE_HEADER); 
     154         
     155        $this->mockRequest->expect('hasValueError', array('foo', stubRequest::SOURCE_HEADER)); 
    148156        $this->request->hasValueError('foo', stubRequest::SOURCE_HEADER); 
    149157         
     
    158166        $this->assertEqual($this->request->getValueErrors(stubRequest::SOURCE_HEADER), array('test_foo' => array(), 'bar' => array())); 
    159167    } 
    160      
     168 
    161169    /** 
    162170     * assure that value error handling works correct 
     
    164172    public function testCookieError() 
    165173    { 
    166         $this->mockRequest->expect('getValueError', array('foo', stubRequest::SOURCE_COOKIE)); 
     174        $errorValue = new stubRequestValueError('bar', array()); 
     175        $this->mockRequest->expect('addValueError', array($errorValue, 'foo', stubRequest::SOURCE_COOKIE)); 
     176        $this->request->addValueError($errorValue, 'foo', stubRequest::SOURCE_COOKIE); 
     177         
     178        $this->mockRequest->expect('hasValueError', array('foo', stubRequest::SOURCE_COOKIE)); 
    167179        $this->request->hasValueError('foo', stubRequest::SOURCE_COOKIE); 
    168180         
     
    177189        $this->assertEqual($this->request->getValueErrors(stubRequest::SOURCE_COOKIE), array('test_foo' => array(), 'bar' => array())); 
    178190    } 
    179      
     191 
    180192    /** 
    181193     * assure that the cancel methods are called 
     
    189201        $this->assertTrue($this->request->isCancelled()); 
    190202    } 
    191      
     203 
    192204    /** 
    193205     * assure that getMethod() is called 
     
    199211        $this->assertEqual($this->request->getMethod(), 'test'); 
    200212    } 
    201      
     213 
    202214    /** 
    203215     * assure that getURI() is called 
     
    209221        $this->assertEqual($this->request->getURI(), 'test'); 
    210222    } 
    211      
     223 
    212224    /** 
    213225     * assure that raw data is handles correct 
     
    220232        $this->assertFalse($this->request->validateRawData($mockValidator)); 
    221233    } 
    222      
     234 
    223235    /** 
    224236     * assure that raw data is handles correct 
     
    231243        $this->assertEqual($this->request->getValidatedRawData($mockValidator), 'foo'); 
    232244    } 
    233      
     245 
    234246    /** 
    235247     * assure that raw data is handles correct 
     
    242254        $this->assertEqual($this->request->getFilteredRawData($mockFilter), 'foo'); 
    243255    } 
    244      
     256 
    245257    /** 
    246258     * assure that value keys are delivered correct