Changeset 969

Show
Ignore:
Timestamp:
10/21/07 19:41:28 (1 year ago)
Author:
mikey
Message:

encoders may now be applied within the @StringFilter? and @TextFilter? annotation

Files:

Legend:

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

    r736 r969  
    1010                      'net.stubbles.ipo.request.filters.stubStringFilter', 
    1111                      'net.stubbles.ipo.request.broker.annotations.stubAbstractFilterAnnotation', 
     12                      'net.stubbles.php.string.stubStringEncoder', 
    1213                      'net.stubbles.util.validators.stubMaxLengthValidator', 
    1314                      'net.stubbles.util.validators.stubMinLengthValidator', 
     
    3940     * @var  string 
    4041     */ 
    41     protected $regex     = null; 
     42    protected $regex        = null; 
     43    /** 
     44     * the encoder class to be used 
     45     * 
     46     * @var  stubReflectionClass 
     47     */ 
     48    protected $encoderClass = null; 
     49    /** 
     50     * the encoding mode to be applied 
     51     * 
     52     * @var  int 
     53     */ 
     54    protected $encoderMode  = stubStringEncoder::MODE_DECODE; 
    4255 
    4356    /** 
    44      * checks if the regex property is set 
     57     * checks if the regex property is set and if the encoder mode is correct 
    4558     * 
    4659     * @throws  ReflectionException 
     
    5063        if (null === $this->regex) { 
    5164            throw new ReflectionException('Can not use ' . $this->getClassName() . ' without setting the regular expression via regex property.'); 
     65        } 
     66         
     67        if (null !== $this->encoderClass && in_array($this->encoderMode, array(stubStringEncoder::MODE_DECODE, stubStringEncoder::MODE_ENCODE)) === false) { 
     68            throw new ReflectionException('Can not use ' . $this->getClassName() . ' with wrong encoder mode ' . $this->encoderMode); 
    5269        } 
    5370    } 
     
    84101 
    85102    /** 
     103     * sets the encoder class to be used 
     104     * 
     105     * @param  stubReflectionClass  $encoder 
     106     */ 
     107    public function setEncoder(stubReflectionClass $encoderClass) 
     108    { 
     109        $this->encoderClass = $encoderClass; 
     110    } 
     111 
     112    /** 
     113     * sets the encoder mode to be used 
     114     * 
     115     * @param  int  $mode 
     116     */ 
     117    public function setEncoderMode($mode) 
     118    { 
     119        $this->encoderMode = $mode; 
     120    } 
     121 
     122    /** 
    86123     * returns the filter defined by the annotation 
    87124     * 
     
    100137        } 
    101138         
     139        if (null !== $this->encoderClass) { 
     140            $stringFilter->setEncoder($this->encoderClass->newInstance(), $this->encoderMode); 
     141        } 
     142         
    102143        return $stringFilter; 
    103144    } 
  • trunk/src/main/php/net/stubbles/ipo/request/broker/annotations/stubTextFilterAnnotation.php

    r731 r969  
    1010                      'net.stubbles.ipo.request.filters.stubTextFilter', 
    1111                      'net.stubbles.ipo.request.broker.annotations.stubAbstractFilterAnnotation', 
     12                      'net.stubbles.php.string.stubStringEncoder', 
    1213                      'net.stubbles.util.validators.stubMaxLengthValidator', 
    1314                      'net.stubbles.util.validators.stubMinLengthValidator', 
     
    4041     */ 
    4142    protected $allowedTags = array(); 
     43    /** 
     44     * the encoder class to be used 
     45     * 
     46     * @var  stubReflectionClass 
     47     */ 
     48    protected $encoderClass = null; 
     49    /** 
     50     * the encoding mode to be applied 
     51     * 
     52     * @var  int 
     53     */ 
     54    protected $encoderMode  = stubStringEncoder::MODE_DECODE; 
     55 
     56    /** 
     57     * checks if the encoder mode is correct 
     58     * 
     59     * @throws  ReflectionException 
     60     */ 
     61    public function finish() 
     62    { 
     63        if (null !== $this->encoderClass && in_array($this->encoderMode, array(stubStringEncoder::MODE_DECODE, stubStringEncoder::MODE_ENCODE)) === false) { 
     64            throw new ReflectionException('Can not use ' . $this->getClassName() . ' with wrong encoder mode ' . $this->encoderMode); 
     65        } 
     66    } 
    4267 
    4368    /** 
     
    75100 
    76101    /** 
     102     * sets the encoder class to be used 
     103     * 
     104     * @param  stubReflectionClass  $encoder 
     105     */ 
     106    public function setEncoder(stubReflectionClass $encoderClass) 
     107    { 
     108        $this->encoderClass = $encoderClass; 
     109    } 
     110 
     111    /** 
     112     * sets the encoder mode to be used 
     113     * 
     114     * @param  int  $mode 
     115     */ 
     116    public function setEncoderMode($mode) 
     117    { 
     118        $this->encoderMode = $mode; 
     119    } 
     120 
     121    /** 
    77122     * returns the filter defined by the annotation 
    78123     * 
     
    91136        } 
    92137         
     138        if (null !== $this->encoderClass) { 
     139            $textFilter->setEncoder($this->encoderClass->newInstance(), $this->encoderMode); 
     140        } 
     141         
    93142        $textFilter->setAllowedTags($this->allowedTags); 
    94143        return $textFilter; 
  • trunk/src/main/php/net/stubbles/ipo/request/filters/stubAbstractStringFilter.php

    r968 r969  
    9999 
    100100    /** 
     101     * returns the encoder to be applied onto the value to filter 
     102     * 
     103     * @return  stubStringEncoder 
     104     */ 
     105    public function getEncoder() 
     106    { 
     107        return $this->encoder; 
     108    } 
     109 
     110    /** 
     111     * returns the encoder mode to be applied onto the value to filter 
     112     * 
     113     * @return  int 
     114     */ 
     115    public function getEncoderMode() 
     116    { 
     117        return $this->encoderMode; 
     118    } 
     119 
     120    /** 
    101121     * filter strings 
    102122     * 
  • trunk/src/test/php/net/stubbles/ipo/request/broker/annotations/stubStringFilterAnnotationTestCase.php

    r964 r969  
    6363 
    6464    /** 
     65     * assert that the encoder is set correct 
     66     */ 
     67    public function testWithEncoder() 
     68    { 
     69        $this->stringFilterAnnotation->setEncoder(new stubReflectionClass('net.stubbles.php.string.stubMd5Encoder')); 
     70        $stringFilter = $this->stringFilterAnnotation->getFilter(); 
     71        $this->assertIsA($stringFilter->getEncoder(), 'stubMd5Encoder'); 
     72        $this->assertEqual($stringFilter->getEncoderMode(), stubStringEncoder::MODE_DECODE); 
     73        $this->stringFilterAnnotation->setEncoderMode(stubStringEncoder::MODE_ENCODE); 
     74        $stringFilter = $this->stringFilterAnnotation->getFilter(); 
     75        $this->assertIsA($stringFilter->getEncoder(), 'stubMd5Encoder'); 
     76        $this->assertEqual($stringFilter->getEncoderMode(), stubStringEncoder::MODE_ENCODE); 
     77    } 
     78 
     79    /** 
    6580     * test that finish() works as expected 
    6681     */ 
    67     public function testFinish() 
     82    public function testFinishWithoutRegex() 
    6883    { 
    6984        $this->stringFilterAnnotation->setRegex(); 
     
    7186        $this->stringFilterAnnotation->finish(); 
    7287    } 
     88 
     89    /** 
     90     * test that finish() works as expected 
     91     */ 
     92    public function testFinishWithWrongEncoderMode() 
     93    { 
     94        $this->stringFilterAnnotation->setEncoder(new stubReflectionClass('net.stubbles.php.string.stubMd5Encoder')); 
     95        $this->stringFilterAnnotation->setEncoderMode('invalid'); 
     96        $this->expectException('ReflectionException'); 
     97        $this->stringFilterAnnotation->finish(); 
     98    } 
    7399} 
    74100?> 
  • trunk/src/test/php/net/stubbles/ipo/request/broker/annotations/stubTextFilterAnnotationTestCase.php

    r731 r969  
    5959        $this->assertEqual($textFilter->getAllowedTags(), array('foo', 'bar', 'baz')); 
    6060    } 
     61 
     62    /** 
     63     * assert that the encoder is set correct 
     64     */ 
     65    public function testWithEncoder() 
     66    { 
     67        $this->textFilterAnnotation->setEncoder(new stubReflectionClass('net.stubbles.php.string.stubMd5Encoder')); 
     68        $textFilter = $this->textFilterAnnotation->getFilter(); 
     69        $this->assertIsA($textFilter->getEncoder(), 'stubMd5Encoder'); 
     70        $this->assertEqual($textFilter->getEncoderMode(), stubStringEncoder::MODE_DECODE); 
     71        $this->textFilterAnnotation->setEncoderMode(stubStringEncoder::MODE_ENCODE); 
     72        $textFilter = $this->textFilterAnnotation->getFilter(); 
     73        $this->assertIsA($textFilter->getEncoder(), 'stubMd5Encoder'); 
     74        $this->assertEqual($textFilter->getEncoderMode(), stubStringEncoder::MODE_ENCODE); 
     75    } 
     76 
     77    /** 
     78     * test that finish() works as expected 
     79     */ 
     80    public function testFinishWithWrongEncoderMode() 
     81    { 
     82        $this->textFilterAnnotation->setEncoder(new stubReflectionClass('net.stubbles.php.string.stubMd5Encoder')); 
     83        $this->textFilterAnnotation->setEncoderMode('invalid'); 
     84        $this->expectException('ReflectionException'); 
     85        $this->textFilterAnnotation->finish(); 
     86    } 
    6187} 
    6288?>